user705414
user705414

Reputation: 21200

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

Given a handle of a window, how can I close the window by using the window handle?

Upvotes: 9

Views: 13280

Answers (3)

dice
dice

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

JaredPar
JaredPar

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

Related Questions