VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Code makes it so that a form cannot go off the screen.

by Matthew Gates (16 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Originally Published: Wed 1st November 2000
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Code makes it so that a form cannot go off the screen.

API Declarations


Public Declare Sub ReleaseCapture Lib "user32" ()
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Rate Code makes it so that a form cannot go off the screen.



'Makes it where your form cant go off the screen
    ReleaseCapture
    SendMessage frm.hwnd, &H112, &HF012, 0
    frm.Refresh
    If frm.Left < 0 Then 'left side of screen
        frm.Left = 0
            If frm.Top < 0 Then
                frm.Top = 0
            ElseIf frm.Top > Screen.Height - frm.Height Then
                frm.Top = Screen.Height - frm.Height
            End If
    ElseIf frm.Top < 0 Then 'top of screen
        frm.Top = 0
            If frm.Left < 0 Then
                frm.Top = 0
            ElseIf frm.Left > Screen.Width - frm.Width Then
              Timer1.Enabled = True
                frm.Left = Screen.Width - frm.Width
            End If
    ElseIf frm.Left > Screen.Width - frm.Width Then 'right of screen
       frm.Left = Screen.Width - frm.Width
            If frm.Top < 0 Then
                frm.Top = 0
            ElseIf frm.Top > Screen.Height - frm.Height Then
                frm.Top = Screen.Height - frm.Height
            End If
    ElseIf frm.Top > Screen.Height - frm.Height Then 'bottom of screen
        frm.Top = Screen.Height - frm.Height
            If frm.Left < 0 Then
                frm.Top = 0
            ElseIf frm.Left > Screen.Width - frm.Width Then
                frm.Left = Screen.Width - frm.Width
            End If
    End If
End Sub

Download this snippet    Add to My Saved Code

Code makes it so that a form cannot go off the screen. Comments

No comments have been posted about Code makes it so that a form cannot go off the screen.. Why not be the first to post a comment about Code makes it so that a form cannot go off the screen..

Post your comment

Subject:
Message:
0/1000 characters