如何判断form中的某个控件是否拥有输入焦点?
比如一个form中有两个grid,如何得知用户当前选中的是哪个grid? 问题点数:60、回复次数:14Top
1 楼lynu(南拳王)回复于 2001-08-20 11:21:57 得分 10
TForm有一个ActiveControl属性指明当前得到焦点的控件.
Top
2 楼wxcwuxuchun(清风)回复于 2001-08-20 11:23:30 得分 10
Focused(),请你看看帮助Top
3 楼Netguy(老家伙)回复于 2001-08-20 11:24:36 得分 0
谢谢。我试试。
具体说,是想达到如下的效果:
当用户点击form中的“添加”按钮时,如果第一个grid拥有焦点,则给第一个grid添加一行;如果第2个grid拥有焦点,则给第2个grid添加一行。Top
4 楼wow228(合欢猪)回复于 2001-08-20 11:31:01 得分 5
凡是基于TwinControl的控件都可以接受输入焦点!
Top
5 楼Netguy(老家伙)回复于 2001-08-20 11:34:11 得分 0
单步跟了一下,由于grid中的每个cell也是个可以接受输入焦点的控件,用form->ActiveControl得到的值似乎不是某个grid的指针。Top
6 楼wxcwuxuchun(清风)回复于 2001-08-20 11:35:01 得分 5
if (grid1.focused())
//insert 1
else if (grid2.focused())
//insert 2
Top
7 楼Netguy(老家伙)回复于 2001-08-20 11:39:56 得分 0
选中一个grid并将光标集中在其中一个cell上,当点击了“确定”按钮后,两个grid的Focused( )方法都返回fasle ;(Top
8 楼wujinbao(abao)回复于 2001-08-20 11:41:11 得分 10
if(ActiveControl ==Grid1)
//insert 1;
else if(ActiveControl == Grid2)
//insert 2;Top
9 楼gloom(苍狗白云)回复于 2001-08-20 11:46:01 得分 10
用API吧,GetFocus()Top
10 楼gloom(苍狗白云)回复于 2001-08-20 11:46:55 得分 0
GetFocus
The GetFocus function retrieves the handle of the window that has the keyboard focus, if the window is associated with the calling thread's message queue.
HWND GetFocus(VOID)
Parameters
This function has no parameters.
Return Values
If the function succeeds, the return value is the handle of the window with the keyboard focus. If the calling thread's message queue does not have an associated window with the keyboard focus, the return value is NULL.
Remarks
Even if GetFocus returns NULL, another thread's queue may be associated with a window that has the keyboard focus.
Use the GetForegroundWindow function to retrieve the handle to the window with which the user is currently working. You can associate your thread's message queue with the windows owned by another thread by using the AttachThreadInput function.
See Also
AttachThreadInput, GetForegroundWindow, SetFocus, WM_KILLFOCUS, WM_SETFOCUS
Top
11 楼Netguy(老家伙)回复于 2001-08-20 12:21:12 得分 0
用Spy++看了一下,发现都是因为点了“确定”按钮的缘故,点了之后焦点就跑到这个按钮上去了,两个grid都失去了焦点。奈何?Top
12 楼Netguy(老家伙)回复于 2001-08-20 12:55:06 得分 0
加分。
如何在点击“添加”按钮之前保存焦点的位置(即在切换焦点之前记下焦点在哪个控件上)?难道要重载OnMouseDown( )并用GetFocus( )获得当前焦点?有无其它办法?Top
13 楼xzgyb(老达摩)回复于 2001-08-20 12:59:48 得分 10
在两个dbgird的OnExit事件中保留一变量值来跟踪是哪一dbgird,
然后在确定按纽里在判断,行吗
Top
14 楼Netguy(老家伙)回复于 2001-08-20 13:10:33 得分 0
好的,就按xzgyb(回首往事,不堪回首)说的办。
结帖子。谢谢大家Top




