VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Close a window

by Marcus Schmitt (6 Submissions)
Category: Windows API Call/Explanation
Compatability: Visual Basic 5.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (5 Votes)

Close a window when you know the title of
this window.
Uses the API FindWindow and PostMessage.

Rate Close a window

Option Explicit
Private Declare Function PostMessage Lib "user32" _
  Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32" _
  Alias "FindWindowA" (ByVal szClass$, ByVal szTitle$) As Long
Private Const WM_CLOSE = &H10
Private Sub Command1_Click()
  Dim hWnd, retval As Long
  Dim WinTitle As String
  WinTitle = "Recycle Bin" '<- Title of Window
  hWnd = FindWindow(vbNullString, WinTitle)
  retval = PostMessage(hWnd, WM_CLOSE, 0&, 0&)
End Sub

Download this snippet    Add to My Saved Code

Close a window Comments

No comments have been posted about Close a window. Why not be the first to post a comment about Close a window.

Post your comment

Subject:
Message:
0/1000 characters