README.md

December 5, 2022 ยท View on GitHub

Window Manipulation

Description

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.

More Info

Submitted On
ByJoe Estock
LevelBeginner
User Rating5.0 (10 globes from 2 users)
CompatibilityVB 6.0
CategoryWindows API Call/ Explanation
WorldVisual Basic
Archive File

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

Source Code

'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