OVH Cloud OVH Cloud

[VBA] - Tester si une application externe est active

4 réponses
Avatar
Chewi
Bonjour à tous,

Je suis actuellement occupé de tester un code pour voir si une application
externe est active ou non.
Si elle est ouverte, je souhaite qu'elle apparaisse au 1er plan.
Si elle ne l'est pas, qu'elle s'ouvre et se mette aussi au 1er plan.

1) Pour l'instant, mon application s'ouvre mais ne se met pas au 1er plan
2) Je trouve ce code très lent

Si vous pouviez m'aider à avancer, Merci d'avance

Voici le code:


Déclarations:

Private Declare Function BringWindowToTop Lib "user32" _
(ByVal HWnd As Long) As Long

Private Declare Function ShowWindow Lib "user32" _
(ByVal HWnd As Long, ByVal nCmdShow As Long) As Long

Private Const SW_SHOWNORMAL = 1


Code:

Sub ProcessusActifs()
Dim svc As Object
Dim sQuery As String
Dim oproc
Dim Appli As New Application
Dim winShell As New ShellWindows
Dim x As Long

Set svc = GetObject("winmgmts:root\cimv2")
sQuery = "select * from win32_process"

For Each oproc In svc.execQuery(sQuery)
Debug.Print oproc.Name

On Error Resume Next
For Each Appli In winShell
If Appli.Name = "Testeur" Then
x = Appli.HWnd
Exit For
End If
Next Appli
Next

If x = 0 Then x = Shell("C:\Program Files\MonApplication\Testeur.exe")
BringWindowToTop x
ShowWindow x, SW_SHOWNORMAL
Set svc = Nothing
End Sub


Remarque:
La référence "Microsoft Internet Controls" nécessite d'être activée.

4 réponses

Avatar
Oliv'
*Chewi que je salue a écrit *:
Bonjour à tous,

Je suis actuellement occupé de tester un code pour voir si une
application externe est active ou non.
Si elle est ouverte, je souhaite qu'elle apparaisse au 1er plan.
Si elle ne l'est pas, qu'elle s'ouvre et se mette aussi au 1er plan.

1) Pour l'instant, mon application s'ouvre mais ne se met pas au 1er
plan 2) Je trouve ce code très lent

Si vous pouviez m'aider à avancer, Merci d'avance

Voici le code:
Essaye avec ce code là en rempalcant toto par le nom de titre de ton appli


'===================Úns un module standard
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassname As String, ByVal lpWindowName As String) As Long
Declare Function SetForegroundWindow Lib "user32" _
(ByVal hwnd As Long) As Long
Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long


' ShowWindow() Commands
Public Const SW_HIDE = 0
Public Const SW_SHOWNORMAL = 1
Public Const SW_NORMAL = 1
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_MAXIMIZE = 3
Public Const SW_SHOWNOACTIVATE = 4
Public Const SW_SHOW = 5
Public Const SW_MINIMIZE = 6
Public Const SW_SHOWMINNOACTIVE = 7
Public Const SW_SHOWNA = 8
Public Const SW_RESTORE = 9
Public Const SW_SHOWDEFAULT = 10
Public Const SW_MAX = 10



Sub ActiveMailWord()
Dim hwnd As Long
toto = "Message sans titre"
hwnd = FindWindow(vbNullString, toto)
' Hwnd = FindWindow("OpusApp", vbNullString)

If hwnd = 0 Then Exit Sub
SetForegroundWindow hwnd
ShowWindow hwnd, SW_SHOWMAXIMIZED
End Sub


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Have a nice day
Oliv'
Outlook : http://faq.outlook.free.fr/
les archives : http://groups.google.com/group/microsoft.public.fr.outlook
Dernière chance http://www.outlookcode.com/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Avatar
Chewi
Bonjour,

Je viens de tester ton code, cela est impeccable.

Une des 2 conditions est donc réalisée (mettre au 1er plan si ouverte) et je
t'en remercie.

