C#中实现窗口拖动
2011-02-20 14:42
363 查看
const int WM_NCLBUTTONDOWN = 0xA1; const int HT_CAPTION = 0x2; [DllImport("user32.dll")] static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); private void Form2_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left & this.WindowState == FormWindowState.Normal) { // 移动窗体 this.Capture = false; SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); } }
实验证明这个方法是比较好的,它模拟了点击窗口标题栏进行拖动的动作,而且即使没有标题栏的窗口也可以用,这段代码转自:http://zhidao.baidu.com/question/195773173.html
我以前用过的方法是设置窗体的Location的方法,很卡,而且快速拖动会不同步
Point m_oldpoint; bool m_mouse_is_down = false; private void pictureBox_title_MouseDown(object sender, MouseEventArgs e) { m_oldpoint = MousePosition; } private void pictureBox_title_MouseMove(object sender, MouseEventArgs e) { if ((e.Button != MouseButtons.Left) || (MousePosition == m_oldpoint)) { return; } this.Location = new Point(this.Location.X + MousePosition.X - m_oldpoint.X, this.Location.Y + MousePosition.Y - m_oldpoint.Y); m_oldpoint = MousePosition; }
相关文章推荐
- C# 无边框窗口实现拖动
- C#下如何实现无标题可拖动窗口 (转)
- c#无边框窗口的入门级拖动实现
- C#实现控件拖动窗口
- C# 实现无边框窗口拖动
- C# 实现无标题栏窗口拖动效果
- c#实现无标题栏窗口的拖动
- C#实现Winform鼠标拖动窗口大小时设定窗口最小尺寸的方法
- C#下如何实现无标题可拖动窗口
- C#下如何实现无标题可拖动窗口2
- C#下如何实现无标题可拖动窗口
- C#实现鼠标拖动自定义窗口
- C#实现窗口拖动时各个控件同比自己主动放缩大小
- c# 实现用鼠标拖动没有标题栏的窗口(窗体的任何部分都可实现拖动效果)
- c#实现无标题栏窗口的拖动
- C#下如何实现无标题可拖动窗口
- c#实现无标题栏窗口的拖动
- C# 实现无标题栏窗口拖动效果
- C# 窗口实现拖动
- C# 实现窗口无边框,可拖动效果