ycsim
Nov 9th, 1999, 02:24 PM
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).]
Serge
Nov 9th, 1999, 07:38 PM
1. If you have the hWnd of the window, then use GetWindowText API.
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:
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:
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
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819 (http://www.icq.com/51055819)