Results 1 to 2 of 2

Thread: Docking forms

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    69

    Question Docking forms

    I'm using this code which allows me to dock a form. However what i need is a way to when the form is docked to either side of the screen the form will adopt the height of the screen and when i move to either the top or the bottom of the screen it addopts the width. Does anyone know how i would intergrate this?

    VB Code:
    1. 'Form declaration section
    2. Dim Xs As Integer               'Change in X
    3. Dim Ys As Integer               'Change in Y
    4. Dim IsBeingDragged As Boolean   'Flag if the form is being dragged
    5. Dim DockScale As Integer        'Scale by which the form docks itself to screen
    6.  
    7. Private Sub Form_Load()
    8. 'set form docking scale (change it according to your needs)
    9. DockScale = 200
    10. End Sub
    11. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    12. 'set flag to true
    13. IsBeingDragged = True
    14. 'get X Change and Y Change
    15. Xs = x
    16. Ys = y
    17. End Sub
    18. Public Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    19. If IsBeingDragged = True And Button = vbLeftButton Then
    20.     'if the drag flag is true and left mouse button is pressed...
    21.    
    22.     'set Left side  docking
    23.     If Form1.Left + (x - Xs) < DockScale Then
    24.         Form1.Left = 0
    25.         Exit Sub
    26.     End If
    27.     'set Top side docking
    28.     If Form1.Top + (y - Ys) < DockScale Then
    29.         Form1.Top = 0
    30.         Exit Sub
    31.     End If
    32.    
    33.     'set right side docking
    34.     If Form1.Left + (x - Xs) + Form1.Width > (Screen.Width - DockScale) Then
    35.         Form1.Left = Screen.Width - Form1.Width
    36.         Exit Sub
    37.     End If
    38.    
    39.     'set bottom side docking
    40.     If Form1.Top + (y - Ys) + Form1.Height > (Screen.Height - DockScale) Then
    41.         Form1.Top = Screen.Height - Form1.Height
    42.         Exit Sub
    43.     End If
    44.    
    45.     'move the form finally
    46.     Form1.Left = Form1.Left + (x - Xs)
    47.     Form1.Top = Form1.Top + (y - Ys)
    48. End If
    49. End Sub
    50. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    51.     'set drag flag to false
    52.     IsBeingDragged = False
    53. End Sub

    Regards


    Carl

  2. #2
    Hyperactive Member guyvdn's Avatar
    Join Date
    Oct 2002
    Location
    Belgium
    Posts
    336

    Re: Docking forms

    Fun thing to do and I got it working

    Code can be cleaned up maybe but it's 2AM over here so I am going to bed.

    VB Code:
    1. 'Form declaration section
    2. Dim Xs As Integer               'Change in X
    3. Dim Ys As Integer               'Change in Y
    4. Dim myHeight As Integer
    5. Dim myWidth As Integer
    6. Dim IsBeingDragged As Boolean   'Flag if the form is being dragged
    7. Dim DockScale As Integer        'Scale by which the form docks itself to screen
    8. Dim blnDocked As Boolean
    9. Dim lngTaskBarHeight As Long
    10.  
    11. Const ABS_AUTOHIDE = &H1
    12. Const ABS_ONTOP = &H2
    13. Const ABM_GETSTATE = &H4
    14. Const ABM_GETTASKBARPOS = &H5
    15. Private Type RECT
    16.     Left As Long
    17.     Top As Long
    18.     Right As Long
    19.     Bottom As Long
    20. End Type
    21. Private Type APPBARDATA
    22.     cbSize As Long
    23.     hwnd As Long
    24.     uCallbackMessage As Long
    25.     uEdge As Long
    26.     rc As RECT
    27.     lParam As Long '  message specific
    28. End Type
    29. Private Declare Function SHAppBarMessage Lib "shell32.dll" (ByVal dwMessage As Long, pData As APPBARDATA) As Long
    30.  
    31. Private Sub Form_Load()
    32.     'set form docking scale (change it according to your needs)
    33.     DockScale = 200
    34.     myHeight = Me.Height
    35.     myWidth = Me.Width
    36.     blnDocked = False
    37.    
    38.     'Get taskbar height
    39.     Dim ABD As APPBARDATA, Ret As Long
    40.     SHAppBarMessage ABM_GETTASKBARPOS, ABD
    41.     lngTaskBarHeight = ABD.rc.Top * Screen.TwipsPerPixelY
    42. End Sub
    43.  
    44. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    45.     'set flag to true
    46.     IsBeingDragged = True
    47.     'get X Change and Y Change
    48.     Xs = x
    49.     Ys = y
    50. End Sub
    51.  
    52. Public Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    53.  
    54.     If IsBeingDragged = True And Button = vbLeftButton Then
    55.         'if the drag flag is true and left mouse button is pressed...
    56.        
    57.         If blnDocked Then
    58.        
    59.             'Allow undocking
    60.             If ((Form1.Left = 0 And Form1.Height = lngTaskBarHeight) And (Form1.Left + (x - Xs) > DockScale)) Or _
    61.                ((Form1.Top = 0 And Form1.Width = Screen.Width) And (Form1.Top + (y - Ys) > DockScale)) Or _
    62.                ((Form1.Left = Screen.Width - Form1.Width And Form1.Height = lngTaskBarHeight) And (Form1.Left + (x - Xs) + Form1.Width < (Screen.Width - DockScale))) Or _
    63.                ((Form1.Top = lngTaskBarHeight - Form1.Height And Form1.Width = Screen.Width) And (Form1.Top + (y - Ys) + Form1.Height > (lngTaskBarHeight - DockScale))) Then
    64.                 blnDocked = False
    65.                 Me.Height = myHeight
    66.                 Me.Width = myWidth
    67.             End If
    68.            
    69.         Else
    70.        
    71.              'set Left side  docking
    72.              If Form1.Left + (x - Xs) < DockScale Then
    73.                  Form1.Left = 0
    74.                  Form1.Height = lngTaskBarHeight
    75.                  Form1.Top = 0
    76.                  blnDocked = True
    77.                  Exit Sub
    78.              End If
    79.              
    80.              'set Top side docking
    81.              If Form1.Top + (y - Ys) < DockScale Then
    82.                  Form1.Top = 0
    83.                  Form1.Width = Screen.Width
    84.                  Form1.Left = 0
    85.                  blnDocked = True
    86.                  Exit Sub
    87.              End If
    88.              
    89.              'set right side docking
    90.              If Form1.Left + (x - Xs) + Form1.Width > (Screen.Width - DockScale) Then
    91.                  Form1.Left = Screen.Width - Form1.Width
    92.                  Form1.Height = lngTaskBarHeight
    93.                  Form1.Top = 0
    94.                  blnDocked = True
    95.                  Exit Sub
    96.              End If
    97.              
    98.              'set bottom side docking
    99.              If Form1.Top + (y - Ys) + Form1.Height > (lngTaskBarHeight - DockScale) Then
    100.                  Form1.Top = lngTaskBarHeight - Form1.Height
    101.                  Form1.Width = Screen.Width
    102.                  Form1.Left = 0
    103.                  blnDocked = True
    104.                  Exit Sub
    105.              End If
    106.            
    107.              'move the form finally
    108.              Form1.Left = Form1.Left + (x - Xs)
    109.              Form1.Top = Form1.Top + (y - Ys)
    110.         End If
    111.        
    112.     End If
    113.  
    114. End Sub
    115.  
    116. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    117.     'set drag flag to false
    118.     IsBeingDragged = False
    119. End Sub
    To deny our own impulses is to deny the very thing that makes us human

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