克里斯蒂安,您的解决方案让我走上了正确的道路,但是对于像我这样的愚蠢和懒惰的人来说,它缺少复制粘贴代码!在这里,如果您取消注释注释部分,您可以移动窗口,除了边框。
private void Action_LMouseDownAndMove(object sender, MouseEventArgs e)
{
Point mousePosition = this.PointToClient(System.Windows.Forms.Cursor.Position);
const int WM_NCLBUTTONDOWN = 0xA1;
//const int HT_CAPTION = 0x2;
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
if (mousePosition.X < 20)
{
if (mousePosition.Y < 20)
SendMessage(Handle, WM_NCLBUTTONDOWN, 13, 0);
else if (mousePosition.Y > this.Size.Height - 20)
SendMessage(Handle, WM_NCLBUTTONDOWN, 16, 0);
else
SendMessage(Handle, WM_NCLBUTTONDOWN, 10, 0);
}
else if (mousePosition.X > this.Size.Width - 20)
{
if (mousePosition.Y < 20)
SendMessage(Handle, WM_NCLBUTTONDOWN, 14, 0);
else if (mousePosition.Y > this.Size.Height - 20)
SendMessage(Handle, WM_NCLBUTTONDOWN, 17, 0);
else
SendMessage(Handle, WM_NCLBUTTONDOWN, 11, 0);
}
else if (mousePosition.Y < 20)
SendMessage(Handle, WM_NCLBUTTONDOWN, 12, 0);
else if (mousePosition.Y > this.Size.Height - 20)
SendMessage(Handle, WM_NCLBUTTONDOWN, 15, 0);
//else
// SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
根据您的喜好更改 20 像素值。我不是 100% 确定左/右和上/下具有完全相同的调整大小区域,或者如果您知道我的意思,一个值是否需要为 19/21……这里的 if/else 树有一些减少的可能性, 我知道。而不是 20 我应该使用一个常数。
要更改光标以向用户显示他可以调整大小,我使用以下代码,它只是一个 MouseMove 事件处理程序:
this.pictureBox.MouseMove += new MouseEventHandler((a, e) =>
{
Point h = this.PointToClient(System.Windows.Forms.Cursor.Position);
if (h.X < 20)
{
if (h.Y < 20)
{
pictureBox.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
}
else if (h.Y > this.Size.Height - 20)
{
pictureBox.Cursor = System.Windows.Forms.Cursors.SizeNESW;
}
else
{
pictureBox.Cursor = System.Windows.Forms.Cursors.SizeWE;
}
}
else if (h.X > this.Size.Width - 20)
{
if (h.Y < 20)
{
pictureBox.Cursor = System.Windows.Forms.Cursors.SizeNESW;
}
else if (h.Y > this.Size.Height - 20)
{
pictureBox.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
}
else
{
pictureBox.Cursor = System.Windows.Forms.Cursors.SizeWE;
}
}
else if (h.Y < 20)
{
pictureBox.Cursor = System.Windows.Forms.Cursors.SizeNS;
}
else if (h.Y > this.Size.Height - 20)
{
pictureBox.Cursor = System.Windows.Forms.Cursors.SizeNS;
}
else
{
pictureBox.Cursor = System.Windows.Forms.Cursors.Default;
}
});