您的位置:首页 > 其它

实现窗体拖动的两种方法

2013-03-10 22:47 330 查看
方法1:系统消息

protected override void WndProc(ref Message msg)
{
base.WndProc(ref msg);

const int WM_NCHITTEST = 0x84;
const int HTCLIENT = 0x01;

if (msg.Msg == WM_NCHITTEST)
if (HTCLIENT == msg.Result.ToInt32())
{
Point p = new Point();
p.X = (msg.LParam.ToInt32() & 0xFFFF);
p.Y = (msg.LParam.ToInt32() >> 16);

p = PointToClient(p);

msg.Result = new IntPtr(2);
}
}


方法2:事件

private int mx = 0, my = 0;
private bool mc = false;

protected override void OnMouseDown(MouseEventArgs e)
{
mx = e.X;
my = e.Y;
mc = true;
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (mc == true)
{
this.Left = this.Left + (e.X - mx);
this.Top = this.Top + (e.Y - my);
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
mc = false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: