Hi
feststellen ob eine Anwendung ansich läuft, geht z.B. 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 GetWindowThreadProcessId Lib "user32" _
(ByVal hWnd As Long, lpdwProcessId As Long) As Long
Const GW_HWNDFIRST = 0
Const GW_HWNDLAST = 1
Const GW_HWNDNEXT = 2
Const GW_HWNDPREV = 3
Const GW_OWNER = 4
Const GW_CHILD = 5
Const GW_MAX = 5
Const GWL_STYLE = (-16)
Const WS_VISIBLE = &H10000000
Const WS_BORDER = &H800000
Public Function GetWindowList(oLSV As ListView, Optional bVisible As Boolean = _
True)
' oLSV - Ein bbeliebiges ListView das die Informationen afnehmen soll
' bVisible - Wenn True werden nur sichtbare Windows angezeigt, ansonsten alles
Dim hWnd As Long
Dim sTitle As String
Dim lTaskID As Long
Dim lStyle As Long
Dim oListItem As ListItem
With oLSV.ColumnHeaders
.Clear
.Add , "hWnd", "hWnd"
.Add , "TaskID", "TaskID"
.Add , "Visible", "Visible"
.Add , "Title", "Title"
End With
oLSV.ListItems.Clear
oLSV.View = lvwReport
' Erstes WindowHandle holen
hWnd = GetWindow(oLSV.Parent.hWnd, GW_HWNDFIRST)
Do
' Handle auswerten und im ListView eintragen
lStyle = GetWindowLong(hWnd, GWL_STYLE)
lStyle = lStyle And (WS_VISIBLE Or WS_BORDER)
sTitle = GetWindowTitle(hWnd)
lTaskID = GetWindowTaskID(hWnd)
If (lStyle = (WS_VISIBLE Or WS_BORDER)) = bVisible Then
' in das ListView eintragen
Set oListItem = oLSV.ListItems.Add(, , hWnd)
With oListItem
.SubItems(1) = lTaskID
.SubItems(2) = lStyle = (WS_VISIBLE Or WS_BORDER)
.SubItems(3) = sTitle
End With
End If
' Nächses Handle
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
Loop Until hWnd = 0
End Function
Private Function GetWindowTitle(ByVal hWnd As Long) As String
' Ermittelt den Namen eines Windows anhand des Window Handle
Dim lResult As Long
Dim sTemp As String
lResult = GetWindowTextLength(hWnd) + 1
sTemp = Space(lResult)
lResult = GetWindowText(hWnd, sTemp, lResult)
GetWindowTitle = Left(sTemp, Len(sTemp) - 1)
End Function
Private Function GetWindowTaskID(ByVal hWnd As Long) As Long
' Ermittelt die TaskID eines Windows anhand des Window Handle
Dim lResult As Long
Dim lTemp As Long
lResult = GetWindowThreadProcessId(hWnd, lTemp)
GetWindowTaskID = lTemp
End Function
Public Function IsWordRunning(oForm As Form) As Boolean
' Gibt True zurück, wenn Word geladen ist
Dim hWnd As Long
Dim sTitle As String
Dim lStyle As Long
IsWordRunning= False ' Ich alter Pessimist ;-)
' Erstes WindowHandle holen
hWnd = GetWindow(oForm.hWnd, GW_HWNDFIRST)
Do
' Handle auswerten und im ListView eintragen
lStyle = GetWindowLong(hWnd, GWL_STYLE)
lStyle = lStyle And (WS_VISIBLE Or WS_BORDER)
If (lStyle = (WS_VISIBLE Or WS_BORDER)) = True Then
sTitle = GetWindowTitle(hWnd)
If InStr(1, sTitle, "Microsoft Word", vbTextCompare) > 0 Then
IsWordRunning= True
Exit Do
End If
End If
' Nächses Handle
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
Loop Until hWnd = 0
End Function Allerdings kann man so nicht feststellen, welche Datei gerade von dieser Anwendung geöffnet ist.
Gruß
Tolwyn |