First, the indication of the cursor
When the cursor moves onto the control to be dragged or zoom, the corresponding action arrow should be displayed. Define an enumerated variable to identify the corresponding state.
ENUM ENUMMOUSEPOINTPSITION
MouseSizenone = 0 '
MouseSizeright = 1 'stretching the right box
MouseSizeleft = 2 'stretch left box
MouseSizeBottom = 3 'stretches under the border
MouseSizetop = 4 'stretches on the border
MouseSizetopleft = 5 'stretching left corner
MouseSizToPright = 6 'stretches the right corner
MouseSizebottomLester = 7 'stretched the lower left corner
MouseSizebottomright = 8 'stretches the right corner
Mousedrag = 9 'mouse drag
END ENUM
DIM M_MOUSEPOINTPSITION As ENUMMOUSEPOINTPSITION
Read the cursor position of the input control in the MouseMove event, and determine the state of the cursor with the following function.
m_mousepositionposition = mousepointPosition (Sender.size, e)
Press the corresponding state to display the cursor shape
SELECT CASE M_MOUSEPOINTPSITION
Case enummouseposition.mousesizenone
Me.cursor = cursors.Arrow 'arrow
Case enummouseposition.mousedrag
Me.cursor = cursors.sizeall 'four directions
Case enummouseposition.mousesizebottom
Me.cursor = Cursors.sizens' North North
Case enummouseposition.mousesizetop
Me.cursor = Cursors.sizens' North North
Case enummouseposition.mousesizeleft
Me.cursor = curs.sizewewe
Case enummouseposition.mousesizeright
Me.cursor = curs.sizewewe
Case enummouseposition.mousesizebottomleaseFT
Me.cursor = cursors.siznesw 'Northeast to South West
Case enummouseposition.mousesizebottomright
Me.cursor = cursors.sizenwse 'southeast to northwest
Case enummouseposition.mousesizeetopleft
Me.cursor = cursors.sizenwse 'southeast to northwest
Case enummouseposition.mousesizetopright
Me.cursor = cursors.siznesw 'Northeast to South West
End SELECT
If the cursor leaves the control, the MouseMove event will not respond, so the identity and display of the cursor status are completed with the MouseEleave event.
Private Sub MyMouseLeave (Byval E AS ISTEM.EventArgs)
m_mousepositionposition = enummouseposition.mousesizenoneMe.cursor = Cursors.Arrow
End Sub
Second, drag
The principle of drag is the location of the mobile control to the current cursor, and keep the height and width of the control. If the starting point of the starting point is P, the current cursor point is e, then the moving distance in the X direction is E.x - P.x, the moving distance in the X direction is E.Y - P.Y.
When the mouse is pressed, it is the beginning of the drag, in the mousedown event, the record cursor begins to drag points.
P = New Point (E.x, E.Y)
In the mousemove event, check if you continue to press the left mouse button, if it is the starting point position of the control
If E.BUTTON = MouseButtons.Left Then
Sender.location = new point (sender.Left E.x - p.x, sender.top E.Y - P.Y)
END IF
Third, zoom
The principle and drag of zoom is similar. If the bottom side is the bottom of the cursor, it changes the height of the control without changing the start of the control.
Similarly, if the bottom side, when the mouse is pressed, it is the beginning of the zoom, in the mousedown event, the record cursor begins to drag the point.
P = New Point (E.x, E.Y)
In the mousemove event, check if the left mouse button continues, if it is the height of the control
If E.BUTTON = MouseButtons.Left Then
Sender.size = new size (sender.width, sender.Height E.Y - P1.Y)
P1 = New Point (E.x, E.Y) 'Recording the current point of the cursor drag
END IF
Some differences here and the drag are that the height change can cause changes in the value of the cursor position, so reusing the position of the cursor start point in the P1. All scaling involved in the bottom of the control and the right side should be processed, while the left and top edges are not available.
Drag in all other directions can be pushed.
Fourth, delegation
All operations, a total of three functions, MyMouseDown, MyMousemove, MyMouseLeave.
After dynamically create a control, you need to delegate the corresponding event to these three functions. If you create a button,
Dim Button As New Button
Controls.add (Button)
AddHandler Button.Mousedown, Addressof mymousedown
AddHandler Button.Mousemove, Addressof MyMousemove
AddHandler Button.MouseLeave, Addressof MyMouseLeave
V. Source
Create a form, place a button, click the button to create a dynamically established a button that can drag and zoom. The entire source code is as follows:
Public Class Form1
Inherits System.Windows.Forms.form
ENUM ENUMMOUSEPOINTPSITION
MouseSizenone = 0 '
MouseSizeright = 1 'stretching the right box
MouseSizeleft = 2 'stretch left box
MouseSizeBottom = 3 'stretches under the border
MouseSizetop = 4 'stretches on the border
MouseSizetopleft = 5 'Stretch left upper corner MOUSESIZETOPRIGHT = 6' stretched right upper corner
MouseSizebottomLester = 7 'stretched the lower left corner
MouseSizebottomright = 8 'stretches the right corner
Mousedrag = 9 'mouse drag
END ENUM
DIM M_MOUSEPOINTPSITION As ENUMMOUSEPOINTPSITION
DIM P, P1 As Point
Private sub mymousedown (Byval e as system.windows.forms.mouseeventargs)
P = New Point (E.x, E.Y) 'Record the cursor to start dragging points
P1 = New Point (E.x, E.Y)
End Sub
Private Sub MyMouseLeave (Byval E AS ISTEM.EventArgs)
M_MousePositionPosition = enummouseposition.mousesizenone
Me.cursor = cursors.Arrow
End Sub
Private sub mymouseemove (Byval e as system.windows.forms.mouseeventargs)
If E.BUTTON = MouseButtons.Left Then
SELECT CASE M_MOUSEPOINTPSITION
Case enummouseposition.mousedrag
Sender.location = new point (sender.Left E.x - p.x, sender.top E.Y - P.Y)
Case enummouseposition.mousesizebottom
Sender.size = new size (sender.width, sender.Height E.Y - P1.Y)
P1 = New Point (E.x, E.Y) 'Recording the current point of the cursor drag
Case enummouseposition.mousesizebottomright
Sender.size = new size (Sender.width E.X - P1.X, Sender.Height E.Y - P1.Y)
P1 = New Point (E.x, E.Y)
Case enummouseposition.mousesizeright
Sender.size = new size (Sender.width E.x - p1.x, sender.height)
P1 = New Point (E.x, E.Y)
Case enummouseposition.mousesizetop
Sender.location = new point (sender.Left, Sender.top (E.Y - P.Y))
Sender.size = new size (sender.width, sender.Height - (E.Y - P.Y))
Case enummouseposition.mousesizeleft
Sender.location = new point (sender.Left E.x - p.x, sender.top)
Sender.size = new size (Sender.width - (E.X - P.X), Sender.height) Case EnumMousePointPosition.MouseSizeBottomLeft
Sender.location = new point (sender.Left E.x - p.x, sender.top)
Sender.size = new size (Sender.width - (E.X - P.X), Sender.Height E.Y - P1.Y)
P1 = New Point (E.x, E.Y)
Case enummouseposition.mousesizetopright
Sender.location = new point (sender.Left, Sender.top (E.Y - P.Y))
Sender.size = new size (Sender.width (E.X - P1.X), Sender.height - (E.Y - P.Y))
P1 = New Point (E.x, E.Y)
Case enummouseposition.mousesizeetopleft
Sender.location = new point (sender.Left E.x - p.x, sender.top (E.Y - P.Y))
Sender.size = new size (Sender.width - (E.X - P.X), Sender.height - (E.Y - P.Y))
End SELECT
Else
M_MousePointPosition = MousePointPosition (Sender.size, e) 'Judging the position status of the cursor
SELECT CASE M_MOUSEPOINTPSITION 'Change Cursor
Case enummouseposition.mousesizenone
Me.cursor = cursors.Arrow 'arrow
Case enummouseposition.mousedrag
Me.cursor = cursors.sizeall 'four directions
Case enummouseposition.mousesizebottom
Me.cursor = Cursors.sizens' North North
Case enummouseposition.mousesizetop
Me.cursor = Cursors.sizens' North North
Case enummouseposition.mousesizeleft
Me.cursor = curs.sizewewe
Case enummouseposition.mousesizeright
Me.cursor = curs.sizewewe
Case enummouseposition.mousesizebottomleaseFT
Me.cursor = cursors.siznesw 'Northeast to South West
Case enummouseposition.mousesizebottomright
Me.cursor = cursors.sizenwse 'southeast to northwest
Case enummouseposition.mousesizeetopleft
Me.cursor = cursors.sizenwse 'southeast to northwest
Case enummouseposition.mousesizetoprightme.cursor = cursors.sizENESW 'Northeast to South West
End SELECT
END IF
End Sub
Private function mouseposition (byval size as size, byval e as system.windows.forms.mouseeventargs) as enummousepositionPosition
Const band = 10
If E.x> = -1 * band and e.x <= size.width and E.Y> = -1 * band and e.y <= size.height dam
IF E.X IF E.Y Return enummouseposition.mousesizetopleft Elseif E.Y> -1 * Band Size.height THEN Return enummouseposition.mousesizebottomleaseFT Else Return enummouseposition.mousesizeleft END IF Elseif E.x> -1 * Band Size.width Then IF E.Y Return enummouseposition.mousesizetopright Elseif E.Y> -1 * Band Size.height THEN Return enummouseposition.mousesizebottomright Else Return enummouseposition.mousesizeright END IF Else IF E.Y Return enummouseposition.mousesizetop Elseif E.Y> -1 * Band Size.height THEN Return enummouseposition.mousesizebottom Else Return EnummousePosition.Mousedrag END IF END IF Else Return enummouseposition.mousesizenone END IF END FUNCTION Private sub button1_click (byvale as system.object, byval e as system.eventargs) Handles Button1.click Dim Button As New Button Controls.add (Button) AddHandler Button.Mousedown, Addressof mymousedown AddHandler Button.Mousemove, Addressof MyMousemove AddHandler Button.MouseLeave, Addressof MyMouseLeave End Sub END CLASS