Rubrik: | VB-Versionen: VB2005 - VB2015 | 01.03.16 |
Mit nachfolgender Funktion lässt sich ein Fenster auf dem primären Anzeige-Bildschirm zentriert oder fest an den vier Eckpunkten des Bildschirms ausrichten. Optional kann hierbei noch ein Abstand zum Bildschirmrand festgelegt werden.
''' <summary>
''' Aufzählung möglicher Ausrichten
''' </summary>
Public Enum LocationPos
CenterScreen = 0
TopLeft = 1
TopRight = 2
BottomLeft = 3
BottomRight = 4
End Enum
''' <summary>
''' Richtet ein Fenster an der angegeben Bildschirmposition aus
''' </summary>
''' <param name="Form">Fenster (Form)</param>
''' <param name="Location">Position, an der das Fenster angezeigt werden soll</param>
''' <param name="xDist">Optional. x-Abstand vom Bildschirmrand</param>
''' <param name="yDist">Optional. y-Abstand vom Bildschirmrand</param>
Public Sub SetFormLocation(ByVal Form As Windows.Forms.Form, ByVal Location As LocationPos, _
Optional ByVal xDist As Integer = 0, Optional ByVal yDist As Integer = 0)
' verfügbarer Arbeitsbereicht des Primären Bildschirms
Dim workArea As Rectangle = Screen.PrimaryScreen.WorkingArea
Dim x As Integer = 0
Dim y As Integer = 0
' Position (Koordinaten) ermitteln
Select Case Location
Case LocationPos.CenterScreen
' zentrieren
x = workArea.Left + ((workArea.Right - workArea.Left - Form.Width) / 2)
y = workArea.Top + ((workArea.Bottom - workArea.Top - Form.Height) / 2)
Case LocationPos.TopLeft
' oben links
x = xDist
y = yDist
Case LocationPos.TopRight
' oben rechts
x = workArea.Right - Form.Width - xDist
y = yDist
Case LocationPos.BottomLeft
' unten links
x = xDist
y = workArea.Bottom - Form.Height - yDist
Case LocationPos.BottomRight
' unten rechts
x = workArea.Right - Form.Width - xDist
y = workArea.Bottom - Form.Height - yDist
End Select
' Fenster neu positionieren
Form.Location = New Point(x, y)
End Sub
Beispielaufruf:
' Fenster zentriert anzeigen
SetFormLocation(Me, LocationPos.CenterScreen)
' Fenster unten rechts anzeigen
' mit zusätzlichem Abstand von 10 Pixel
SetFormLocation(Me, LocationPos.BottomRight, 10, 10)