1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| Option Explicit
Option Compare Binary
Private Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
Private Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetClipboardData Lib "user32.dll" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags&, ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetWindowA Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wCmd 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 Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Private Const CF_TEXT = 1
Private Const MAXSIZE = 65536
Private Const GMEM_MOVEABLE = &H2
Private Const GMEM_ZEROINIT = &H40
Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)
'------------------------------------------------------------------------------------------------------
Sub Explorateur_Grandes_Icones()
'------------------------------------------------------------------------------------------------------
' Trouve le Handle de l'explorateur:
Dim Hdc As LongPtr, i As Integer
Hdc = FindWindowA("CabinetWClass", vbNullString)
' Modifie la barre d'adresse pour indiquer le répertoire désiré:
Call Hdc_EnvoyerTouches(Hdc, vbKeyControl, vbKeyL)
Sleep 100
Call Hdc_EnvoyerTouches(Hdc, "C:\Users\ott_l\TPS\") ' <- A adapter pour le répertoire désiré.
Call Hdc_EnvoyerTouches(Hdc, vbKeyReturn)
Sleep 1000
' Boucle pour trouver la sélection des fichiers:
For i = 1 To 9
Call Hdc_EnvoyerTouches(Hdc, vbKeyTab)
' Passe en grandes icônes:
Call Hdc_EnvoyerTouches(Hdc, vbKeyControl, vbKeyShift, vbKey1)
Next i
End Sub
'------------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------------------------
Public Sub Hdc_EnvoyerTouches(hWndApp As Variant, ParamArray Combinaison() As Variant)
'------------------------------------------------------------------------------------------------------
' Envoie des touches à une application.
' hWndApp : est soit le numéro Hdc de la fenêtre (ou 0 si fenêtre active), soit son nom.
' Combinaison : touche(s) a envoyer. Ce peut être une chaîne ou une variable vbKey.
'------------------------------------------------------------------------------------------------------
' Exemples : Hdc_EnvoyerTouches Hdc, "Bonjour"
' Hdc_EnvoyerTouches Hdc, vbKeyMultiply
'------------------------------------------------------------------------------------------------------
' Astuce : l'impression écran est impossible avec Sendkeys, utilisez Hdc_EnvoyerTouches 0, vbKeySnapshot
' ou pour n'avoir que le forumaire actif: Hdc_EnvoyerTouches 0, vbKeyMenu, vbKeySnapshot
' Exmple pour la calculatrice : Hdc_EnvoyerTouches "*Calculatrice*", vbKeyMenu, vbKeySnapshot
' puis pour coller dans Excel : Sheets("Feuil1").Paste Range("A1")
'------------------------------------------------------------------------------------------------------
Dim i As Integer, j As Integer, s As String, Etat As Boolean, Maj As Boolean
Dim Hdc As Long
If IsNumeric(hWndApp) = True Then
Hdc = hWndApp
Else
Hdc = TrouverFenetre(CStr(hWndApp))
End If
' Vide la presse-papiers:
ClipBoard_Clear
' Place le focus sur la fenêtre demandée (si son numéro est passé <> 0):
If Hdc <> 0 Then
SetForegroundWindow Hdc
SetFocus Hdc
End If
' Si une chaîne de carractères est passée en argument:
If VarType(Combinaison(0)) <> vbInteger Then
' L'envoie dans le presse-papiers:
If ClipBoard_SetData(CStr(Combinaison(0))) = True Then
' Si cela réussi alors colle avec Ctrl+V:
keybd_event vbKeyControl, 0, 0, 0
keybd_event vbKeyV, 0, 0, 0
keybd_event vbKeyControl, 0, 2, 0
keybd_event vbKeyV, 0, 2, 0
End If
' Si c'est une combinaison numérique qui est passée en argument:
Else
' Active:
For i = LBound(Combinaison()) To UBound(Combinaison())
keybd_event Combinaison(i), 0, 0, 0
Next i
' Relache:
For i = LBound(Combinaison()) To UBound(Combinaison())
keybd_event Combinaison(i), 0, 2, 0
Next i
End If
Sleep 100
DoEvents
End Sub
'---------------------------------------------------------------------------------------
Private Function ClipBoard_SetData(MyString As String) As Boolean
'---------------------------------------------------------------------------------------
' Envoie une chaîne de caractères dans le presse-papiers.
' MyString : Chaîne à envoyer
'---------------------------------------------------------------------------------------
' Sources : 32-bit code by Microsoft: http://msdn.microsoft.com/en-us/library/office/ff192913.aspx
'---------------------------------------------------------------------------------------
Dim hGlobalMemory As LongPtr, lpGlobalMemory As LongPtr
Dim hClipMemory As LongPtr
' Gestion de la taille maximale du message a envoyer au clavier:
If Len(MyString) >= MAXSIZE Then ' "Chaîne trop grande"
Exit Function
End If
' Gestion des chaînes vides:
If MyString = "" Then Exit Function
' Allocate moveable global memory.
hGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1)
' Lock the block to get a far pointer to this memory.
lpGlobalMemory = GlobalLock(hGlobalMemory)
' Copy the string to this global memory.
lpGlobalMemory = lstrcpy(lpGlobalMemory, MyString)
' Unlock the memory.
If GlobalUnlock(hGlobalMemory) <> 0 Then '"Could not unlock memory location. Copy aborted."
GoTo OutOfHere
End If
' Open the Clipboard to copy data to.
If OpenClipboard(0&) = 0 Then ' "Could not open the Clipboard. Copy aborted."
Exit Function
End If
' Clear the Clipboard.
EmptyClipboard
' Copy the data to the Clipboard.
hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)
OutOfHere:
If CloseClipboard() <> 0 Then ClipBoard_SetData = True
End Function
'---------------------------------------------------------------------------------------
Private Function ClipBoard_Clear() As Boolean
'---------------------------------------------------------------------------------------
If OpenClipboard(0) = 0 Then Exit Function
EmptyClipboard
If CloseClipboard() <> 0 Then ClipBoard_Clear = True
End Function
'------------------------------------------------------------------------------------------------------
Public Function TrouverFenetre(StrFenetre As String, Optional ByRef StrTitre As String = "") As Long
'------------------------------------------------------------------------------------------------------
' Retourne le Handle de la fenêtre passé en argument. Utilise l'opérateur Like donc accepte * et ?
' et autres, voir l'aide.
' Retourne 0 si la fenêtre n'est pas trouvée.
'------------------------------------------------------------------------------------------------------
Dim Ret As Long
Dim MyStr As String
' Boucle sur les fenêtres actives:
Ret = FindWindow(ByVal 0&, ByVal 0&)
Do While Ret <> 0
' Cherche le nom de la fenêtre:
MyStr = String(100, Chr$(0))
GetWindowText Ret, MyStr, Len(MyStr)
' Si c'est la fenêtre recherchée alors renvoie l'Hdc:
If Left(MyStr, InStr(1, MyStr, Chr(0)) - 1) Like StrFenetre Then
TrouverFenetre = Ret
StrTitre = Left(MyStr, InStr(1, MyStr, Chr(0)) - 1)
Exit Function
End If
' Cherche la fenêtre suivante:
Ret = GetWindowA(Ret, 2)
Loop
End Function
'------------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------------------------ |
Partager