我有这段代码在运行时调整表单大小:
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HTBORDER = 18;
private const int HTBOTTOM = 15;
private const int HTBOTTOMLEFT = 16;
private const int HTBOTTOMRIGHT = 17;
private const int HTCAPTION = 2;
private const int HTLEFT = 10;
private const int HTRIGHT = 11;
private const int HTTOP = 12;
private const int HTTOPLEFT = 13;
private const int HTTOPRIGHT = 14;
[DllImport("user32.dll")]
public extern static bool ReleaseCapture();
[DllImport("user32.dll")]
public extern static bool SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private enum ResizeDirection
{
None,
Left,
TopLeft,
Top,
TopRight,
Right,
BottomRight,
Bottom,
BottomLeft
}
private ResizeDirection resizeDir;
private ResizeDirection ResizeDir
{
get { return resizeDir; }
set
{
resizeDir = value;
switch (resizeDir)
{
case ResizeDirection.Left:
Cursor = Cursors.SizeWE;
break;
case ResizeDirection.Right:
Cursor = Cursors.SizeWE;
break;
case ResizeDirection.Top:
Cursor = Cursors.SizeNS;
break;
case ResizeDirection.Bottom:
Cursor = Cursors.SizeNS;
break;
case ResizeDirection.BottomLeft:
Cursor = Cursors.SizeNESW;
break;
case ResizeDirection.TopRight:
Cursor = Cursors.SizeNESW;
break;
case ResizeDirection.BottomRight:
Cursor = Cursors.SizeNWSE;
break;
case ResizeDirection.TopLeft:
Cursor = Cursors.SizeNWSE;
break;
default:
Cursor = Cursors.Default;
break;
}
}
}
private void ResizeForm(ResizeDirection direction)
{
int dir = -1;
switch (direction)
{
case ResizeDirection.Left:
dir = HTLEFT;
break;
case ResizeDirection.TopLeft:
dir = HTTOPLEFT;
break;
case ResizeDirection.Top:
dir = HTTOP;
break;
case ResizeDirection.TopRight:
dir = HTTOPRIGHT;
break;
case ResizeDirection.Right:
dir = HTRIGHT;
break;
case ResizeDirection.BottomRight:
dir = HTBOTTOMRIGHT;
break;
case ResizeDirection.Bottom:
dir = HTBOTTOM;
break;
case ResizeDirection.BottomLeft:
dir = HTBOTTOMLEFT;
break;
}
if (dir != -1)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, dir, 0);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && WindowState != FormWindowState.Maximized)
ResizeForm(ResizeDir);
base.OnMouseDown(e);
}
private const int BorderWidth = 6;
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Location.X < BorderWidth && e.Location.Y < BorderWidth)
ResizeDir = ResizeDirection.TopLeft;
else if (e.Location.X < BorderWidth && e.Location.Y > Height - BorderWidth)
ResizeDir = ResizeDirection.BottomLeft;
else if (e.Location.X > Width - BorderWidth && e.Location.Y > Height - BorderWidth)
ResizeDir = ResizeDirection.BottomRight;
else if (e.Location.X > Width - BorderWidth && e.Location.Y < BorderWidth)
ResizeDir = ResizeDirection.TopRight;
else if (e.Location.X < BorderWidth)
ResizeDir = ResizeDirection.Left;
else if (e.Location.X > Width - BorderWidth)
ResizeDir = ResizeDirection.Right;
else if (e.Location.Y < BorderWidth)
ResizeDir = ResizeDirection.Top;
else if (e.Location.Y > Height - BorderWidth)
ResizeDir = ResizeDirection.Bottom;
else ResizeDir = ResizeDirection.None;
base.OnMouseMove(e);
}
代码工作正常没有问题,但我也希望在表单上显示系统菜单,我也使用此代码:
private const int WS_CLIPCHILDREN = 0x2000000;
private const int WS_MINIMIZEBOX = 0x20000;
private const int WS_MAXIMIZEBOX = 0x10000;
private const int WS_SYSMENU = 0x80000;
private const int CS_DBLCLKS = 0x8;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU;
cp.ClassStyle = CS_DBLCLKS;
return cp;
}
}
当我放置最后一个代码时,缩放器代码停止工作,当我更改此代码时:
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU;
cp.Style |= WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_MAXIMIZEBOX; //| WS_SYSMENU;
cp.ClassStyle = CS_DBLCLKS;
return cp;
}
}
缩放器代码重新开始工作,但系统菜单不再出现 解决方案是什么?