我有一个名为ProLaunch.exe的应用程序。 如果用户在指定的时间段内没有执行任何操作,我想获得活动窗口并closures它。 应用程序中的计时器将用于此目的。
我怎样才能得到活动窗口,并closures它?
如果我正确地理解了这个问题,那么可以使用Win32 API GetActiveWindow 。 这应该适用于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); }