问题
Given a handle of a window, how can I close the window by using the window handle?
回答1:
The easiest way is to use PInvoke and do a SendMessage
with WM_CLOSE
.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
private const UInt32 WM_CLOSE = 0x0010;
void CloseWindow(IntPtr hwnd) {
SendMessage(hwnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
回答2:
Not sure if there is another way but you could PInvoke the following:
// close the window using API
SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
回答3:
Call CloseWindow via P/Invoke:
http://www.pinvoke.net/default.aspx/user32.closewindow
Or DestroyWindow
http://www.pinvoke.net/default.aspx/user32/DestroyWindow.html
来源:https://stackoverflow.com/questions/9519206/give-a-windows-handle-native-how-to-close-the-windows-using-c