Colle tout ce qui suit dans un module standard et exécute la procédure test()
Option Explicit
' Module Name: ModFindWindowLike
' (c) 2005 Wayne Phillips (
http://www.everythingaccess.com)
' Written 02/06/2005
Private Declare Function EnumWindows Lib "user32" _
(ByVal lpEnumFunc As Long, _
ByVal lParam 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 GetWindowModuleFileName Lib "user32" _
Alias "GetWindowModuleFileNameA" (ByVal hwnd As Long, _
ByVal Filename As String, ByVal cch As Long) As Long
Private Declare Function ShowWindow& Lib "user32" _
(ByVal hwnd As Long, ByVal ncmdshow As Long)
Public Const SW_MINIMIZE = 6
'Custom structure for passing in the parameters in/out of the hook enumeration function
'Could use global variables instead, but this is nicer.
Private Type FindWindowParameters
strTitle As String 'INPUT
hwnd As Long 'OUTPUT
End Type
'-------------------------------------------------------------
Public Function FnFindWindowLike(strWindowTitle As String) As Long
'We'll pass a custom structure in as the parameter to store our result...
Dim Parameters As FindWindowParameters
Parameters.strTitle = strWindowTitle ' Input parameter
Call EnumWindows(AddressOf EnumWindowProc, VarPtr(Parameters))
FnFindWindowLike = Parameters.hwnd
End Function
'-------------------------------------------------------------
Private Function EnumWindowProc(ByVal hwnd As Long, _
lParam As FindWindowParameters) As Long
Static x
Dim strWindowTitle As String
strWindowTitle = Space(260)
Call GetWindowText(hwnd, strWindowTitle, 260)
strWindowTitle = TrimNull(strWindowTitle) ' Remove extra null terminator
'If strWindowTitle <> "" Then
If strWindowTitle Like "*.pdf*" Or _
strWindowTitle Like "*acrobat" Then
Debug.Print strWindowTitle
lParam.hwnd = hwnd 'Store the result for later.
EnumWindowProc = 0 'This will stop enumerating more windows
End If
EnumWindowProc = 1
End Function
'-------------------------------------------------------------
Private Function TrimNull(strNullTerminatedString As String)
Dim lngPos As Long
'Remove unnecessary null terminator
lngPos = InStr(strNullTerminatedString, Chr$(0))
If lngPos Then
TrimNull = Left$(strNullTerminatedString, lngPos - 1)
Else
TrimNull = strNullTerminatedString
End If
End Function
'-------------------------------------------------------------
Sub test()
Dim MyAppHWnd As Long, File As String
MyAppHWnd = FnFindWindowLike("*Acrobat")
If MyAppHWnd > 0 Then
File = String$(1024, 0)
GetWindowModuleFileName MyAppHWnd, File, Len(File)
ShowWindow MyAppHWnd, SW_MINIMIZE
End If
End Sub
'-------------------------------------------------------------
--
MichD
---------------------------------------------------------------