(*Credit Goes to The Hobo*)
How to get a windows HWND
Since many tasks through API can be used on other windows, such as changing a caption or getting a caption, it'd
be important to first now how to obtain the HWND (which is used in most API) of another window. There are two methods
explored below:
Get HWND from caption
This example requires that you know the exact caption of the window, such as 'Untitled' with Notepad.
VB Code:
Option Explicit 'declare API: Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Sub Form_Load() Dim strCaption As String, lhWnd As Long 'Exact caption of the window: strCaption = "Untitled - Notepad" lhWnd = FindWindow(vbNullString, strCaption) 'if the result is 0, window was not found: If lhWnd = 0 Then MsgBox "Could not find Notepad..." Else MsgBox "Notepad found: " & lhWnd End If End Sub
Get HWND from class name
The other method would be to use the window's class name:
VB Code:
Option Explicit 'declare API: Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Sub Form_Load() Dim strClassName As String, lhWnd As Long 'Class Name of the window: strClassName = "Notepad" lhWnd = FindWindow(strClassName, vbNullString) 'if the result is 0, window was not found: If lhWnd = 0 Then MsgBox "Could not find Notepad..." Else MsgBox "Notepad found: " & lhWnd End If End Sub




Reply With Quote