最近有需求做到这个无边框窗体的缩放已经移动,在网上和帮助里面查了很久资料,总结的前人也很多,
最后还是觉得使用重写wndProc这个消息函数要来的靠谱和简单,网上很多地方的代码多少都有点儿问题
在前人的肩膀上,我修改了一些地方,经过严格测试后,觉得还是挺不错得,特地来分享给大家,也算是对前人们
的回报,看代码
private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
#region 窗体移动
if (m.Msg == WM_NCHITTEST)
{
this.DefWndProc(ref m);
if (m.Result.ToInt32() == HTCLIENT)
m.Result = new IntPtr(HTCAPTION);
}
#endregion
#region 窗体缩放
if (m.Msg == WM_NCHITTEST && this.FormBorderStyle == FormBorderStyle.None && this.WindowState!=FormWindowState.Minimized)
{
Point z = Control.MousePosition;
if (z != null)
{
Point tmp = this.PointToScreen(new Point());
z.X -= tmp.X;
z.Y -= tmp.Y;
}
Padding resizePadding = new Padding(4);
int result = 0;
if (z.X < ( resizePadding.Left)) result = 10; //鼠标在左边
else if (z.X > (this.Width- resizePadding.Right)) result = 11;//鼠标在右边
else result = 0;
if (z.Y < ( resizePadding.Top))
{
switch (result)
{
case 0:
result = 12;
break;
case 10:
result = 13;
break;
case 11:
result = 14;
break;
default:
break;
}
}
else if (z.Y < (Top + 20))
{
switch (result)
{
case 0:
result = 2; //可以拖动
break;
default:
break;
}
}
else if (z.Y > (this.Height-resizePadding.Bottom))
{
switch (result)
{
case 0:
result = 15;
break;
case 10:
result = 16;
break;
case 11:
result = 17;
break;
default:
break;
}
}
if (result != 0) m.Result = (IntPtr)result;
}
#endregion
}