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

Zavrnitev odgovornosti za umaknjeno vsebino KB

Ta članek govori o izdelkih, za katere Microsoft ne ponuja več podpore. Zato je ta članek na voljo »tak, kot je«, in ne bo več posodobljen.

Summary

You could use SPY.EXE, which comes with Microsoft Visual C/C++, to get information such as a window's class name. However, this article shows by example how you can create your own Visual Basic application that does the same thing; displays a window's class name along with several other attributes. For example, you might need a window's class name for use in a function in the Microsoft Windows Application Programming Interface (Windows API).

More Information

This example uses several functions from the Windows API to get information about the window the cursor is currently over. First, the routine calls GetCursorPos to get the current position of the cursor. Then it calls WindowFromPoint to get the handle to the window the cursor is currently over. Then it calls several other functions in the Windows API to get specific 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 by calling 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 similar to those produced by SPY.EXE, and makes use of conditional compilation so that the program can be run in either 16-bit or 32-bit editions of Visual Basic.


NOTE: Although the code below works in Visual Basic 5.0, it not necessary to use the conditional compilation (#If Win16, #ElseIf Win32) in Visual Basic 5.0 since it requires 32-bit. As a result, you can shorten 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 similar to 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
Lastnosti

ID članka: 112649 – Zadnji pregled: 1. jul. 2004 – Revizija: 1

Povratne informacije