Sending the WM_CLOSE message to a window, naturally enough, instructs it to close. Some windows will prompt the user with a confirmation dialog box before it closes, while others will close immediately. Exactly what happens depends on the program. Just keep in mind that sending WM_CLOSE merely asks the window to close -- there's no guarantee that will actually happen.
This message should always return zero.
None.
Const WM_CLOSE = &H10
Let the user enter the title of a window in a text box. Then, attempt to close the window whose title bar matches that text when the user clicks a button. To use this example, place a text boxed named txtTitle and a command button named cmdClose on a form window.
' This code is licensed according to the terms and conditions listed here.
' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal _
Msg As Long, wParam As Any, lParam As Any) As Long
Public Const WM_CLOSE = &H10
Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
' *** Place the following code inside the form window. ***
Private Sub cmdClose_Click ()
Dim hWnd As Long ' handle of the window to try to close
Dim result As Long ' result of the message
' Get a handle to the window whose title the user entered.
hWnd = FindWindow(vbNullString, txtTitle.Text)
If hWnd <> 0 Then
' Ask the window to close.
result = SendMessage(hWnd, WM_CLOSE, ByVal CLng(0), ByVal CLng(0))
Debug.Print "Told " & txtTitle.Text & " to close."
Else
' Couldn't find the window the user specified.
Debug.Print "Unable to find window titled " & txtTitle.Text & "."
End If
End Sub
Back to the Function list.
Back to the Reference section.
Last Modified: December 17, 2000
This page is copyright © 2000 Paul Kuliniewicz.
Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/w/wm_close.html