将form从下面的FormBase类继承,拖动form上的任意一点都可移动.并且解决了原方案中如果将formBorderStyle设置成none之后,MouseMove的第一下有个跳动的问题.可用于不规则窗体的制作.
public class FormBase : Form
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Declarations#region Declarations
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private bool drag = false;
private Point start_point = new Point(0, 0);
private bool draggable = true;
private string exclude_list = "";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <SUMMARY>
/// Required designer variable.
/// </SUMMARY>
private IContainer components = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Constructor , Dispose#region Constructor , Dispose
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public FormBase()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
//Adding Mouse Event Handlers for the Form
//
MouseDown += new MouseEventHandler(Form_MouseDown);
MouseUp += new MouseEventHandler(Form_MouseUp);
MouseMove += new MouseEventHandler(Form_MouseMove);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <SUMMARY>
/// Clean up any resources being used.
/// </SUMMARY>
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (disposing && (components != null))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
base.Dispose(disposing);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows Form Designer generated code#region Windows Form Designer generated code
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <SUMMARY>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </SUMMARY>
private void InitializeComponent()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// FormBase
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(369, 182);
this.Name = "FormBase";
this.Text = "AlerterForm";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Overriden Functions#region Overriden Functions
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected override void OnControlAdded(ControlEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
//Add Mouse Event Handlers for each control added into the form,
//if Draggable property of the form is set to true and the control
//name is not in the ExcludeList.Exclude list is the comma seperated
//list of the Controls for which you do not require the mouse handler
//to be added. For Example a button.
//
if (Draggable && (ExcludeList.IndexOf(e.Control.Name) == -1))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
e.Control.MouseDown += new MouseEventHandler(Form_MouseDown);
e.Control.MouseUp += new MouseEventHandler(Form_MouseUp);
e.Control.MouseMove += new MouseEventHandler(Form_MouseMove);
}
base.OnControlAdded(e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Event Handlers#region Event Handlers
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void Form_MouseDown(object sender, MouseEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
//On Mouse Down set the flag drag=true and
//Store the clicked point to the start_point variable
//
if (e.Button == MouseButtons.Left)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
drag = true;
start_point = new Point(e.X, e.Y);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void Form_MouseUp(object sender, MouseEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
//Set the drag flag = false;
//
if (e.Button == MouseButtons.Left)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
drag = false;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void Form_MouseMove(object sender, MouseEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
//If drag = true, drag the form
//
if (drag)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Top += e.Y - start_point.Y;
Left += e.X - start_point.X;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Properties#region Properties
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string ExcludeList
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ exclude_list = value; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return exclude_list.Trim(); }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool Draggable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ draggable = value; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return draggable; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
参考文献
1.http://www.codeproject.com/useritems/DraggableForm.asp
2.http://www.codeproject.com/csharp/irregularshapedform.asp