Drag a custom control

Source: Internet
Author: User
// Add an event
This. mouseup + = new system. Windows. Forms. mouseeventhandler (this. dragend );
This. mousemove + = new system. Windows. Forms. mouseeventhandler (this. dragmove );
This. mousedown + = new system. Windows. Forms. mouseeventhandler (this. dragbegin );

Private Point P1; // The Screen coordinate of the cursor before dragging
Private point P2; // The Screen coordinate of the mouse after dragging
Private bool _ isdrag; // whether it is being dragged
Private bool _ allowdragmove;
/// <Summary>
/// Whether to allow dragging.
/// </Summary>
Public bool allowdragmove
{
Get
{
Return this. _ allowdragmove;
}
Set
{
This. _ allowdragmove = value;
}
}

Private void dragbegin (Object sender, system. Windows. Forms. mouseeventargs E)
{
If (E. Button = mousebuttons. Left & this. allowdragmove)
{
This. _ isdrag = true;

// Record the coordinates before dragging
This. p1 = This. pointtoscreen (new point (E. X, E. y ));
}
}

Private void dragend (Object sender, system. Windows. Forms. mouseeventargs E)
{
If (E. Button = mousebuttons. Left & this. _ isdrag)
{
This. _ isdrag = false;

// Record the coordinates After dragging
This. P2 = This. pointtoscreen (new point (E. X, E. y ));
// Calculates the displacement.
Int x = p2.x-p1.x;
Int y = p2.y-p1.y;

// Move the widget location
This. Left + = X;
This. Top + = y;
}
}

Private void dragmove (Object sender, system. Windows. Forms. mouseeventargs E)
{
If (this. _ isdrag)
{
// Record the coordinates After dragging
This. P2 = This. pointtoscreen (new point (E. X, E. y ));
// Calculates the displacement.
Int x = p2.x-p1.x;
Int y = p2.y-p1.y;

// Move the widget location
This. Left + = X;
This. Top + = y;

// Use the dragged vertex as the new vertex before dragging
P1 = P2;
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.