Herbers Excel-Forum - das Archiv
API - Fenstertitel aufzählen
Betrifft: API - Fenstertitel aufzählen
von: Hans T.
Geschrieben am: 06.10.2003 17:13:50
Hallo SpezialistInnen
Wie kann ich die Fenstertitel aller offenen Fenster (nicht nur Excel, sondern auch alle andern laufenden Programme) in eine Collection oder ein Array füllen?
Kennt jemand einen entsprechenden API-Trick? Hinweise werden heiss begehrt.
Viele Grüsse
Hans T.
Betrifft: AW: API - Fenstertitel aufzählen
von: Nepumuk
Geschrieben am: 06.10.2003 18:28:04
Hallo Hans,
meinst du so?
Option Explicit
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal wIndx As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd 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
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2
Const GWL_STYLE = (-16)
Const WS_VISIBLE = &H10000000
Const WS_BORDER = &H800000
Public Sub GetWindowList()
Dim hWnd As Long, sTitle As String, lStyle As Long, Task_name() As String
Dim count As Integer, index As Integer, gefunden As Boolean
hWnd = FindWindow(ByVal 0&, ByVal 0&)
hWnd = GetWindow(hWnd, GW_HWNDFIRST)
Do
gefunden = False
lStyle = GetWindowLong(hWnd, GWL_STYLE)
lStyle = lStyle And (WS_VISIBLE Or WS_BORDER)
sTitle = GetWindowTitle(hWnd)
If (lStyle = (WS_VISIBLE Or WS_BORDER)) = True Then
If Trim(sTitle) <> "" Then
For index = 1 To count
If Task_name(index) = sTitle Then
gefunden = True
Exit For
End If
Next index
If Not gefunden Then
count = count + 1
ReDim Preserve Task_name(1 To count)
Task_name(count) = sTitle
End If
End If
End If
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
Loop Until hWnd = 0
For index = 1 To count
MsgBox Task_name(index)
Next index
End Sub
Private Function GetWindowTitle(ByVal hWnd As Long) As String
Dim lResult As Long, sTemp As String
lResult = GetWindowTextLength(hWnd) + 1
sTemp = Space(lResult)
lResult = GetWindowText(hWnd, sTemp, lResult)
GetWindowTitle = Left(sTemp, Len(sTemp) - 1)
End Function
Code eingefügt mit: Excel Code Jeanie
Gruß
Nepumuk
Betrifft: AW: API - Fenstertitel aufzählen
von: Hans T.
Geschrieben am: 06.10.2003 19:27:57
Hallo Nepomuk
Das ist ja perfekt! Der Code funktioniert bestens, und ich kann ihn an meine Bedürfnisse anpassen. Riesigen Dank.
Gruss
Hans T.
Betrifft: AW: API - Fenstertitel aufzählen
von: Reinhard
Geschrieben am: 06.10.2003 18:37:10
Hi Hans,
folgender Code listet hwnd und Fenstertitel auf. Bitte keine Rückgragen zu der API, hab das selber aus dem Netz gefischt und keine Ahnung von APIs :-)
Apropos keine Ahnung, weisst du zufällig, oder jmd. anders warum er auch die leeren Titel (sTitle) auflistet obwohl da eindeutig "If Not sTitle = "" Then" steht? Und sTitle ist leer bzw hat in den Fällen die Länge Null????
Gruß
Reinhard
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName _ As Long, ByVal lpWindowName As Long) As Long
Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2
Const GWL_STYLE = (-16)
Const WS_VISIBLE = &H10000000
Const WS_BORDER = &H800000
Public Sub GetWindowList()
Dim hWnd As Long, sTitle As String, lStyle As Long, Task_name() As String
Dim count As Integer, index As Integer, gefunden As Boolean
Dim r As Long
r = 1
hWnd = FindWindow(ByVal 0&, ByVal 0&)
hWnd = GetWindow(hWnd, GW_HWNDFIRST)
Do
lStyle = GetWindowLong(hWnd, GWL_STYLE)
lStyle = lStyle And (WS_VISIBLE Or WS_BORDER)
sTitle = GetWindowTitle(hWnd)
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
'If Trim(sTitle) <> "" Then
If Not sTitle = "" Then
Cells(r, 1) = hWnd
Cells(r, 2) = sTitle
Cells(r, 3) = Len(sTitle)
End If
r = r + 1
Loop Until hWnd = 0
End Sub
Private Function GetWindowTitle(ByVal hWnd As Long) As String
Dim lResult As Long, sTemp As String
lResult = GetWindowTextLength(hWnd) + 1
sTemp = Space(lResult)
lResult = GetWindowText(hWnd, sTemp, lResult)
GetWindowTitle = Left(sTemp, Len(sTemp) - 1)
End Function
Betrifft: AW: API - Fenstertitel aufzählen
von: Hans T.
Geschrieben am: 06.10.2003 19:30:06
Hallo Reinhard
Der Ansatz ist gut, aber der Code ist unvollständig. Nepomuk hat den kompletten Code geliefert.
Trotzdem vielen Dank für die Recherche.
Gruss
Hans T.