Reputation: 21200
Given a handle of a window, how can I close the window by using the window handle?
Upvotes: 9
Views: 13280
Reputation: 8408
Call CloseWindow via P/Invoke:
http://www.pinvoke.net/default.aspx/user32.closewindow
Or DestroyWindow
http://www.pinvoke.net/default.aspx/user32/DestroyWindow.html
Upvotes: -1
Reputation: 2880
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);
Upvotes: 1
Reputation: 755169
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);
}
Upvotes: 22