Eur*_*ric 11
一个相当简单的方法是P/Invoke GetForegroundWindow()并将返回的HWND与应用程序的form.Handle属性进行比较.
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,从您的表单:
if (MyNamespace.GFW.IsActive(this.Handle))
{
// Do whatever.
}
Run Code Online (Sandbox Code Playgroud)