(*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:
  1. Option Explicit
  2.  
  3. 'declare API:
  4. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
  5.   (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  6.  
  7. Private Sub Form_Load()
  8. Dim strCaption As String, lhWnd As Long
  9.  
  10.   'Exact caption of the window:
  11.   strCaption = "Untitled - Notepad"
  12.   lhWnd = FindWindow(vbNullString, strCaption)
  13.  
  14.   'if the result is 0, window was not found:
  15.   If lhWnd = 0 Then
  16.     MsgBox "Could not find Notepad..."
  17.   Else
  18.     MsgBox "Notepad found: " & lhWnd
  19.   End If
  20. End Sub

Get HWND from class name
The other method would be to use the window's class name:


VB Code:
  1. Option Explicit
  2.  
  3. 'declare API:
  4. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
  5.   (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  6.  
  7. Private Sub Form_Load()
  8. Dim strClassName As String, lhWnd As Long
  9.  
  10.   'Class Name of the window:
  11.   strClassName = "Notepad"
  12.   lhWnd = FindWindow(strClassName, vbNullString)
  13.  
  14.   'if the result is 0, window was not found:
  15.   If lhWnd = 0 Then
  16.     MsgBox "Could not find Notepad..."
  17.   Else
  18.     MsgBox "Notepad found: " & lhWnd
  19.   End If
  20. End Sub