之前一直没有怎么好好学习C#,只是了解一个大概,做起事来才知道是多么的难,就拿本问来说,这一个功能,我找了好多资料都没有实现我想要的效果,最终还是自己琢磨出来了,发出来分享下,方便后来者借鉴,当然大家有什么更好的办法可以交流哈哈,言归正传。大家都知道,在有边框的情况下,form的拖动是不需要什么代码就可以实现的,但是在很多情况下,边框的存在影响了整体程序的布局和美观,不得不取消掉,那么就会发现根本无法拖动了,怎么实现拖动呢?首先定义三个参数private bool moveFlag = false;//鼠标点击
private int x = 0;
private int y = 0;x,y就是坐标了,不用解释接下来: protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
{
if (moveFlag && (e.Button == MouseButtons.Left))
this.SetBounds(Left + e.X - x, Top + e.Y - y, this.Width, this.Height);
base.OnMouseMove(e);
} protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if (!moveFlag && e.Clicks >= 1)
moveFlag = true;
x = e.X;
y = e.Y;
base.OnMouseDown(e);
} protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
if (moveFlag)
moveFlag = false;
base.OnMouseUp(e); }三个函数,一目了然,不用多做解释了,唯一要说明的事,获取的鼠标事件是当前form的,所以System.Windows.Forms.MouseEventArgs e。另外呢,还要加一点,大家都看到千千静听啊什么的那种软件,是多FORM的,怎么样在拖动主 form的时候附属form也跟着一起动呢?这点的实现也很简单,请看代码:在form1中定义一个 Form f;这个对象用来做什么呢?后面就会用到了在你打开form2的事件函数下加上Form2 f3 = new Form2(this);f3.show();f = f3;//哈哈,当前打开的Form2赋给f了好了,后面就可以直接操作f了。在上面的第三个函数中加入 protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
if (moveFlag)
moveFlag = false;
base.OnMouseUp(e); f.Hide();
Form3 f31 = new Form3(this);
f31.Show();
f = f31; }OK,测试一下吧,是不是实现了呢?
private int x = 0;
private int y = 0;x,y就是坐标了,不用解释接下来: protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
{
if (moveFlag && (e.Button == MouseButtons.Left))
this.SetBounds(Left + e.X - x, Top + e.Y - y, this.Width, this.Height);
base.OnMouseMove(e);
} protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if (!moveFlag && e.Clicks >= 1)
moveFlag = true;
x = e.X;
y = e.Y;
base.OnMouseDown(e);
} protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
if (moveFlag)
moveFlag = false;
base.OnMouseUp(e); }三个函数,一目了然,不用多做解释了,唯一要说明的事,获取的鼠标事件是当前form的,所以System.Windows.Forms.MouseEventArgs e。另外呢,还要加一点,大家都看到千千静听啊什么的那种软件,是多FORM的,怎么样在拖动主 form的时候附属form也跟着一起动呢?这点的实现也很简单,请看代码:在form1中定义一个 Form f;这个对象用来做什么呢?后面就会用到了在你打开form2的事件函数下加上Form2 f3 = new Form2(this);f3.show();f = f3;//哈哈,当前打开的Form2赋给f了好了,后面就可以直接操作f了。在上面的第三个函数中加入 protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
if (moveFlag)
moveFlag = false;
base.OnMouseUp(e); f.Hide();
Form3 f31 = new Form3(this);
f31.Show();
f = f31; }OK,测试一下吧,是不是实现了呢?
{
if (m.Msg == 163 && this.ClientRectangle.Contains(this.PointToClient(new Point(m.LParam.ToInt32()))) && m.WParam.ToInt32() == 2)
m.WParam = (IntPtr)1;
base.WndProc(ref m);
if (m.Msg == 132 && m.Result.ToInt32() == 1)
m.Result = (IntPtr)2;
}
int a = 0, b = 0;
private void frmLogin_MouseDown(object sender, MouseEventArgs e)
{
a = e.X;
b = e.Y;
this.Cursor = System.Windows.Forms.Cursors.SizeAll;
} private void frmLogin_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Location = new Point(e.X + this.Left - a, e.Y + this.Top - b);
}
} private void frmLogin_MouseUp(object sender, MouseEventArgs e)
{
this.Cursor = System.Windows.Forms.Cursors.Default;
if (this.Left < -Width / 2)
this.Left = -this.Width / 2;
if (this.Left > SystemInformation.PrimaryMonitorSize.Width - this.Width / 2)
this.Left = SystemInformation.PrimaryMonitorSize.Width - this.Width / 2;
if (this.Top < 20)
this.Top = 20;
if (this.Top > SystemInformation.PrimaryMonitorSize.Height - 20)
this.Top = SystemInformation.PrimaryMonitorSize.Height - 20;
} #endregion
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wparam, int lparam);
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)//按下的是鼠标左键
{
Capture = false;//释放鼠标,使能够手动操作
SendMessage(this.Handle, 0x00A1, 2, 0);//拖动窗体
}
}