Mettre au premier plan l'application CATIA V5

StagiaireEnMousse

XLDnaute Nouveau
Bonjour bonjour, encore dans la galère :(

Je voudrais mettre au-dessus de toutes mes fenêtres (au premier plan donc) ma fenêtre active du logiciel CATIA V5.

J'essaie d'utiliser la fonction "BringWindowToTop"; en vain. En fait, pour définir le hwnd, on doit mettre le nom de la fenêtre en question. Mais j'ai beau mettre le nom que je vois :

CATIA V5 - [PRODUCT1]

La fenêtre ne vient pas au premier plan.

Voici le code que j'ai fait en test, que j'ai mis dans un bouton

Code:
Option Explicit
 
Private Declare Function BringWindowToTop Lib "user32" _
    (ByVal Hwnd As Long) As Long
 
Private Declare Function FindWindow Lib "user32" Alias _
    "FindWindowA" (ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long
 
Private Declare Function ShowWindow Lib "user32" _
    (ByVal Hwnd As Long, ByVal nCmdShow As Long) As Long
 

Sub ApplicationPremierPlan()

    Dim Hwnd As Long
 
    'Récupère le Handle d'une fenêtre
    'Le Handle est un nombre entier unique généré par Windows afin d'identifier les fenêtres.
    Hwnd = FindWindow(vbNullString, "CATIA V5 - [PRODUCT1]")
 
    'Si l'application est déjà ouverte
    If Hwnd > 0 Then
        'Ramène l'application au premier plan
        BringWindowToTop Hwnd
        'Affiche en mode "Normal"
        ShowWindow Hwnd, 1
    End If
End Sub


J'ai pris le code du site ci-contre : FAQ Excel

D'avance merci pour vos suggestions.

Une stagiaire en mousse

EDIT : Le problème survient lorsque l'on a ouvert une PART ou un PRODUCT.
Le HWND de l'application est seulement "CATIA V5", mais de l'application + produit/part, le HWND m'est impossible à trouver.
La concaténation des nom ne marche pas.
 
Dernière édition:

gilbert_RGI

XLDnaute Barbatruc
Re : Mettre au premier plan l'application CATIA V5

Bonjour,

je n'ai pas "CATIA V5" mais j'ai fais le test avec "WinRAR"

'Remarque importante:
'La procédure ne doit pas être déclenchée depuis l'éditeur de macros /!\


Option Explicit

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

Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long


Sub ApplicationPremierPlan()
Dim hwnd As Long, TiTre As String
Dim Sh As Worksheet, rep As Boolean
Set Sh = Sheets(1)
TiTre = "Downloads - WinRAR" '"WinRAR" ne fonctionne pas
'Récupère le Handle d'une fenêtre (WinRar dans cet exemple).
'Le Handle est un nombre entier unique généré par Windows afin d'identifier les fenêtres.
If Sh.[a1].Value <> 0 Then
hwnd = Sh.[a1].Value
Else
'Titre correspond au titre de la fenêtre.
hwnd = FindWindow(vbNullString, TiTre)
End If
Sh.[a1].Value = hwnd
'Si WinRAR est déjà ouvert
If hwnd > 0 Then
'Ramène WinRAR au premier plan
rep = BringWindowToTop(hwnd)
'rep = SetForegroundWindow(hwnd)
If rep = 0 Then
Sh.[a1].Value = 0
Shell "C:\Program Files\WinRAR\WinRAR.exe", vbNormalFocus
'MsgBox rep
Else
'Affiche en mode "Normal"
ShowWindow hwnd, 1
End If
Else
'Sinon, ouvre WinRAR
Shell "C:\Program Files\WinRAR\WinRAR.exe", vbNormalFocus
End If
Set Sh = Nothing
End Sub
 
Dernière édition:

david84

XLDnaute Barbatruc
Re : Mettre au premier plan l'application CATIA V5

david84



Pas trouvé de changement ?

peux-tu en dire plus merci
En fait cela dépend du but de la manœuvre : si l'on parle d'activer et de donner le focus à telle fenêtre SetForegroundWindow le fait. Si l'on veut que la fenêtre soit physiquement au 1er plan c'est possible mais il faut faire autrement mais le fait qu'elle ne soit pas au 1er plan ne signifie pas que ce ne soit pas la fenêtre active...et vice-versa, donc quel est le but final ?
A+
 
Dernière édition:

david84

XLDnaute Barbatruc
Re : Mettre au premier plan l'application CATIA V5

Une possibilité parmi d'autres :
Code:
Option Explicit

#If Win64 Then
  Declare PtrSafe Function FindWindow Lib "user32" Alias _
  "FindWindowA" (ByVal lpClassName As String, _
  ByVal lpWindowName As String) As Long
  
  Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  
  Declare PtrSafe Sub ClientToScreen Lib "user32" _
  (ByVal hwnd As Long, lpPoint As POINT)
  
  Declare PtrSafe Sub mouse_event Lib "user32" _
  (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, _
  ByVal cButtons As Long, ByVal dwExtraInfo As Long)
                                        
  Declare PtrSafe Function SetCursorPos Lib "user32" _
  (ByVal x As Long, ByVal y As Long) As Long
#Else
  Declare Function FindWindow Lib "user32" Alias _
  "FindWindowA" (ByVal lpClassName As String, _
  ByVal lpWindowName As String) As Long
  
  Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  
  Declare PtrSafe Sub ClientToScreen Lib "user32" _
  (ByVal hwnd As Long, lpPoint As POINT)
  
  Declare Sub mouse_event Lib "user32" _
  (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, _
  ByVal cButtons As Long, ByVal dwExtraInfo As Long)
                                        
  Declare Function SetCursorPos Lib "user32" _
  (ByVal x As Long, ByVal y As Long) As Long
#End If


Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
 
Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type


Type POINT
  x As Long
  y As Long
End Type


Sub ApplicationPremierPlan()
Dim wWinRar As Long, wNotepad As Long, hwnd As Long
Dim Zone As RECT, Pos As POINT


'on ouvre WinRar
wWinRar = Shell("C:\Program Files\WinRAR\WinRAR.exe", vbNormalFocus)
Sleep 200


'on ouvre Notepad=>Notepad est au 1er plan
wNotepad = Shell("C:\Windows\System32\notepad.exe", vbNormalFocus)
Sleep 200


'on recherche le handle de la fenêtre WinRar affichée au second plan
hwnd = FindWindow("WinRarWindow", vbNullString)
Sleep 200


Pos.x = Zone.Left
Pos.y = Zone.Top


ClientToScreen hwnd, Pos


SetCursorPos Pos.x, Pos.y
mouse_event MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, Pos.x, Pos.y, 0, 0


End Sub
A+
 

david84

XLDnaute Barbatruc
Re : Mettre au premier plan l'application CATIA V5

Une autre plus simple avec SetForegroundWindow :
Code:
Option Explicit

#If Win64 Then
  Declare PtrSafe Function FindWindow Lib "user32" Alias _
  "FindWindowA" (ByVal lpClassName As String, _
  ByVal lpWindowName As String) As Long
  
  Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  
  Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

#Else
  Declare Function FindWindow Lib "user32" Alias _
  "FindWindowA" (ByVal lpClassName As String, _
  ByVal lpWindowName As String) As Long
  
  Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  
  Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
#End If


Sub ApplicationPremierPlan()
Dim wWinRar As Long, wNotepad As Long, hwnd As Long
'on ouvre WinRar
wWinRar = Shell("C:\Program Files\WinRAR\WinRAR.exe", vbNormalFocus)
Sleep 200

'on ouvre Notepad=>Notepad est au 1er plan
wNotepad = Shell("C:\Windows\System32\notepad.exe", vbNormalFocus)
Sleep 200

'on recherche le handle de la fenêtre WinRar affichée au second plan
hwnd = FindWindow("WinRarWindow", vbNullString)
Sleep 200
SetForegroundWindow hwnd 'on replace WinRar au 1er plan
End Sub

L'utilisation des Sleep peut être facultatif (à tester avec puis éventuellement sans).
A+
 

Discussions similaires