by Joe Estock (3 Submissions)
Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (2 Votes)
This code will show you how to retreive a handle of an open window and how to manipulate that window once you have it's handle.
API Declarations'Add the following declarations to your form's
'General (Declarations) section
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long, ByVal bInvert As Long) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
'Add a command button to a form, set the command
'button's name to Command1, then add this code
'to your form's code.
Private Sub Command1_Click()
Dim lhWnd As Long 'Holds the handle to the window
'The FindWindow parameter is as follows:
'lpClassName: This is the name of the class
' Use the Spy ++ utility to retreive
' this information
'lpWindowName: This is the caption of the window
lhWnd = FindWindow("Minesweeper", "Minesweeper")
Text1.Text = lhWnd
'Make sure we have a valid handle
If lhWnd <> 0 Then
'Flash the window by inverting it
'If it has focus, then remove focus
'If it doesn't have focus, give it focus
FlashWindow lhWnd, 1
End If
If lhWnd <> 0 Then
'Change the window's caption
SetWindowText lhWnd, "ChangedSweeper"
End If
End Sub