C#无边框窗体移动的三种方法
1. 重写WndProc
protected override void WndProc(ref Message m) { const int WM_NCHITTEST = 0x84; const int HTCLIENT = 0x01; const int HTCAPTION = 0x02; if (m.Msg == WM_NCHITTEST) { this.DefWndProc(ref m); if (m.Result.ToInt32() == HTCLIENT) m.Result = new IntPtr(HTCAPTION); else base.WndProc(ref m); } else { base.WndProc(ref m); } }
2.创建消息
private const int WM_NCLBUTTONDOWN = 0x00A1; private const int WM_NCHITTEST = 0x84; private const int HT_CAPTION = 0x2; private const int HT_CLIENT = 0x1; private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { //this.Capture = false; pictureBox1.Capture = false; Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero); WndProc(ref msg); }
3.调用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); public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; //在窗体的_MouseDown中加入如下代码: 如: private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); }
WinForm 禁止最大化、最小化、双击标题栏、双击图标等操作
protected override void WndProc(ref Message m) { if (m.Msg==0x112) { switch ((int) m.WParam) { //禁止双击标题栏关闭窗体 case 0xF063: case 0xF093: m.WParam = IntPtr.Zero; break; //禁止拖拽标题栏还原窗体 case 0xF012: case 0xF010: m.WParam = IntPtr.Zero; break; //禁止双击标题栏 case 0xf122: m.WParam = IntPtr.Zero; break; //禁止关闭按钮 case 0xF060: m.WParam = IntPtr.Zero; break; //禁止最大化按钮 case 0xf020: m.WParam = IntPtr.Zero; break; //禁止最小化按钮 case 0xf030: m.WParam = IntPtr.Zero; break; //禁止还原按钮 case 0xf120: m.WParam = IntPtr.Zero; break; } } base.WndProc(ref m); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。