With MonitorInformation
Me.StartUpPosition = 0
Me.Left = PixelsToPointsX(.rcMonitor.Left) + (PixelsToPointsX(.rcMonitor.Right - .rcMonitor.Left) - Me.Width) / 2
Me.Top = (PixelsToPointsY(.rcMonitor.Bottom - .rcMonitor.Top) - Me.Height) / 2
End With
Option Explicit
Private Type RECT: Left As Long: Top As Long: Right As Long: Bottom As Long: End Type
Private Type MONITORINFO: cbSize As Long: rcMonitor As RECT: rcWork As RECT: dwFlags As Long: End Type
Private Declare Function GetMonitorInfo Lib "user32.dll" Alias "GetMonitorInfoA" (ByVal hMonitor As Long, ByRef lpmi As MONITORINFO) As Long
Private Declare Function MonitorFromPoint Lib "user32.dll" (ByVal x As Long, ByVal y As Long, ByVal dwFlags As Long) As Long
Sub affiche_a_Gauche() 'centre ecran gauche ou centre de l'ecran par defaut si un seul ecran
Dim MI_G As MONITORINFO, Mx, My, MG, gauche#, LtoP, fois&, PtPx#
Mx = MonitorFromPoint(100, 100, &H2) '&H2=MONITOR_DEFAULTTONEAREST
My = MonitorFromPoint(-100, -100, &H2) '&H2=MONITOR_DEFAULTTONEAREST
MG = Mx: fois = 1
If Val(My) <> Val(Mx) Then MG = My: fois = -1
PtPx = 0.75 ' que saint DUDU me pardonne j'en ai ras le bol de ce ptpx :):):)
MI_G.cbSize = Len(MI_G): GetMonitorInfo MG, MI_G
gauche = (((MI_G.rcMonitor.Right * PtPx) - (UserForm1.Width)) / 2) * fois
LtoP = (((MI_G.rcMonitor.Bottom * PtPx) - (UserForm1.Height)) / 2) * fois
With UserForm1
.Show 0
.Move gauche, LtoP
End With
End Sub
on s'en fou que excel soit a gauche ou a droiteAlors en effet, ça place le UserForm sur l'écran de gauche quand Excel est sur l'écran de droite.
Sub test()
'coté:=0 =ecran gauche
'coté:=1 =ecran droite si 2 ecran ou ecran si un seul ecran
afficheUserform UserForm1, coté:=1
End Sub
Sub afficheUserform(usf, Optional coté As Long = 0) 'centre ecran gauche ou centre de l'ecran par defaut si un seul ecran
Dim MI_G As MONITORINFO, Mx, My, MG, gauche#, LtoP, fois&, PtPx#
Mx = MonitorFromPoint(100, 100, &H2) '&H2=MONITOR_DEFAULTTONEAREST
My = MonitorFromPoint(-100, -100, &H2) '&H2=MONITOR_DEFAULTTONEAREST
MG = Mx: fois = 1
If Val(My) <> Val(Mx) Then MG = My: fois = -1
If coté = 1 Then fois = Abs(fois)
PtPx = 0.75 ' que saint DUDU me pardonne j'en ai ras le bol de ce ptpx :):):)
MI_G.cbSize = Len(MI_G): GetMonitorInfo MG, MI_G
gauche = (((MI_G.rcMonitor.Right * 0.75) - (UserForm1.Width)) / 2) * fois
LtoP = (((MI_G.rcMonitor.Bottom * 0.75) - (UserForm1.Height)) / 2) * fois
With usf
.Show 0
.Move gauche, LtoP
End With
End Sub
Option Explicit
Private Type RECT: Left As Long: Top As Long: Right As Long: Bottom As Long: End Type
Private Type MONITORINFO: cbSize As Long: rcMonitor As RECT: rcWork As RECT: dwFlags As Long: End Type
Private Declare Function GetMonitorInfo Lib "user32.dll" Alias "GetMonitorInfoA" (ByVal hMonitor As Long, ByRef lpmi As MONITORINFO) As Long
Private Declare Function MonitorFromPoint Lib "user32.dll" (ByVal x As Long, ByVal y As Long, ByVal dwFlags As Long) As Long
Private Declare Function MonitorFromWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal dwFlags As Long) As Long
Sub test()
'coté:=0 =l'ecran gauche si 2 ecran ou ecran si un seul ecran
'coté:=1 =l'ecran droite si 2 ecran ou ecran si un seul ecran
'coté:=2 ='lécran ou se trouve la fenêtre excel
'coté omis =gauche par defaut ou ecran si un seul ecran
afficheUserform UserForm1, coté:=2
End Sub
Sub afficheUserform(usf, Optional coté As Long = 0) 'centre ecran gauche ou centre de l'ecran par defaut si un seul ecran
Dim MI_G As MONITORINFO, Mx, My, MG, MExcel, gauche#, LtoP, fois&, PtPx#
Mx = MonitorFromPoint(100, 100, &H2) '&H2=MONITOR_DEFAULTTONEAREST 'l'ecran a 100 de droite du point (0,0)
My = MonitorFromPoint(-100, -100, &H2) '&H2=MONITOR_DEFAULTTONEAREST'l'ecran a -100 de gauche du point (0,0)
MExcel = MonitorFromWindow(Application.hWnd, &H1) ' l'ecran ou se trouve l'application excel
MG = Mx: fois = 1
If Val(My) <> Val(Mx) Then MG = My: fois = -1
If coté = 1 Then fois = Abs(fois)
If coté = 2 Then MG = MExcel: fois = 1
PtPx = 0.75 ' que saint DUDU me pardonne j'en ai ras le bol de ce ptpx :):):)
MI_G.cbSize = Len(MI_G): GetMonitorInfo MG, MI_G
gauche = (((MI_G.rcMonitor.Right * 0.75) - (UserForm1.Width)) / 2) * fois
LtoP = (((MI_G.rcMonitor.Bottom * 0.75) - (UserForm1.Height)) / 2) * fois
With usf
.Show 0
.Move gauche, LtoP
End With
End Sub