Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 895
user705414 Avatar asked Mar 01 '12 15:03

user705414


People also ask

How do I find and close the window using win API?

This article explains how to find and close the window using Win API . The FindWindow function retrieves a handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows. This function does not perform a case-sensitive search.

How do I Close a window in Windows 10?

The user can close an application window by clicking the Close button, or by using a keyboard shortcut such as ALT+F4. Any of these actions causes the window to receive a WM_CLOSE message. The WM_CLOSE message gives you an opportunity to prompt the user before closing the window.

What is the use of CloseHandle in Linux?

In general, CloseHandle invalidates the specified object handle, decrements the object's handle count, and performs object retention checks. After the last handle to an object is closed, the object is removed from the system. For a summary of the creator functions for these objects, see Kernel Objects .

How do I Close a window when it is not responding?

If you really do want to close the window, call the DestroyWindow function. Otherwise, simply return zero from the WM_CLOSE message, and the operating system will ignore the message and not destroy the window. Here is an example of how a program might handle WM_CLOSE.


1 Answers

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);
}
like image 95
JaredPar Avatar answered Sep 17 '22 11:09

JaredPar