Results 1 to 2 of 2

Thread: A few questions about windows

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 1999
    Location
    Singapore
    Posts
    116

    Post

    1)How do I get the caption of the specified window given the handle???

    2)How do I get the handles or captions of all Windows that are currently open???

    Thanx

    ------------------
    YC Sim
    Teenage Programmer
    UIN 37903254



    [This message has been edited by ycsim (edited 11-10-1999).]

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    1. If you have the hWnd of the window, then use GetWindowText API.

    Code:
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    
    
    '---------Put this on any event you want
        Dim lRet As Long
        Dim strWinText
    
        strWinText = Space(255)
        lRet = GetWindowText(hWnd, strWinText, Len(strWinText))
    2. You can enumerate windows using EnumWindows API. Add a ListView to the form. Also, add a module to your project.

    Module Code:
    Code:
    Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Public Type udtWin
        hWnd As Long
        Class As String
        WinText As String
    End Type
    Public tWin() As udtWin
    
    
    Public Function EnumWindowProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
        Dim lClass As Long, lWin As Long
        Dim strClass As String, strWinText As String
        Static i As Integer
        
        strWinText = Space(255)
        strClass = Space(255)
        lWin = GetWindowText(hWnd, strWinText, Len(strWinText))
        lClass = GetClassName(hWnd, strClass, Len(strClass))
        If lWin Then strWinText = Left(strWinText, lWin)
        If lClass Then strClass = Left(strClass, lClass)
        
        ReDim Preserve tWin(i)
        tWin(i).hWnd = hWnd
        tWin(i).Class = strClass
        tWin(i).WinText = strWinText
        
        i = i + 1
        EnumWindowProc = 1
    End Function

    Form Code:
    Code:
    Private Sub Form_Load()
        Dim xColHeader As ColumnHeader
        Dim i As Integer
        Dim xItem As ListItem
        
        With ListView1
            .View = lvwReport
            Set xColHeader = .ColumnHeaders.Add(, , "hWnd")
            Set xColHeader = .ColumnHeaders.Add(, , "Class")
            Set xColHeader = .ColumnHeaders.Add(, , "Caption", 4000)
        
            Call EnumWindows(AddressOf EnumWindowProc, 0)
            For i = 0 To UBound(tWin)
                Set xItem = .ListItems.Add(, , tWin(i).hWnd)
                xItem.SubItems(1) = tWin(i).Class
                xItem.SubItems(2) = tWin(i).WinText
            Next
        End With
    End Sub
    NOTE: You must have VB5 or higher.

    Regards,

    ------------------

    Serge

    Software Developer
    [email protected]
    [email protected]
    ICQ#: 51055819


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width