Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionOption Explicit Declare Function GetForegroundWindow Lib "user32" () As Long Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long Declare Function GetDesktopWindow Lib "user32" () As Long Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long Sub ChekForegroundWindow(ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) 'quelle fenetre est au premier plan ? If Not GetForegroundWindow = 0 Then 'si ce n'est pas le bureau windows on arrete tout KillTimer Application.hWnd, 0 End End If End Sub Sub MaMacro() 'on active le bureau windows SetForegroundWindow GetDesktopWindow 'on lance le timer qui verifira toute les secondes si c'est le bureau qui est au "premier plan" SetTimer Application.hWnd, 0, 1000, AddressOf ChekForegroundWindow Do DoEvents 'pour la demo uniquement 'avec cette methode les select et activate sont sans effet, le thread du bureau resta au premier plan Sheets(1).Select Sheets(1).Activate [A1].Value = [A1].Value + 1 Loop End Sub
Option Explicit Declare Function GetForegroundWindow Lib "user32" () As Long Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long Declare Function GetDesktopWindow Lib "user32" () As Long Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long Public Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long Dim ArretDemande As Boolean Sub ChekForegroundWindow(ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) 'quelle fenetre est au premier plan ? If Not GetForegroundWindow = 0 Then 'si ce n'est pas le bureau windows on arrete tout ArretDemande = True DoEvents End If End Sub Sub MaMacro() 'on active le bureau windows ArretDemande = False SetForegroundWindow GetDesktopWindow 'on lance le timer qui verifira toute les secondes si c'est le bureau qui est au "premier plan" SetTimer Application.hWnd, 0, 1000, AddressOf ChekForegroundWindow Do While ArretDemande = False DoEvents 'pour la demo uniquement [A1].Value = [A1].Value + 1 Loop KillTimer Application.hWnd, 0 End Sub
Option Explicit Declare Function GetForegroundWindow Lib "user32" () As Long Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long Declare Function GetDesktopWindow Lib "user32" () As Long Sub MaMacro() 'on active le bureau windows SetForegroundWindow GetDesktopWindow Do While GetForegroundWindow = 0 DoEvents 'pour la demo uniquement [A1].Value = [A1].Value + 1 Loop End Sub
Option Explicit Declare Function GetForegroundWindow Lib "user32" () As Long Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long Declare Function GetDesktopWindow Lib "user32" () As Long Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long Public Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer Dim ArretDemande As Boolean, KeyTest As Boolean Sub ChekForegroundWindow(ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) 'quelle fenetre est au premier plan ? If Not GetForegroundWindow = 0 Then 'si ce n'est pas le bureau windows on arrete tout ArretDemande = True DoEvents End If End Sub Sub MaMacro() 'on active le bureau windows ArretDemande = False SetForegroundWindow GetDesktopWindow 'on lance le timer qui verifira toute les secondes si c'est le bureau qui est au "premier plan" SetTimer Application.hWnd, 0, 1000, AddressOf ChekForegroundWindow Do Until ArretDemande True Or KeyStat True DoEvents 'pour la demo uniquement [A1].Value = [A1].Value + 1 Loop KillTimer Application.hWnd, 0 End Sub Private Function KeyStat() As Boolean Dim i As Long KeyStat = False 'remise a false avant vérif. For i = 1 To 256 If Not GetAsyncKeyState(i) = 0 Then KeyStat = True Exit For End If Next End Function