Give a windows handle (Native), how to close the windows using C#?

点点圈 提交于 2021-02-20 09:27:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!