C# 无边框窗体 移动 两种方法
2016-08-25 13:47
525 查看
转载自:http://blog.sina.com.cn/s/blog_6b7c38030100xx17.html网上和书上大致有两种方法,各有长短吧。一种是使用Windows API://需添加using System.Runtime.InteropServices;[DllImport("user32.dll")] public static extern bool ReleaseCapture();[DllImport("user32.dll")]public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); //......private void Form1_MouseDown(object sender, MouseEventArgs e){//窗体移动 if (e.Button == MouseButtons.Left) { ReleaseCapture(); //释放鼠标捕捉 //发送左键点击的消息至该窗体(标题栏) SendMessage(Handle, 0xA1, 0x02, 0); } }这个方法,使用的话拖动效果和正常窗口拖动效果差不多,但是一句ReleaseCapture()就使窗口的某些Mouse事件无法响应。比如MouseClick。有时候是不能忍的。第二种方法是在某几个Mouse事件中共同实现。如下:bool beginMove = false;//初始化
int currentXPosition;
int currentYPosition;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{ if (e.Button == MouseButtons.Left) {
beginMove = true;
currentXPosition = MousePosition.X;//鼠标的x坐标为当前窗体左上角x坐标
currentYPosition = MousePosition.Y;//鼠标的y坐标为当前窗体左上角y坐标 }
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (beginMove)
{
this.Left += MousePosition.X - currentXPosition;//根据鼠标x坐标确定窗体的左边坐标x
this.Top += MousePosition.Y - currentYPosition;//根据鼠标的y坐标窗体的顶部,即Y坐标
currentXPosition = MousePosition.X;
currentYPosition = MousePosition.Y;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left){
currentXPosition = 0; //设置初始状态currentYPosition = 0;beginMove = false;
}}这种方法在C#中比较正规(不用调用API了)。缺点是,还得多加一些方法优化窗口移动效果。
相关文章推荐
- [转]C#无边框窗体移动的三种方法
- C# 系统应用之无标题窗体移动的两种方法
- [转]C#无边框窗体移动的三种方法
- C#移动无边框窗体的简单方法
- C#实现无边框窗体移动方法
- c#中两种方法拖动没有边框的窗体
- [转]C#无边框窗体移动的三种方法
- [转]C#无边框窗体移动的三种方法
- C#WinForm无边框窗体移动方法、模仿鼠标单击标题栏移动窗体位置
- c# 无边框窗体鼠标点击移动方法
- VB.NET移动无边框窗体 两种方法
- C#无边框窗体移动的三种方法
- C# 无边框窗体的移动,任务栏右键菜单,调整大小
- C# 窗体无边框 实现窗体移动(可在指定位置)
- 使用C#实现移动无边框窗体
- c#无边框窗体移动 屏蔽双击最大化
- winform技巧—没有边框移动窗体的方法
- C#无边框窗体调整大小的方法
- C# 移动无边框的窗体
- C#无边框窗体的移动