Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何查看我的表单当前是否位于其他表单之上?_C#_Winforms_Topmost - Fatal编程技术网

C# 如何查看我的表单当前是否位于其他表单之上?

C# 如何查看我的表单当前是否位于其他表单之上?,c#,winforms,topmost,C#,Winforms,Topmost,基本上,我如何判断我的程序是否比所有其他程序的层次都高?一个相当简单的方法是p/Invoke并比较返回到应用程序的form.Handle属性的HWND using System; using System.Runtime.InteropServices; namespace MyNamespace { class GFW { [DllImport("user32.dll")] private static extern IntPtr GetFor

基本上,我如何判断我的程序是否比所有其他程序的层次都高?

一个相当简单的方法是p/Invoke并比较返回到应用程序的form.Handle属性的HWND

using System;
using System.Runtime.InteropServices;

namespace MyNamespace
{
    class GFW
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        public bool IsActive(IntPtr handle)
        {
            IntPtr activeHandle = GetForegroundWindow();
            return (activeHandle == handle);
        }
    }
}
然后,从您的表单:

if (MyNamespace.GFW.IsActive(this.Handle))
{
  // Do whatever.
}
您可以使用:

WINAPI导入(在类级别):

指定一个属性来保存该值,并通过IDE或在InitializeComponent()之后将检查添加到窗体的GotFocus事件中

e、 g:


如果您的窗口继承了窗体,则可以检查窗体。最顶层属性对相同问题的回答给出了一个很好的解决方案:

///如果当前应用程序具有焦点,则返回true,否则返回false
公共静态布尔应用程序激活()
{
var activatedHandle=GetForegroundWindow();
if(activatedHandle==IntPtr.Zero){
return false;//当前未激活任何窗口
}
var procId=Process.GetCurrentProcess().Id;
int-activeProcId;
GetWindowThreadProcessId(activatedHandle,out-activeProcId);
返回activeProcId==procId;
}
[DllImport(“user32.dll”,CharSet=CharSet.Auto,ExactSpelling=true)]
私有静态外部IntPtr GetForegroundWindow();
[DllImport(“user32.dll”,CharSet=CharSet.Auto,SetLastError=true)]
私有静态extern int GetWindowThreadProcessId(IntPtr句柄,out int processId);

如果您的程序显示对话框或具有可拆卸的窗口(如使用windows停靠框架),Euric当前接受的解决方案不起作用。

两个问题-我需要它告诉何时它成为最顶端的窗口,并且我不知道将导入放在何处。因此,当它成为最顶端的窗体时,将立即运行此解决方案?不,上面的代码示例只是检查是否有前景窗口。要在成为前台窗口时执行代码,必须使用SetWinEventHook。非常感谢,我花了几个小时在谷歌上搜索这个解决方案,但现在你是我的英雄
if (GetForegroundWindow() == Process.GetCurrentProcess().MainWindowHandle)
{
     //do stuff
}
[System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool GetForegroundWindow();
//.....
InitalizeComponent();
this.GotFocus += (myFocusCheck);
//...

private bool onTop = false;

private void myFocusCheck(object s, EventArgs e)
{
    if(GetFore......){ onTop = true; }
}
/// <summary>Returns true if the current application has focus, false otherwise</summary>
public static bool ApplicationIsActivated()
{
    var activatedHandle = GetForegroundWindow();
    if (activatedHandle == IntPtr.Zero) {
        return false;       // No window is currently activated
    }

    var procId = Process.GetCurrentProcess().Id;
    int activeProcId;
    GetWindowThreadProcessId(activatedHandle, out activeProcId);

    return activeProcId == procId;
}


[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);