日期:2014-05-18  浏览次数:20858 次

.NET怎么移动没有边框的窗体
求各位大神指导下,最好有个例子,要注意哪些 需要什么操作 最好也说一下

------解决方案--------------------
重写窗体的WndProc

protected override void WndProc(ref Message m)
{
const int WM_NCHITTEST = 0x84;
const int HTCLIENT = 0x01;
const int HTCAPTION = 0x02;
const int WM_SYSCOMMAND = 0x112;
const int SC_MAXMIZE = 0xF030;
const int WM_NCLBUTTONDBLCLK = 0xA3;
switch (m.Msg)
{
case 0x4e:
case 0xd:
case 0xe:
case 0x14:
base.WndProc(ref m);
break;
case WM_NCHITTEST://鼠标点任意位置后可以拖动窗体

this.DefWndProc(ref m);
if (m.Result.ToInt32() == HTCLIENT)
{
m.Result = new IntPtr(HTCAPTION);//让系统以为点的是标题
return;
}
break;
case WM_NCLBUTTONDBLCLK://禁止双击最大化
return;
break;
default:

base.WndProc(ref m);
break;
}
}
------解决方案--------------------
[DllImport("User32.dll", CharSet=CharSet.Auto)]
public static extern bool ReleaseCapture();
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static public extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); 
private static int WM_NCLBUTTONDOWN=0xA1;
private static int HTCAPTION = 2;

private void Form_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
------解决方案--------------------
把这个方法放在窗体的类中,然后运行看下效果
C# code
protected override void WndProc(ref Message m)
{
    if (m.Msg == 0x0201)
    {
        m.Msg = 0x00A1;
        m.WParam = (IntPtr)2;
    }
    base.WndProc(ref m);
}