104 lines
3.2 KiB
VB.net
104 lines
3.2 KiB
VB.net
Imports System
|
|
Imports System.Drawing
|
|
Imports System.Windows.Forms
|
|
|
|
Public Class ScreenEx
|
|
Public Const DEFAULT_WINDOW_HEIGHT = 480
|
|
Public Const DEFAULT_WINDOW_WIDTH = 640
|
|
|
|
Public Shared Function GetLocationWithinScreen(pLocation As Point) As Point?
|
|
For Each screen As Screen In Screen.AllScreens
|
|
If screen.Bounds.Contains(pLocation) Then
|
|
Return New Point(pLocation.X - screen.Bounds.Left, pLocation.Y - screen.Bounds.Top)
|
|
End If
|
|
Next
|
|
|
|
Return Nothing
|
|
End Function
|
|
|
|
Public Shared Sub RestoreFormPosition(pForm As Form, pPosition As Point)
|
|
Dim oLocationWithinScreen As Point? = GetLocationWithinScreen(pPosition)
|
|
|
|
If oLocationWithinScreen Is Nothing Then
|
|
Dim oPrimaryScreen = Screen.PrimaryScreen
|
|
pForm.StartPosition = FormStartPosition.CenterScreen
|
|
Else
|
|
pForm.StartPosition = FormStartPosition.Manual
|
|
pForm.Location = pPosition
|
|
End If
|
|
End Sub
|
|
|
|
Public Shared Sub RestoreFormState(pForm As Form, pFormState As FormWindowState)
|
|
If pFormState = FormWindowState.Maximized Then
|
|
pForm.WindowState = FormWindowState.Normal
|
|
pForm.WindowState = FormWindowState.Maximized
|
|
ElseIf pFormState = FormWindowState.Minimized Then
|
|
pForm.WindowState = FormWindowState.Normal
|
|
pForm.WindowState = FormWindowState.Minimized
|
|
Else
|
|
pForm.WindowState = FormWindowState.Normal
|
|
End If
|
|
End Sub
|
|
|
|
Public Shared Sub RestoreFormState(pForm As Form, pFormState As String)
|
|
Dim oFormState As FormWindowState
|
|
If Not [Enum].TryParse(pFormState, oFormState) Then
|
|
oFormState = FormWindowState.Normal
|
|
End If
|
|
|
|
RestoreFormState(pForm, oFormState)
|
|
End Sub
|
|
|
|
Public Shared Sub RestoreWindowSize(pForm As Form, pFormSize As Size)
|
|
Dim oFormSize As Size
|
|
If pFormSize.Height < 1 Or pFormSize.Width < 1 Or pFormSize.IsEmpty Then
|
|
oFormSize = New Size(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT)
|
|
Else
|
|
oFormSize = pFormSize
|
|
End If
|
|
|
|
pForm.Size = oFormSize
|
|
End Sub
|
|
|
|
''' <summary>
|
|
''' Checks if a point is Visible on any screen
|
|
''' </summary>
|
|
Public Shared Function IsVisibleOnAnyScreen(Location As Point) As Boolean
|
|
Try
|
|
Dim oRect As New Rectangle(Location, New Size(0, 0))
|
|
|
|
For Each oScreen In Screen.AllScreens
|
|
If oScreen.WorkingArea.IntersectsWith(oRect) Then
|
|
Return True
|
|
End If
|
|
Next
|
|
|
|
Return False
|
|
Catch ex As Exception
|
|
Return False
|
|
End Try
|
|
End Function
|
|
|
|
''' <summary>
|
|
''' Checks if Size is not negative
|
|
''' </summary>
|
|
Public Shared Function SizeIsVisible(Size As Size) As Boolean
|
|
If Size.Width >= 0 And Size.Height >= 0 Then
|
|
Return True
|
|
End If
|
|
|
|
Return False
|
|
End Function
|
|
|
|
''' <summary>
|
|
''' Checks if Location is not negative
|
|
''' </summary>
|
|
Public Shared Function LocationIsVisible(Location As Point) As Boolean
|
|
If Location.X >= 0 And Location.Y >= 0 Then
|
|
Return True
|
|
End If
|
|
|
|
Return False
|
|
End Function
|
|
End Class
|