C#实现无边框窗体拖动的两个方案
2014-06-01 02:35
531 查看
方案一:通过DllImport方式调用Windows API实现
方案二:通过重写WndProc窗口过程函数实现
using System.Runtime.InteropServices; const int WM_SYSCOMMAND = 0x0112; const int SC_MOVE = 0xF010; const int HT_CAPTION = 2; [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) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HT_CAPTION, 0); }
方案二:通过重写WndProc窗口过程函数实现
const int WM_NCHITTEST = 0x0084; const int HT_LEFT = 10; const int HT_RIGHT = 11; const int HT_TOP = 12; const int HT_TOPLEFT = 13; const int HT_TOPRIGHT = 14; const int HT_BOTTOM = 15; const int HT_BOTTOMLEFT = 16; const int HT_BOTTOMRIGHT = 17; const int HT_CAPTION = 2; protected override void WndProc(ref Message Msg) { if (Msg.Msg == WM_NCHITTEST) { //获取鼠标位置 int nPosX = (Msg.LParam.ToInt32() & 65535); int nPosY = (Msg.LParam.ToInt32() >> 16); //右下角 if (nPosX >= this.Right - 6 && nPosY >= this.Bottom - 6) { Msg.Result = new IntPtr(HT_BOTTOMRIGHT); return; } //左上角 else if (nPosX <= this.Left + 6 && nPosY <= this.Top + 6) { Msg.Result = new IntPtr(HT_TOPLEFT); return; } //左下角 else if (nPosX <= this.Left + 6 && nPosY >= this.Bottom - 6) { Msg.Result = new IntPtr(HT_BOTTOMLEFT); return; } //右上角 else if (nPosX >= this.Right - 6 && nPosY <= this.Top + 6) { Msg.Result = new IntPtr(HT_TOPRIGHT); return; } else if (nPosX >= this.Right - 2) { Msg.Result = new IntPtr(HT_RIGHT); return; } else if (nPosY >= this.Bottom - 2) { Msg.Result = new IntPtr(HT_BOTTOM); return; } else if (nPosX <= this.Left + 2) { Msg.Result = new IntPtr(HT_LEFT); return; } else if (nPosY <= this.Top + 2) { Msg.Result = new IntPtr(HT_TOP); return; } else { Msg.Result = new IntPtr(HT_CAPTION); return; } } base.WndProc(ref Msg); }
相关文章推荐
- C#实现无边框窗体拖动
- C#实现无边框窗体拖动
- C#中实现拖动无边框窗体Form
- C#中实现拖动无边框窗体Form
- C#实现无边框窗体拖动
- c# 当窗体设置成无边框后,如何实现拖动效果,简洁实用
- C#中实现拖动无边框Form窗体和窗体的起始位置
- C#中实现拖动无边框窗体Form
- C# 如何窗体去掉上面的最大化,最小化合关闭以及实现无边框窗体拖动【申明:来源于网络】
- C#中实现拖动无边框窗体Form
- C#无边框窗体实现以及拖动代码
- C# 实现拖动无边框窗体
- C# 如何窗体去掉上面的最大化,最小化合关闭以及实现无边框窗体拖动
- C#基础系列:开发自己的窗体设计器(实现控件的拖动)
- 利用System.EventHandler来实现两个窗体间的事件调用 (C#)
- C#实现窗体和任意控件的拖动
- C#中实现鼠标拖动窗体的方法
- MFC无边框的窗体实现用鼠标拖动窗体边缘实现窗体大小变化
- C#实现控件随窗体拖动而自动缩放其大小
- C# 窗体无边框 实现窗体移动(可在指定位置)