gpt4 book ai didi

c# - 如何在 WPF 窗口中只允许统一调整大小?

转载 作者:IT王子 更新时间:2023-10-29 04:53:51 26 4
gpt4 key购买 nike

我不希望我的窗口“仅水平”或“仅垂直”调整大小。是否有我可以在我的窗口上设置的属性来强制执行此操作,或者是否有我可以使用的漂亮的代码隐藏技巧?

最佳答案

您始终可以处理 WM_WINDOWPOSCHANGING 消息,这样您就可以在调整大小的过程中控制窗口大小和位置(而不是在用户完成调整后进行修复)。

下面是你在 WPF 中的做法,我结合了几个来源的代码,因此其中可能存在一些语法错误。

internal enum WM
{
WINDOWPOSCHANGING = 0x0046,
}

[StructLayout(LayoutKind.Sequential)]
internal struct WINDOWPOS
{
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int x;
public int y;
public int cx;
public int cy;
public int flags;
}

private void Window_SourceInitialized(object sender, EventArgs ea)
{
HwndSource hwndSource = (HwndSource)HwndSource.FromVisual((Window)sender);
hwndSource.AddHook(DragHook);
}

private static IntPtr DragHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handeled)
{
switch ((WM)msg)
{
case WM.WINDOWPOSCHANGING:
{
WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS));
if ((pos.flags & (int)SWP.NOMOVE) != 0)
{
return IntPtr.Zero;
}

Window wnd = (Window)HwndSource.FromHwnd(hwnd).RootVisual;
if (wnd == null)
{
return IntPtr.Zero;
}

bool changedPos = false;

// ***********************
// Here you check the values inside the pos structure
// if you want to override tehm just change the pos
// structure and set changedPos to true
// ***********************

if (!changedPos)
{
return IntPtr.Zero;
}

Marshal.StructureToPtr(pos, lParam, true);
handeled = true;
}
break;
}

return IntPtr.Zero;
}

关于c# - 如何在 WPF 窗口中只允许统一调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/386484/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com