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

2023-09-03 05:28:43 作者:- 王道

我有一个名为ProLaunch.exe的应用程序。我想在成为活动窗口,并关闭它,如果用户没有进行任何操作的期间speicified。在应用一个定时器将被用于此目的。

I have an application named 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?

推荐答案

如果我理解正确的问题,你可以使用Win32 API的 GetActiveWindow 这一点。这应该在两个窗体和WPF应用程序。

If I understand the question correctly, you can use the Win32 API GetActiveWindow for this. This should work in both Forms and WPF apps.

[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);
}