Windows可以在单个控件上管理多个焦点吗? [英] Can Windows manage multiple focus points on a single control?

查看:378
本文介绍了Windows可以在单个控件上管理多个焦点吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个有多个焦点的自定义控件。例如,在1个控件中,假设有3个区域(可以定义为画布上的矩形),以便能够获得焦点。取决于哪一个具有焦点,我不希望它周围的默认Windows虚线,但一些特殊的处理。原来的项目是一个单独的TPanel,上面有几个VCL控件,当然每个控件都是自己的自己的窗口,因此有自己的重点。但是现在我把它放到一个自己定制的类中,这三个控件将不再存在(它们只是版本1中的原型),现在我需要以某种方式模仿这些不同区域的焦点。 / p>

我觉得类似于TListBox这样简单的东西,其中控件中的某些项目获得了焦点,而不是整个控件的自身。



下面是一张图片,以帮助证明我在做什么...



最上面的是带按钮的原件。但是底部的是我正在构建的新的,这是所有的自定义绘制。

详细说,我想看看如果Windows已经有特殊的处理对于这种情况下,我去之前,重新发明轮子。

我不是在寻找最简单的方式来实现这一点。而且我也不想建议恢复到以前的状态,因为有很多原因我不希望其中有多个其他控件。我只是需要一个是或否,与解释。

更多



我只是意识到主要关心的是使用选项卡键。控件必须首先获得焦点,开始关注它应该的任何子项,然后在我的命令中对 tab 作出响应,直到达到结束,然后通过切换到下一个控件。那么它也需要shift + tab。我怎样才能实现这个标签?这就是我陷入困境的地方,但是让我觉得这是我所要求的主要原因。 解决方案

让窗口识别同一个窗口句柄中的多个键盘焦点,因为每个带窗口句柄的控件都有或没有键盘焦点。多重控制之间的内在焦点由您自己决定。



正如您已经知道的,最简单的方法是使用多个子控件用自己的窗口句柄,这就是你说的你正在做的:

pre $ t $ t $ t $没有窗户把手!
保护
FEdit1:TEdit;
FEdit2:TEdit;
FEdit3:TEdit;
...
end

在上面的情况下,父控件是一个TControl,它创建了几个子控件,在我上面的例子中,所有三个都有自己的窗口句柄,因此Windows可以在您点击标签时显示键盘焦点,并将鼠标焦点作为Windows常用控件库功能的一部分。



简而言之,在主控制中包含子对象(其他控件)的组合方法实际上是唯一的方法让Windows完成大部分的工作。另一方面,你可能正在寻找的不是控制绘制本身的方法,而是一些代码让东西看起来像是聚焦在你自己定制的绘画例程中,如果你正在寻找的东西,你应该看看VCL源代码或这个 link on about.com如何告诉Windows绘制焦点矩形等。about.com链接是一个模仿,并不使用真正的Windows代码来绘制一个Windows主题感知的方式焦点。

更新:你也在寻找的方法是确定鼠标坐标是否在指定的矩形内(矩形代表你的情况下的一个按钮),如果是,绘制一个热状态为按钮。如果你想自己构建一个控件,还有更多的子任务需要完成,我建议你研究VCL源代码中的TStringGrid和TCategoryButtons等现有的控件,以查看你将需要的MouseMove,MouseDown和MouseUp处理代码做你正在做的事情。特别是,StringGrid源代码是查看如何使用单个窗口句柄在单个控件中使用tab键的方式,因为在该控件中可以使用tab键(如果正确的选项打开)在字符串网格中的所有单元格之间导航,就好像每个单元格都是单独的控件,即使它是一个控件也是如此。


I'm building a custom control which is to have multiple focus points. For example, within 1 control, let's say there's 3 regions (could be defined as a rect on the canvas) which are to be able to have focus. Depending on which one has focus, I don't want the default Windows dotted line around it, but some special handling. I know nothing about how to even give 1 custom control its own focus.

The original project was a single TPanel with a few VCL controls on it, each of course being its own window, thus having its own focus. But now I'm putting it into a custom class of its own, which these 3 controls will no longer exist (they were only there in version 1 as a prototype) and I need to now somehow mimic the focus in these different regions.

I guess similar to something as simple as a TListBox, where certain items within that control get the focus instead of the entire control its self.

Here's a picture to help demonstrate what I'm making...

The one on the top is the original with the buttons. But the one on the bottom is the new one I'm building which is all custom drawn.

To elaborate, I'd like to see if Windows already has special handling for this type of scenario before I go and re-invent the wheel.

I'm not looking for the "Easiest" way to accomplish this. And I also do not want recommendations to revert back to how I had it before, because there's many reasons I don't want 1 control with multiple other controls within. I just need a yes or no, with an explanation.

More

I just realized the main concern is the use of the tab key. The control has to first get the focus, start the focus on whichever sub-item it's supposed to, then respond to tab on my command until it reaches the end, then pass the tabbing over to the next control. Then it also needs shift+tab as well. How on earth would I implement this tabbing? That's where I got stuck, but it just struck me that this is the main reason I'm asking.

解决方案

No you can't get windows to recognize multiple points of keyboard focus inside the same window handle, since each control with a window handle either has, or does not have, keyboard focus. The "inner focus" between multiple controls is up to you to sort out.

As you already knew, the most simple way to accomplish this is to have multiple sub-controls with their own window-handles, which is what you said you are doing:

TMyThreeEditControls = class(TControl) //  parent has no window handle!!!!
    protected
        FEdit1:TEdit;
        FEdit2:TEdit;
        FEdit3:TEdit;
    ...
end

In the situation above, the parent control is a TControl, it creates several sub controls, in my example above, all three have their own window handles, and thus Windows can display keyboard focus when you hit tab, and handle mouse focus as part of Windows's common controls library's functionality.

In short, the "composite" approach where you include sub-objects (other controls) in your main control, which you are trying to move away from is in fact, the only way to let Windows do most of the work.

On the other hand, what you might be looking for is not a way to have a control paint itself, but some code to make something look like it is focused, in your own custom painting routines, if that is what you are looking for, you should look into the VCL source code or this link on about.com for examples on how to tell Windows to draw a focus rectangle, etc. The about.com link is an imitation and does not use real windows code to draw focus in a Windows-theme aware way.

Update: it is possible that what you are also looking for is the way to determine if mouse co-ordinates are within a specified rectangle (the rectangle represents a button in your case) and if so, to draw a "hot state" for the button. There are more sub-tasks than this to accomplish if you wish to build a control yourself, I recommend you study existing controls such as TStringGrid and TCategoryButtons in the VCL source code, to see the MouseMove, MouseDown, and MouseUp handling code you will need to do the things you are trying to do. In particular, StringGrid source code is the way to see how the "tab key" can be used within a single control with a single window handle, because in that control the tab key can be used (if the right options are turned on) to navigate among all the cells inside the string grid, as if each one was a separate control, even though it is all one control.

这篇关于Windows可以在单个控件上管理多个焦点吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