Il faut maintenant arriver à poser les conditions (si ouverte... ce code. si
fermée, activer (connu aussi)).

Peux-tu me dire la condition à poser?

If... ouverte Then
ton code

Else ... Shell...

End If

Déjà un grand merci


"Chewi" wrote in message
news:%
Bonjour à tous,

Je suis actuellement occupé de tester un code pour voir si une application
externe est active ou non.
Si elle est ouverte, je souhaite qu'elle apparaisse au 1er plan.
Si elle ne l'est pas, qu'elle s'ouvre et se mette aussi au 1er plan.

1) Pour l'instant, mon application s'ouvre mais ne se met pas au 1er plan
2) Je trouve ce code très lent

Si vous pouviez m'aider à avancer, Merci d'avance

Voici le code:


Déclarations:

Private Declare Function BringWindowToTop Lib "user32" _
(ByVal HWnd As Long) As Long

Private Declare Function ShowWindow Lib "user32" _
(ByVal HWnd As Long, ByVal nCmdShow As Long) As Long

Private Const SW_SHOWNORMAL = 1


Code:

Sub ProcessusActifs()
Dim svc As Object
Dim sQuery As String
Dim oproc
Dim Appli As New Application
Dim winShell As New ShellWindows
Dim x As Long

Set svc = GetObject("winmgmts:rootcimv2")
sQuery = "select * from win32_process"

For Each oproc In svc.execQuery(sQuery)
Debug.Print oproc.Name

On Error Resume Next
For Each Appli In winShell
If Appli.Name = "Testeur" Then
x = Appli.HWnd
Exit For
End If
Next Appli
Next

If x = 0 Then x = Shell("C:Program FilesMonApplicationTesteur.exe")
BringWindowToTop x
ShowWindow x, SW_SHOWNORMAL
Set svc = Nothing
End Sub


Remarque:
La référence "Microsoft Internet Controls" nécessite d'être activée.



Avatar
Oliv'
*Chewi que je salue a écrit *:
Bonjour,

Je viens de tester ton code, cela est impeccable.

Une des 2 conditions est donc réalisée (mettre au 1er plan si
ouverte) et je t'en remercie.

Il faut maintenant arriver à poser les conditions (si ouverte... ce
code. si fermée, activer (connu aussi)).

Peux-tu me dire la condition à poser?

If... ouverte Then
ton code

Else ... Shell...

End If

Déjà un grand merci



Remplace If hwnd = 0 Then Exit Sub
par
If hwnd = 0 Then
'je lance le programme
else
SetForegroundWindow hwnd
ShowWindow hwnd, SW_SHOWMAXIMIZED
End if
End Sub

j'ai pas testé mais ca doit être cela

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Have a nice day
Oliv'
Outlook : http://faq.outlook.free.fr/
les archives : http://groups.google.com/group/microsoft.public.fr.outlook
Dernière chance http://www.outlookcode.com/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Avatar
Chewi
OK, j'y suis arrivé!

Merci beaucoup!

"Oliv'" wrote in message
news:e%

*Chewi que je salue a écrit *:
Bonjour,

Je viens de tester ton code, cela est impeccable.

Une des 2 conditions est donc réalisée (mettre au 1er plan si
ouverte) et je t'en remercie.

Il faut maintenant arriver à poser les conditions (si ouverte... ce
code. si fermée, activer (connu aussi)).

Peux-tu me dire la condition à poser?

If... ouverte Then
ton code

Else ... Shell...

End If

Déjà un grand merci



Remplace If hwnd = 0 Then Exit Sub
par
If hwnd = 0 Then
'je lance le programme
else
SetForegroundWindow hwnd
ShowWindow hwnd, SW_SHOWMAXIMIZED
End if
End Sub

j'ai pas testé mais ca doit être cela

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Have a nice day
Oliv'
Outlook : http://faq.outlook.free.fr/
les archives : http://groups.google.com/group/microsoft.public.fr.outlook
Dernière chance http://www.outlookcode.com/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~