如何检查我的应用程序是否有焦点?
答
致电Windows.GetForegroundWindow()
然后将HWND
传递给Controls.FindControl()
函数。如果HWND
属于您的进程,它将返回非零TWinControl
指针。例如:
if FindControl(GetForegroundWindow()) <> nil then
// has focus ...
else
// does not have focus ...
答
如果您的应用程序由一个单一的形式,然后
GetForegroundWindow = Handle
就足够了。上面的表达式当且仅当您的窗体是前景窗口,也就是说,如果键盘焦点属于此窗体上的控件(或窗体本身),则上述表达式为true。
如果您的应用程序包含多个表单,只需遍历它们并检查它们中的任何一个是否匹配GetForegroundWindow
。
答
上Remys响应的细微变化是:
Var
Win: TWinControl;
Begin
Win := FindControl(GetForegroundWindow);
if Win <> nil then
// StringGrid1.Row :=5;
// StringGrid1.SetFocus;
编译OK的我,但我发现它不可靠的调试过程中,stringgrid.setfocus被称为即使窗口ISN”重点导致一个错误。
答
D2007具有这种有用的属性
if Application.Active then
//