destroywindow (user32)
Last changed: ksgfk-176.122.161.184

.
Summary
Destroys the specified window.

C# Signature:

/// <summary>
/// <para>The DestroyWindow function destroys the specified window. The function sends WM_DESTROY and WM_NCDESTROY messages to the window to deactivate it and remove the keyboard focus from it. The function also destroys the window's menu, flushes the thread message queue, destroys timers, removes clipboard ownership, and breaks the clipboard viewer chain (if the window is at the top of the viewer chain).</para>
/// <para>If the specified window is a parent or owner window, DestroyWindow automatically destroys the associated child or owned windows when it destroys the parent or owner window. The function first destroys child or owned windows, and then it destroys the parent or owner window.</para>
/// <para>DestroyWindow also destroys modeless dialog boxes created by the CreateDialog function.</para>
/// </summary>
/// <param name="hwnd">Handle to the window to be destroyed.</param>
/// <returns>If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.</returns>
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DestroyWindow(IntPtr hwnd);

VB Signature:

Declare Function DestroyWindow Lib "user32.dll" (hWnd As IntPtr) As Boolean

VB.NET Signature

<DllImport("user32.dll", SetLastError := True, CharSet := CharSet.Unicode)> _
Private Function  DestroyWindow (hWnd As IntPtr) As <MarshalAs(UnmanagedType.U1)> Boolean
End Function

Notes:

hWnd is the pointer to the window you want to close/destroy. Create a new IntPtr object with the window's handle as a parameter to it's constructor. Make sure the pointers are in decimal, not in hex as spy++ gives them to you.

Tips & Tricks:

Please add some!

Sample Code:

[DllImport("user32.dll")]

static extern bool DestroyWindow(IntPtr hWnd);

//Closes the form

btnCloseWindow_Click(object sender, EventArgs e)

{

    DestroyWindow(this.Handle)    

}

Alternative Managed API:

System.Windows.Forms.Form.Close

Documentation