我已经为我的应用程序创建了一些全局热键,并且我希望它们仅在我的应用程序处于活动状态时才起作用。(如果我的应用程序不是活动表单,则它不起作用)。
那么,如何检查我的C#
winform应用程序是否是所有其他Windows应用程序中的活动窗体?
我试过
if(this.Focused)
//Do somthing
但这没用
尝试这个:
[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);
public static bool Activates()
{
var x = GetForegroundWindow();
if (x == IntPtr.Zero) {
return false;
}
var y = Process.GetCurrentProcess().Id;
int i;
GetWindowThreadProcessId(x, out i);
return i == y;
}
您还可以参考:C#:检测哪个应用程序具有焦点
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句