You are currently offline, waiting for your internet to reconnect

How To Get a Window's Class Name and Other Window Attributes

This article was previously published under Q112649
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
SUMMARY
You could use SPY.EXE, which comes with Microsoft Visual C/C++, to getinformation such as a window's class name. However, this article shows byexample how you can create your own Visual Basic application that does thesame thing; displays a window's class name along with several otherattributes. For example, you might need a window's class name for use in afunction in the Microsoft Windows Application Programming Interface(Windows API).
MORE INFORMATION
This example uses several functions from the Windows API to get informationabout the window the cursor is currently over. First, the routine callsGetCursorPos to get the current position of the cursor. Then it callsWindowFromPoint to get the handle to the window the cursor is currentlyover. Then it calls several other functions in the Windows API to getspecific information pertaining to the window.

The example finds the following information about the window:
  • Window Handle
  • Window Text
  • Window Class Name
  • Window Style
  • Window ID Number
  • Parent Window Handle (if applicable)
  • Parent Window Text (if applicable)
  • Parent Window Class Name (if applicable)
  • Module File Name
The example can be easily expanded to get other window attributes bycalling appropriate functions in the Windows API.

Step-by-Step Example

THE FOLLOWING SECTION APPLIES TO VISUAL BASIC 4.0 AND 5.0 ONLY:

This example creates a Visual Basic program that produces results similarto those produced by SPY.EXE, and makes use of conditional compilation sothat the program can be run in either 16-bit or 32-bit editions of VisualBasic.

