Subs im Modul
' === Fenster exakt mittig auf Monitor der Referenzform setzen ===
Public Sub CenterFormOnSameMonitor(ByVal frmToCenter As Form, ByVal _
referenceForm As Form)
Dim hMonitor As Long
Dim mi As MONITORINFO
Dim hwndTarget As Long
hMonitor = MonitorFromWindow(referenceForm.hWnd, MONITOR_DEFAULTTONEAREST)
mi.cbSize = Len(mi)
If GetMonitorInfo(hMonitor, mi) <> 0 Then
Dim monitorLeft As Long
Dim monitorTop As Long
Dim monitorWidth As Long
Dim monitorHeight As Long
Dim winWidth As Long
Dim winHeight As Long
Dim newLeft As Long
Dim newTop As Long
' Monitor-Infos holen (in echten Pixeln)
monitorLeft = mi.rcWork.Left
monitorTop = mi.rcWork.Top
monitorWidth = mi.rcWork.Right - mi.rcWork.Left
monitorHeight = mi.rcWork.Bottom - mi.rcWork.Top
' Fenstergröße (in Pixeln!)
winWidth = frmToCenter.Width \ Screen.TwipsPerPixelX
winHeight = frmToCenter.Height \ Screen.TwipsPerPixelY
' Neue Position berechnen (zentriert im Arbeitsbereich des Monitors)
newLeft = monitorLeft + ((monitorWidth - winWidth) \ 2)
newTop = monitorTop + ((monitorHeight - winHeight) \ 2)
' Fensterposition setzen (in Pixeln!)
hwndTarget = frmToCenter.hWnd
Call SetWindowPos(hwndTarget, 0, newLeft, newTop, 0, 0, SWP_NOSIZE Or _
SWP_NOZORDER Or SWP_NOACTIVATE)
End If
End Sub
' === Form relativ zur Referenzform positionieren ===
Public Sub PositionFormRelativeToForm(ByVal frmToMove As Form, ByVal _
referenceForm As Form, ByVal position As String, Optional ByVal spacing As _
Long = 10)
Dim refLeft As Long
Dim refTop As Long
Dim refWidth As Long
Dim refHeight As Long
Dim winWidth As Long
Dim winHeight As Long
Dim newLeft As Long
Dim newTop As Long
' Konvertiere Referenz-Form-Koordinaten in Pixel
refLeft = referenceForm.Left \ Screen.TwipsPerPixelX
refTop = referenceForm.Top \ Screen.TwipsPerPixelY
refWidth = referenceForm.Width \ Screen.TwipsPerPixelX
refHeight = referenceForm.Height \ Screen.TwipsPerPixelY
' Ziel-Form-Größe in Pixel
winWidth = frmToMove.Width \ Screen.TwipsPerPixelX
winHeight = frmToMove.Height \ Screen.TwipsPerPixelY
Select Case LCase(position)
Case "right"
newLeft = refLeft + refWidth + spacing
newTop = refTop + (refHeight - winHeight) \ 2
Case "left"
newLeft = refLeft - winWidth - spacing
newTop = refTop + (refHeight - winHeight) \ 2
Case "below"
newLeft = refLeft + (refWidth - winWidth) \ 2
newTop = refTop + refHeight + spacing
Case "above"
newLeft = refLeft + (refWidth - winWidth) \ 2
newTop = refTop - winHeight - spacing
Case "center"
newLeft = refLeft + (refWidth - winWidth) \ 2
newTop = refTop + (refHeight - winHeight) \ 2
Case Else
' Default: center
newLeft = refLeft + (refWidth - winWidth) \ 2
newTop = refTop + (refHeight - winHeight) \ 2
End Select
' Position mit SetWindowPos setzen (in echten Pixeln)
Call SetWindowPos(frmToMove.hWnd, 0, newLeft, newTop, 0, 0, SWP_NOSIZE Or _
SWP_NOZORDER Or SWP_NOACTIVATE)
End Sub |