How to use WM_Close in C#?

前端 未结 2 778
深忆病人
深忆病人 2020-12-20 22:23

Can anyone provide me an example of how to use WM_CLOSE to close a small application like Notepad?

2条回答
  •  清酒与你
    2020-12-20 22:47

    Provided you already have a handle to send to.

    ...Some Class...
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
    
    //I'd double check this constant, just in case
    static uint WM_CLOSE = 0x10;
    
    public void CloseWindow(IntPtr hWindow)
    {
      SendMessage(hWindow, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
    }
    ...Continue Class...
    

    Getting a handle can be tricky. Control descendant classes (WinForms, basically) have Handle's, and you can enumerate all top-level windows with EnumWindows (which requires more advanced p/invoke, though only slightly).

提交回复
热议问题