There are most comments in the program, you are welcome to correct me!
Code
[Copy to Clipboard]
CODE:
1using System;
2using System.Drawing;
3using System.Windows.Forms;
4
5namespace yoker.formutils
6{
7/**////<summary>
8///<para> Description: Form Drag class, the method provided by this class to implement any control on the form to assist in dragging forms </para>
9///<para> author:yoker.wu</para>
///<para> original address:[url]http://yoker.cnblogs.com[/url]</para>
///</summary>
public class Dragformclass
13 {
private static bool Ismousedown = FALSE;
private static point mouseoffset;
private static Form _form;
The public Dragformclass () {}
18
/**////<summary>
20///Add drag events on the form
///</summary>
///<param name= "Control" > Controls Object </param>
public static void Bindcontrol (Control control)
24 {
25//If the control is empty
if (control = = null)
27 {
return;
29}
_form = control. Findform ();
31//Add mouse Drag form move event
control. MouseMove + = new MouseEventHandler (control_mousemove);
control. MouseDown + = new MouseEventHandler (Control_mousedown);
control. MouseUp + = new MouseEventHandler (control_mouseup);
35}
/**////<summary>
37///When the mouse is pressed, save the mouse position relative to the form
///</summary>
///<param name= "Sender" ></param>
///<param name= "E" ></param>
private static void Control_mousedown (object sender, MouseEventArgs e)
42 {
(E.button = = MouseButtons.Left)
44 {
control = sender as control;
int offsetX =-e.x;
int offsetY =-e.y;
48//Determines whether a form or control improves the position of the mouse relative to the form
if (!) ( Control is System.Windows.Forms.Form))
50 {
Wuyi OffsetX = Offsetx-control. Left;
OffsetY = Offsety-control. Top;
53}
54//Determine if the form has a title bar to improve the position of the mouse relative to the form
if (_form. FormBorderStyle!= Formborderstyle.none)
56 {
OffsetX = Offsetx-systeminformation.framebordersize.width;
OffsetY = offsety-systeminformation.framebordersize.height-systeminformation.captionheight;
59}
mouseoffset = new Point (OffsetX, OffsetY);
Ismousedown = true;
62}
63}
/**////<summary>
65///To change the position of the form when moving the mouse
///</summary>
///<param name= "Sender" ></param>
///<param name= "E" ></param>
private static void Control_mousemove (object sender, MouseEventArgs e)
70 {
if (ismousedown)
72 {
point mouse = control.mouseposition;
Mouse. Offset (Mouseoffset.x, MOUSEOFFSET.Y);
_form. Location = mouse;
76}
77}
/**////<summary>
79///When the mouse is released, reset the event
///</summary>
Bayi///<param name= "Sender" ></param>
///<param name= "E" ></param>
-private static void Control_mouseup (object sender, MouseEventArgs e)
84 {
if (E.button = = MouseButtons.Left)
86 {
Ismousedown = false;
88}
89}
90}
91}
92