Maanu
Maanu

Reputation: 5203

Get Active Window of .net application

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?

Upvotes: 2

Views: 24649

Answers (2)

Brian
Brian

Reputation: 7156

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

Upvotes: 7

Related Questions