简体   繁体   English

获取.net应用程序的活动窗口

[英]Get Active Window of .net application

I have an application named ProLaunch.exe. 我有一个名为ProLaunch.exe的应用程序。 I want to get the active window in it and close it if the user is not performing any operation for the speicified period. 我想在其中获取活动窗口,如果用户在指定时间内未执行任何操作,则将其关闭。 A timer in the application will be used for this purpose. 应用程序中的计时器将用于此目的。

How can I get the active window and close it? 如何获得活动窗口并关闭它?

If I understand the question correctly, you can use the Win32 API GetActiveWindow for this. 如果我正确理解了该问题,则可以为此使用Win32 API GetActiveWindow This should work in both Forms and WPF apps. 这应该在Forms和WPF应用程序中都可以使用。

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

[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, IntPtr msg, int wParam, int lParam);

public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;

// close the active window using API        
private void FindAndCloseActiveWindow()
{
 IntPtr handle=GetActiveWindow();
 SendMessage(handle, WM_SYSCOMMAND, SC_CLOSE, 0);
}

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

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