NOTE: Although the code below works in Visual Basic 5.0, it notnecessary to use the conditional compilation (#If Win16, #ElseIf Win32)in Visual Basic 5.0 since it requires 32-bit. As a result, you canshorten the code below by using only the 32-bit declarations.
  1. Start a new project in Visual Basic. Form1 is created by default.
  2. Add the following declarations to the general declarations section of Form1:
       #If Win16 Then      Private Declare Sub GetCursorPos Lib "User" (lpPoint As Long)      Private Declare Function WindowFromPoint Lib "User" (ByVal ptScreen _         As Any) As Integer      Private Declare Function GetModuleFileName Lib "Kernel" (ByVal _         hModule As Integer, ByVal lpFileName As String, ByVal nSize As _         Integer) As Integer      Private Declare Function GetWindowWord Lib "User" (ByVal hwnd As _         Integer, ByVal nIndex As Integer) As Integer      Private Declare Function GetWindowLong Lib "User" (ByVal hwnd As _         Integer, ByVal nIndex As Integer) As Long      Private Declare Function GetParent Lib "User" (ByVal hwnd As _         Integer) As Integer      Private Declare Function GetClassName Lib "User" (ByVal hwnd As _         Integer, ByVal lpClassName As String, ByVal nMaxCount As _         Integer) As Integer      Private Declare Function GetWindowText Lib "User" (ByVal hwnd As _         Integer, ByVal lpString As String, ByVal aint As Integer) As _         Integer   #ElseIf Win32 Then      Private Type POINTAPI         x As Long         y As Long      End Type      Private Declare Function GetCursorPos Lib "User32" (lpPoint As _         POINTAPI) As Long      Private Declare Function WindowFromPointXY Lib "User32" Alias _         "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) _         As Long      Private Declare Function GetModuleFileName Lib "kernel32" Alias _         "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As _         String, ByVal nSize As Long) As Long      Private Declare Function GetWindowWord Lib "User32" (ByVal hwnd As _         Long, ByVal nIndex As Long) As Integer      Private Declare Function GetWindowLong Lib "User32" Alias _         "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As _         Long      Private Declare Function GetParent Lib "User32" (ByVal hwnd As _         Long) As Long      Private Declare Function GetClassName Lib "User32" Alias _         "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As _         String, ByVal nMaxCount As Long) As Long      Private Declare Function GetWindowText Lib "User32" Alias _         "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, _         ByVal cch As Long) As Long   #End If   Const GWW_HINSTANCE = (-6)   Const GWW_ID = (-12)   Const GWL_STYLE = (-16)
  3. Add a timer control (Timer1) to the form.
  4. Set the interval property of the timer to 200.
  5. Add the following code to timer's timer event:
       Sub Timer1_Timer()   #If Win16 Then      Dim ptCursor As Long      Dim sWindowText As String * 100      Dim sClassName As String * 100      Dim hWndOver As Integer      Dim hWndParent As Integer      Dim sParentClassName As String * 100      Dim wID As Integer      Dim lWindowStyle As Long      Dim hInstance As Integer      Dim sParentWindowText As String * 100      Dim sModuleFileName As String * 100      Static hWndLast As Integer      Call GetCursorPos(ptCursor)               ' Get cursor position      hWndOver = WindowFromPoint(ptCursor)      ' Get window cursor is over      If hWndOver <> hWndLast Then              ' If changed update display         hWndLast = hWndOver                    ' Save change         Cls                                      ' Clear the form         Print "Window Handle: &H"; Hex(hWndOver) ' Display window handle         r = GetWindowText(hWndOver, sWindowText, 100)      ' Window text         Print "Window Text: " & Left(sWindowText, r)         r = GetClassName(hWndOver, sClassName, 100)         ' Window Class         Print "Window Class Name: "; Left(sClassName, r)         lWindowStyle = GetWindowLong(hWndOver, GWL_STYLE)   ' Window Style         Print "Window Style: &H"; Hex(lWindowStyle)         ' Get handle of parent window:         hWndParent = GetParent(hWndOver)         ' If there is a parent get more info:         If hWndParent <> 0 Then            ' Get ID of window:            wID = GetWindowWord(hWndOver, GWW_ID)            Print "Window ID Number: &H"; Hex(wID)            Print "Parent Window Handle: &H"; Hex(hWndParent)            ' Get the text of the Parent window:            r = GetWindowText(hWndParent, sParentWindowText, 100)            Print "Parent Window Text: " & Left(sParentWindowText, r)            ' Get the class name of the parent window:            r = GetClassName(hWndParent, sParentClassName, 100)            Print "Parent Window Class Name: "; Left(sParentClassName, r)         Else            ' Update fields when no parent:            Print "Window ID Number: N/A"            Print "Parent Window Handle: N/A"            Print "Parent Window Text : N/A"            Print "Parent Window Class Name: N/A"         End If         ' Get window instance:         hInstance = GetWindowLong(hWndOver, GWW_HINSTANCE)         ' Get module file name:         r = GetModuleFileName(hInstance, sModuleFileName, 100)         If r = 0 then            Print "Module: N/A"         Else                 Print "Module: "; Left(sModuleFileName, r)         End If      End If   #ElseIf Win32 Then      Dim pt32 As POINTAPI      Dim ptx As Long      Dim pty As Long      Dim sWindowText As String * 100      Dim sClassName As String * 100      Dim hWndOver As Long      Dim hWndParent As Long      Dim sParentClassName As String * 100      Dim wID As Long      Dim lWindowStyle As Long      Dim hInstance As Long      Dim sParentWindowText As String * 100      Dim sModuleFileName As String * 100      Static hWndLast As Long      Call GetCursorPos(pt32)               ' Get cursor position      ptx = pt32.x      pty = pt32.y      hWndOver = WindowFromPointXY(ptx, pty)    ' Get window cursor is over      If hWndOver <> hWndLast Then              ' If changed update display         hWndLast = hWndOver                    ' Save change         Cls                                      ' Clear the form         Print "Window Handle: &H"; Hex(hWndOver) ' Display window handle         r = GetWindowText(hWndOver, sWindowText, 100)      ' Window text         Print "Window Text: " & Left(sWindowText, r)         r = GetClassName(hWndOver, sClassName, 100)         ' Window Class         Print "Window Class Name: "; Left(sClassName, r)         lWindowStyle = GetWindowLong(hWndOver, GWL_STYLE)   ' Window Style         Print "Window Style: &H"; Hex(lWindowStyle)         ' Get handle of parent window:         hWndParent = GetParent(hWndOver)         ' If there is a parent get more info:         If hWndParent <> 0 Then            ' Get ID of window:            wID = GetWindowWord(hWndOver, GWW_ID)            Print "Window ID Number: &H"; Hex(wID)            Print "Parent Window Handle: &H"; Hex(hWndParent)            ' Get the text of the Parent window:            r = GetWindowText(hWndParent, sParentWindowText, 100)            Print "Parent Window Text: " & Left(sParentWindowText, r)            ' Get the class name of the parent window:            r = GetClassName(hWndParent, sParentClassName, 100)            Print "Parent Window Class Name: "; Left(sParentClassName, r)         Else            ' Update fields when no parent:            Print "Window ID Number: N/A"            Print "Parent Window Handle: N/A"            Print "Parent Window Text : N/A"            Print "Parent Window Class Name: N/A"         End If         ' Get window instance:         hInstance = GetWindowWord(hWndOver, GWW_HINSTANCE)         ' Get module file name:         r = GetModuleFileName(hInstance, sModuleFileName, 100)         Print "Module: "; Left(sModuleFileName, r)      End If   #End If   End Sub
  6. Save the project files.
  7. Run the program, and move the mouse over different windows. You will see the field values change as you move the mouse over different windows.

Step-by-Step Example

THE FOLLOWING SECTION APPLIES TO VISUAL BASIC 3.0 ONLY:

This example creates a Visual Basic program that produces results similarto those produced by SPY.EXE.
  1. Start a new project in Visual Basic. Form1 is created by default.
  2. Add the following declarations to the general declarations section of Form1:

    'VB3Line: Enter the following lines as one line
       Declare Sub GetCursorPos Lib "User" (lpPoint As Long)   Declare Function WindowFromPoint Lib "User" (ByVal ptScreen As Any) As      Integer   Declare Function GetModuleFileName Lib "Kernel" (ByVal hModule As      Integer, ByVal lpFilename As String, ByVal nSize As Integer) As      Integer   Declare Function GetWindowWord Lib "User" (ByVal hWnd As Integer, ByVal      nIndex As Integer) As Integer   Declare Function GetWindowLong Lib "User" (ByVal hWnd As Integer, ByVal      nIndex As Integer) As Long   Declare Function GetParent Lib "User" (ByVal hWnd As Integer) As Integer   Declare Function GetClassName Lib "User" (ByVal hWnd As Integer, ByVal      lpClassName As String, ByVal nMaxCount As Integer) As Integer   Declare Function GetWindowText Lib "User" (ByVal hWnd As Integer, ByVal      lpString As String, ByVal aint As Integer) As Integer   Const GWW_HINSTANCE = (-6)   Const GWW_ID = (-12)   Const GWL_STYLE = (-16)
  3. Add a timer control (Timer1) to the form.
  4. Set the interval property of the timer to 200.
  5. Add the following code to timer's timer event:
       Sub Timer1_Timer()      Dim ptCursor As Long      Dim sWindowText As String * 100      Dim sClassName As String * 100      Dim hWndOver As Integer      Dim hWndParent As Integer      Dim sParentClassName As String * 100      Dim wID As Integer      Dim lWindowStyle As Long      Dim hInstance As Integer      Dim sParentWindowText As String * 100      Dim sModuleFileName As String * 100      Static hWndLast As Integer      Call GetCursorPos(ptCursor)               ' Get cursor position      hWndOver = WindowFromPoint(ptCursor)      ' Get window cursor is over      If hWndOver <> hWndLast Then              ' If changed update display         hWndLast = hWndOver                    ' Save change         Cls                                      ' Clear the form         Print "Window Handle: &H"; Hex(hWndOver) ' Display window handle         r = GetWindowText(hWndOver, sWindowText, 100)      ' Window text         Print "Window Text: " & Left(sWindowText, r)         r = GetClassName(hWndOver, sClassName, 100)         ' Window Class         Print "Window Class Name: "; Left(sClassName, r)         lWindowStyle = GetWindowLong(hWndOver, GWL_STYLE)   ' Window Style         Print "Window Style: &H"; Hex(lWindowStyle)         ' Get handle of parent window:         hWndParent = GetParent(hWndOver)         ' If there is a parent get more info:         If hWndParent <> 0 Then            ' Get ID of window:            wID = GetWindowWord(hWndOver, GWW_ID)            Print "Window ID Number: &H"; Hex(wID)            Print "Parent Window Handle: &H"; Hex(hWndParent)            ' Get the text of the Parent window:            r = GetWindowText(hWndParent, sParentWindowText, 100)            Print "Parent Window Text: " & Left(sParentWindowText, r)            ' Get the class name of the parent window:            r = GetClassName(hWndParent, sParentClassName, 100)            Print "Parent Window Class Name: "; Left(sParentClassName, r)         Else            ' Update fields when no parent:            Print "Window ID Number: N/A"            Print "Parent Window Handle: N/A"            Print "Parent Window Text : N/A"            Print "Parent Window Class Name: N/A"         End If         ' Get window instance:         hInstance = GetWindowWord(hWndOver, GWW_HINSTANCE)         ' Get module file name:         r = GetModuleFileName(hInstance, sModuleFileName, 100)         Print "Module: "; Left(sModuleFileName, r)      End If   End Sub
  6. Save the project files.
  7. Run the program, and move the mouse over different windows. You will see the field values change as you move the mouse over different windows.
REFERENCES
  1. Microsoft Windows Software Development Kit (SDK)
  2. Microsoft Visual Basic for Windows SDK Help file
ClassName FindWindow
Properties

Article ID: 112649 - Last Review: 07/01/2004 16:27:57 - Revision: 1.2

  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 16-bit Enterprise Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • kbhowto kbui kbwndw KB112649
Feedback