简体   繁体   English

如何判断我的程序是否专注于那个程序?

[英]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.

相关问题 如何判断我的程序是否在域控制器上运行? - How can I tell if my program is running on a Domain Controller? 我怎么知道我的程序在单元测试环境下 - How can I tell that my program is under unit test environment 我可以打电话告诉我我的程序是编译器吗 - Can I call tell that my program is compiler 如何使用Reactive绑定到条目的“焦点”事件? - How can I bind to an Entry's “focused” event with Reactive? 如何判断文本框是否集中在操作系统上? - How to tell if a textbox is focused in the OS or not? 如何判断我的方法是否由与主UI不同的线程调用? - How can I tell if my method is being called by a different thread than the main UI one? 如何使用此堆栈跟踪来判断是什么导致控制器的动作中断? - How can I use this stack trace to tell what is causing my controller's action to break? 如何判断我的文件是否已使用他们的 SDK 从 Amazon S3 中成功删除? - How can I tell if my file was successfully deleted from Amazon S3 using their SDK? 如何判断我的对象的值是float还是int? - How can tell if my object's value is a float or int? 我如何告诉 Ninject 不要注入我的构造函数参数之一? - How do I tell Ninject not to inject one of my constructor parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM