找了一天,才解决,虽然解决的也不是很好。是参考这篇文章的。http://blog.sina.com.cn/s/blog_6ab738af0100juex.html
实在太感谢了
贴下来代码,以后再用
#region WndProc
/// <summary>
/// 重写WndProc
/// </summary>
const int SC_SIZE = 0xF000;
const int WM_NCHITTEST = 0x0084;
const int HTLEFT = 10;
const int HTRIGHT = 11;
const int HTTOP = 12;
const int HTTOPLEFT = 13;
const int HTTOPRIGHT = 14;
const int HTBOTTOM = 15;
const int HTBOTTOMLEFT = 0x10;
const int HTBOTTOMRIGHT = 17;
[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;
public const int HTCLIENT = 0x1;
public const int WMSZ_LEFT = 0xF001;
public const int WMSZ_RIGHT = 0xF002;
public const int WMSZ_TOP = 0xF003;
public const int WMSZ_TOPLEFT = 0xF004;
public const int WMSZ_TOPRIGHT = 0xF005;
public const int WMSZ_BOTTOM = 0xF006;
public const int WMSZ_BOTTOMLEFT = 0xF007;
public const int WMSZ_BOTTOMRIGHT = 0xF008;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_NCHITTEST:
base.WndProc(ref m);
Point vPoint = new Point((int)m.LParam & 0xFFFF,
(int)m.LParam >> 16 & 0xFFFF);
vPoint = PointToClient(vPoint);
if (this.WindowState != FormWindowState.Maximized)
{
if (vPoint.X < 10)
{
if (vPoint.Y < 10)
{
m.Result = (IntPtr)HTTOPLEFT;
}
else if (vPoint.Y > this.Height - 10)
{
m.Result = (IntPtr)HTBOTTOMLEFT;
}
else
{
m.Result = (IntPtr)HTLEFT;
}
}
else if (vPoint.X > this.Width - 10)
{
if (vPoint.Y < 10)
{
m.Result = (IntPtr)HTTOPRIGHT;
}
else if (vPoint.Y > this.Height - 10)
{
m.Result = (IntPtr)HTBOTTOMRIGHT;
}
else
{
m.Result = (IntPtr)HTRIGHT;
}
}
else if (10 < vPoint.X && vPoint.X < this.Width - 10)
{
if (vPoint.Y < 10)
{
m.Result = (IntPtr)HTTOP;
}
else if (vPoint.Y > this.Height - 10)
{
m.Result = (IntPtr)HTBOTTOM;
}
}
}
break;
}
base.WndProc(ref m);
}
#endregion
然后,在鼠标按下时,SendMessage就可以了。
private void pnlRight_MouseDown(object sender, MouseEventArgs e)
{
this.Cursor = Cursors.SizeWE;
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, WMSZ_RIGHT, 0);
}
别的方向,就换一下参数
然后,如果要虚线框的话,就加上WS_SIZEBOX
int WS_SYSMENU = 0x00080000;//系统菜单
int WS_MINIMIZEBOX = 0x20000; // 最大最小化按钮
int WS_SIZEBOX = 0x00040000;
int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));
SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU | WS_MINIMIZEBOX| WS_SIZEBOX);
但是这样做的不好是,改变大小后,窗口外多了框。不知道怎么去掉。
所以不要粗虚线框的话,还是把WS_SIZEBOX去掉把