我一直在搜索几个小时并且没有找到任何有用的东西,因为所有其他问题都过于本地化,所以这是我的这个(常见)问题的版本:
我有一个WPF窗口,其中 WindowStyle =无和 ResizeMode = NoResize (否则我会得到我不想要的大小调整边框)和最重要的 AllowTransparency = 错误,我必须坚持此设置。
将 ResizeMode 更改为 CanResize 并使用以下MouseDown处理程序自定义调整大小手柄
[DllImport("User32.dll")]
public static extern bool ReleaseCapture();
[DllImport("User32.dll")]
public static extern IntPtr SendMessage(
IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);
private void OnResizeGripMouseDown(object sender, MouseButtonEventArgs e) {
ReleaseCapture();
SendMessage(Window.Handle, WM_NCLBUTTONDOWN, HTBOTTOMRIGHT, 0);
}
允许我完美地调整窗口的大小,但是剩下调整大小的边框。有没有办法强制窗口的可恢复性,虽然它有 ResizeMode = NoResize ?
(也许是通过SetWindowLong
和GWL_EXSTYLE
?如果需要留言,我已经设置了WindowProc
来处理此问题。)
答案 0 :(得分:3)
我能够通过使用另一条消息强制执行所需的行为
SendMessage(Handle, WM_SYSCOMMAND, SC_SIZE + direction, 0);
其中WM_SYSCOMMAND是默认的Windows消息,SC_SIZE是由0xF000定义的wParam,方向是此枚举定义的命中测试句柄的数值
public enum SysCommandSize : int {
SC_SIZE_HTLEFT = 1,
SC_SIZE_HTRIGHT = 2,
SC_SIZE_HTTOP = 3,
SC_SIZE_HTTOPLEFT = 4,
SC_SIZE_HTTOPRIGHT = 5,
SC_SIZE_HTBOTTOM = 6,
SC_SIZE_HTBOTTOMLEFT = 7,
SC_SIZE_HTBOTTOMRIGHT = 8
}
答案 1 :(得分:0)
克里斯蒂安,你的解决方案让我走上了正确的道路,但它缺乏像我这样愚蠢和懒惰的复制粘贴代码! 在这里,如果您取消注释评论的部分,您可以移动窗口,除了边框。
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;
}
});