如何检查我的应用程序是否具有焦点?
我想要做的是检查我的应用程序是否具有焦点,因为如果没有焦点,我将在通知区域上方弹出一个警报窗口,以向最终用户显示一些消息。
What I want to do is check if my application has focus because if it is not then I will popup an Alert Window just over the Notification Area to display some message to the end user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
调用
Windows.GetForegroundWindow()
,然后将HWND
传递给Controls.FindControl()
函数。如果HWND
属于您的进程,它将返回一个非零的TWinControl
指针。例如:Call
Windows.GetForegroundWindow()
and then pass theHWND
to theControls.FindControl()
function. It will return a non-nilTWinControl
pointer if theHWND
belongs to your process. For example:D2007 有这个有用的属性
D2007 has this useful property
如果您的申请由单一表格组成,那么
就足够了。当且仅当您的窗体是前台窗口,即键盘焦点属于该窗体上的控件(或窗体本身)时,上面的表达式才成立。
如果您的应用程序由多个表单组成,只需循环遍历它们并检查其中是否有任何一个与
GetForegroundWindow
匹配。If your application consists of a single form, then
will suffice. The expression above is true if and only if the your form is the foreground window, that is, if keyboard focus belongs to a control on this form (or to the form itself).
If your application consists of a number of forms, simply loop through them and check if any of them matches
GetForegroundWindow
.Remys 响应的一个细微变化是:
对我来说编译没问题,但我发现它在调试期间不可靠,即使窗口未聚焦导致错误,也会调用 stringgrid.setfocus 。
A slight variation on Remys response is:
compiled ok for me, but I found it unreliable during debug, the stringgrid.setfocus is called even when the window isn't focused causing an error.