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
| Option Compare Database
Option Explicit
' === Fonction de centrage du formulaire ===
Private Type Position
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
' Pour obtenir les coordonnées de la fenêtre parent
Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
' Pour obtenir les coordonnées de notre formulaire
Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, _
lpRect As Position) As Long
' Pour obtenir les coordonnées de l'écran
Public Declare Function GetDesktopWindow Lib "user32" () As Long
' Pour positionner et dimensionner la fenêtre
Public Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, _
ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, _
ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Public Sub Positionner(frm As Form)
Dim FParent As Position ' Fenêtre Parent
Dim Fenetre As Position
Dim Largeur As Integer
Dim Hauteur As Integer
Dim LParent As Integer ' Largeur Fenêtre Parent sendobject
Dim HParent As Integer ' Hauteur Fenêtre Parent
Dim PParent As Long ' Posion parent
On Error GoTo Erreur
' Trouver les coordonnées de mon formulaire à centrer.
PParent = GetParent(frm.hwnd)
' Obtenir les coordonnées de mon formulaire et celles de son parent.
Call GetWindowRect(frm.hwnd, Fenetre)
' Si le parent est la fenêtre Access, pas de soustracion
If PParent <> Application.hWndAccessApp Then
Call GetWindowRect(PParent, FParent)
Else
' les coordonnées du Desktop
Call GetWindowRect(GetDesktopWindow(), FParent)
End If
' Calcul de la largeur et de la hauteur du parent
With FParent
LParent = .Right - .Left
HParent = .Bottom - .Top
End With
' Calcul de la largeur et de la hauteur de mon formulaire
With Fenetre
Largeur = .Right - .Left
Hauteur = .Bottom - .Top
.Left = (LParent - Largeur) \ 2
.Top = (HParent - Hauteur) \ 2
End With
' Centrer mon formulaire
Call MoveWindow(frm.hwnd, Fenetre.Left, Fenetre.Top, Largeur, Hauteur, bRepaint:=True)
Exit Sub
Erreur:
MsgBox "Erreur: " & Err.Number & vbCrLf & Err.Description
End Sub
' === Fin de la fonction de centrage === |
Partager