Results 1 to 5 of 5

Thread: Keep a form inside the desktop area

  1. #1

    Thread Starter
    Hyperactive Member Steve Stunning's Avatar
    Join Date
    Jul 1999
    Location
    Fairfax, Virginia
    Posts
    314

    Question

    How can I allow a user to move a form but prevent the form from being placed outside the screen area.

    I do not want my application to be out of sight of they need to move it around a little.

    Anyone have an idea?

    Thanks for reading this.
    Steve Stunning

  2. #2
    Guest
    Try this:

    Code:
    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
    
    Public Sub FormMove(frm As Form)
        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
                    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

  3. #3

    Thread Starter
    Hyperactive Member Steve Stunning's Avatar
    Join Date
    Jul 1999
    Location
    Fairfax, Virginia
    Posts
    314
    I gave the code a try but had not luck. Must be too early in the morning for me. Can you tell me how to make this work properly?

    I have a project due today for review so I am limited on time to mess around with it.

    Thanks for your assistance.
    Steve Stunning

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    Option Explicit
       
    'you could do it this way / add a timer control
    
    Private Sub Form_Load()
        Timer1.Interval = 1
    End Sub
    
    Private Sub Timer1_Timer()
      If ((Form1.Left) < 0) Then Form1.Left = 0
      If ((Form1.Top) < 0) Then Form1.Top = 0
      If ((Form1.Left) > Screen.Width - Form1.Width) _
          Then Form1.Left = Screen.Width - Form1.Width
      If ((Form1.Top) > Screen.Height - Form1.Height) _
          Then Form1.Top = Screen.Height - Form1.Height
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5

    Thread Starter
    Hyperactive Member Steve Stunning's Avatar
    Join Date
    Jul 1999
    Location
    Fairfax, Virginia
    Posts
    314
    That is a good option... I can use if necessary..

    But do not think they will move the form around enough to do that. LOL I am hoping to have a way to do it thru the system itself.
    Steve Stunning

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width