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

时间:2021-07-03 20:47:25

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? For example, when you use Skype, it can tell whether it is the one focused and visible or not. If you receive a Skype IM and the program is either not visible (hidden behind other windows)/minimized it will make the notification sound. 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.

我具体要问的是,我如何判断我的程序(winforms)是否对用户可见,并且它是专注的?例如,当您使用Skype时,它可以判断它是否是聚焦和可见的。如果您收到Skype IM并且该程序不可见(隐藏在其他窗口后面)/最小化它将使通知发出声音。如果Skype知道它是具有焦点的应用程序,也是最重要的应用程序,它将不会费心去做通知,因为它假设您正在使用它。

Now, I've tried if (this.Focused == true), but it doesn't seem to work at all. 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.

现在,我已经尝试过(this.Focused == true),但它似乎根本不起作用。它无法分辨我将重点放在Windows资源管理器上,并且我自己的程序位于它的后面,而不是专注于顶部的灰色。

2 个解决方案

#1


3  

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; if it's not null, it has focus!

我刚刚测试了它,它的工作方式就像我想要的那样!我可以检查Form.ActiveForm是否为null:如果它为null,则它没有焦点;如果它不是null,它有焦点!

#2


1  

An easier way of doing what you want is by using events.

做你想做的更简单的方法是使用事件。

Here is an excellent post about that https://*.com/a/1786376/2692994

这是一篇关于https://*.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. If you want to check at any time if it's focused or not, just make a "public bool focused = true;" and set it inside of the events to true or false.

在活动中,您可以将您想要的任何内容放入表单中。 ^我添加了上述内容,因为这可能是Skype使用的内容。如果你想在任何时候检查它是否有重点,只需制作一个“公共bool focus = true;”并将其设置在事件内部为true或false。

#1


3  

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; if it's not null, it has focus!

我刚刚测试了它,它的工作方式就像我想要的那样!我可以检查Form.ActiveForm是否为null:如果它为null,则它没有焦点;如果它不是null,它有焦点!

#2


1  

An easier way of doing what you want is by using events.

做你想做的更简单的方法是使用事件。

Here is an excellent post about that https://*.com/a/1786376/2692994

这是一篇关于https://*.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. If you want to check at any time if it's focused or not, just make a "public bool focused = true;" and set it inside of the events to true or false.

在活动中,您可以将您想要的任何内容放入表单中。 ^我添加了上述内容,因为这可能是Skype使用的内容。如果你想在任何时候检查它是否有重点,只需制作一个“公共bool focus = true;”并将其设置在事件内部为true或false。