vb@rchiv
VB Classic
VB.NET
ADO.NET
VBA
C#
Top-Preis! AP-Access-Tools-CD Volume 1  
 vb@rchiv Quick-Search: Suche startenErweiterte Suche starten   Impressum  | Datenschutz  | vb@rchiv CD Vol.6  | Shop Copyright ©2000-2025
 
zurück

 Sie sind aktuell nicht angemeldet.Funktionen: Einloggen  |  Neu registrieren  |  Suchen

Fortgeschrittene Programmierung
Re: StartUpPosition 1 - Fenstermitte 
Autor: B.Th.
Datum: 02.09.25 18:16

Erweiterung der Public Sub PositionFormRelativeToForm im Modul. Dabei werden nun die Monitorgrenzen beachtet, damit Form2 nicht aus dem Bildschirm ragt.
' === Form relativ zur Referenzform positionieren, dabei Monitorgrenzen 
' beachten ===
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
 
    Dim hMonitor As Long
    Dim mi As MONITORINFO
    Dim monitorLeft As Long
    Dim monitorTop As Long
    Dim monitorWidth As Long
    Dim monitorHeight As Long
 
    ' Monitor bestimmen
    hMonitor = MonitorFromWindow(referenceForm.hWnd, MONITOR_DEFAULTTONEAREST)
    mi.cbSize = Len(mi)
    If GetMonitorInfo(hMonitor, mi) <> 0 Then
        monitorLeft = mi.rcWork.Left
        monitorTop = mi.rcWork.Top
        monitorWidth = mi.rcWork.Right - mi.rcWork.Left
        monitorHeight = mi.rcWork.Bottom - mi.rcWork.Top
    Else
        ' Falls fehlgeschlagen, gesamten Bildschirm nutzen
        monitorLeft = 0
        monitorTop = 0
        monitorWidth = Screen.Width \ Screen.TwipsPerPixelX
        monitorHeight = Screen.Height \ Screen.TwipsPerPixelY
    End If
 
    ' 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
 
    ' Grundposition berechnen
    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
            newLeft = refLeft + (refWidth - winWidth) \ 2
            newTop = refTop + (refHeight - winHeight) \ 2
    End Select
 
    ' === Begrenzen auf Monitor-Arbeitsbereich ===
    If newLeft < monitorLeft Then newLeft = monitorLeft
    If newTop < monitorTop Then newTop = monitorTop
    If newLeft + winWidth > monitorLeft + monitorWidth Then
        newLeft = monitorLeft + monitorWidth - winWidth
    End If
    If newTop + winHeight > monitorTop + monitorHeight Then
        newTop = monitorTop + monitorHeight - winHeight
    End If
 
    ' Position setzen
    Call SetWindowPos(frmToMove.hWnd, 0, newLeft, newTop, 0, 0, _
                      SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE)
End Sub
alle Nachrichten anzeigenGesamtübersicht  |  Zum Thema  |  Suchen

 ThemaViews  AutorDatum
StartUpPosition 1 - Fenstermitte144B.Th.01.09.25 16:54
Re: StartUpPosition 1 - Fenstermitte55B.Th.01.09.25 17:13
Re: StartUpPosition 1 - Fenstermitte57B.Th.01.09.25 17:15
Re: StartUpPosition 1 - Fenstermitte40B.Th.02.09.25 18:16

Sie sind nicht angemeldet!
Um auf diesen Beitrag zu antworten oder neue Beiträge schreiben zu können, müssen Sie sich zunächst anmelden.

Einloggen  |  Neu registrieren

Funktionen:  Zum Thema  |  GesamtübersichtSuchen 

nach obenzurück
 
   

Copyright ©2000-2025 vb@rchiv Dieter Otter
Alle Rechte vorbehalten.
Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein.

Diese Seiten wurden optimiert für eine Bildschirmauflösung von mind. 1280x1024 Pixel