2010-09-14 118 views

回答

16

致电Windows.GetForegroundWindow()然后将HWND传递给Controls.FindControl()函数。如果HWND属于您的进程,它将返回非零TWinControl指针。例如:

if FindControl(GetForegroundWindow()) <> nil then 
    // has focus ... 
else 
    // does not have focus ... 
4

如果您的应用程序由一个单一的形式,然后

GetForegroundWindow = Handle 

就足够了。上面的表达式当且仅当您的窗体是前景窗口,也就是说,如果键盘焦点属于此窗体上的控件(或窗体本身),则上述表达式为true。

如果您的应用程序包含多个表单,只需遍历它们并检查它们中的任何一个是否匹配GetForegroundWindow

0

上Remys响应的细微变化是:

Var 
    Win: TWinControl; 
Begin 
    Win := FindControl(GetForegroundWindow); 
    if Win <> nil then 
//  StringGrid1.Row :=5; 
//  StringGrid1.SetFocus; 

编译OK的我,但我发现它不可靠的调试过程中,stringgrid.setfocus被称为即使窗口ISN”重点导致一个错误。

3

D2007具有这种有用的属性

if Application.Active then 
// 
相关问题