[英]How can I tell if my program is the one that's focused?
I can't find a good answer to this... 我找不到一个好的答案......
What I'm asking specifically is, how can I tell if my program (winforms) is visible to the user, and it's focused? 我具体要问的是,我如何判断我的程序(winforms)是否对用户可见,并且它是专注的? For example, when you use Skype, it can tell whether it is the one focused and visible or not.
例如,当您使用Skype时,它可以判断它是否是焦点和可见的。 If you receive a Skype IM and the program is either not visible (hidden behind other windows)/minimized it will make the notification sound.
如果您收到Skype IM并且该程序不可见(隐藏在其他窗口后面)/最小化它将使通知发出声音。 If Skype knows that it's the one with the focus, the top most application , it won't bother to make the notification because it assumes you're using it.
如果Skype知道它是具有焦点的应用程序 ,也是最重要的应用程序 ,它将不会费心去做通知,因为它假设您正在使用它。
Now, I've tried if (this.Focused == true)
, but it doesn't seem to work at all. 现在,我已经尝试了
if (this.Focused == true)
,但它似乎根本不起作用。 It can't tell that I'm giving focus to Windows Explorer and that my own program is at the back of it, not focused and greyed out at the top. 它无法分辨我将重点放在Windows资源管理器上,并且我自己的程序位于它的后面,而不是专注于顶部而且是灰色的。
I've just tested it out and it works just the way I wanted it! 我刚刚测试了它,它的工作方式就像我想要的那样! I can check if
Form.ActiveForm
is null: if it's null, then it doesn't have focus; 我可以检查
Form.ActiveForm
是否为null:如果它为null,则它没有焦点; if it's not null, it has focus! 如果它不是null,它有焦点!
An easier way of doing what you want is by using events. 通过使用事件,您可以更轻松地完成所需操作。
Here is an excellent post about that https://stackoverflow.com/a/1786376/2692994 这是一篇关于https://stackoverflow.com/a/1786376/2692994的优秀文章
Inside the event, you put whatever you want to do with your form. 在活动中,您可以放置任何您想要的表单。 ^ I added the above since that is probably what Skype uses.
^我添加了上述内容,因为这可能是Skype使用的内容。 If you want to check at any time if it's focused or not, just make a "public bool focused = true;"
如果你想在任何时候检查它是否重点关注,只需制作一个“public bool focused = true;” and set it inside of the events to true or false.
并将其设置在事件内部为true或false。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.