简体   繁体   English

如何知道是否未选择表单?

[英]How to know if a form is not selected?

I have a lots of problem to distinguish such a simple thing. 区分这种简单的事情有很多问题。

I need to know if a form is currently in front of everything, the one which receives key entries. 我需要知道当前是否存在一种形式,即接收关键条目的形式。

I have no way to know if it is. 我没有办法知道。

I can check if not minimized. 我可以检查是否没有最小化。 But then it may just be behind other windows, or just not being selected (for example it is openend, desktop is behind, you click on desktop, then you still see the application, but it doesn't receive key inputs). 但是然后它可能就在其他窗口的后面,或者只是未被选中(例如,它是openend的,桌面在后面,您单击桌面,然后您仍然看到该应用程序,但是它没有收到关键输入)。

The property focus is irrevelant for this. 物业focus对此毫无意义。

Here is the code 这是代码

    protected override void OnActivated(EventArgs e)
    {
        base.OnActivated(e);

        if (this.Focused)
        {
            gotFocus = true;
            // never reaches tis
        }

Check if window is the current active window. 检查窗口是否为当前活动窗口。

Code: 码:

using System.Runtime.InteropServices; // To use DllImport

... ...

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

        if ((IntPtr)GetForegroundWindow() == this.Handle)
        {
            // Do stuff
        }

See: Use GetForegroundWindow result in an if statement to check user's current window 请参阅: 在Get语句中使用GetForegroundWindow结果检查用户的当前窗口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM