Friday 20 January 2012

Drag Control in Window Form


For this, just need 1 Button.

Public Dragging As Boolean                                                   
Public mousex, mousey As Integer                                             

Private Sub Button1_MouseUp(ByVal sender As System.Object, ByVal e As        System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp                 
        If Dragging Then                                                     
            'end the dragging                                                
            Dragging = False                                                 
            'Me.Capture = False                                              
            Cursor.Clip = Nothing                                            
            Button1.Invalidate()                                             
        End If                                                               
End Sub                                                                      

Private Sub Button1_MouseDown(ByVal sender As System.Object, ByVal e As      System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown               
        If e.Button = MouseButtons.Left Then                                 
            Dragging = True                                                  
            mousex = -e.X                                                    
            mousey = -e.Y                                                    
            Dim clipleft As Integer = Me.PointToClient(MousePosition).X -    Button1.Location.X                                                           
            Dim cliptop As Integer = Me.PointToClient(MousePosition).Y -     Button1.Location.Y                                                           
            Dim clipwidth As Integer = Me.ClientSize.Width - (Button1.Width  
- clipleft)                                                                  
            Dim clipheight As Integer = Me.ClientSize.Height - (Button1.Height
 - cliptop)                                                                   
            Cursor.Clip = Me.RectangleToScreen(New Rectangle(clipleft, cliptop, clipwidth, clipheight))                                                         
            Button1.Invalidate()                                                
        End If                                                                  
End Sub                                                                         

Private Sub Button1_MouseMove(ByVal sender As System.Object, ByVal e As         System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove                  
         If Dragging Then                                                       
            'move control to new position                                       
            Dim MPosition As New Point()                                        
            MPosition = Me.PointToClient(MousePosition)                         
            MPosition.Offset(mousex, mousey)                                    
            'ensure control cannot leave container                              
            Button1.Location = MPosition                                        
        End If                                                                  
End Sub                                                                         


No comments:

Post a Comment