gpt4 book ai didi

c# - Windows WPF 中的垂直最大化

转载 作者:可可西里 更新时间:2023-11-01 14:35:59 26 4
gpt4 key购买 nike

我正在开发一个 WPF 应用程序,我在窗口周围添加了一个清晰的边框,以便可以从主窗口外部调整它的大小。我已经覆盖了 MINMAXINFO如图here .使用下面的代码,当我进行常规最大化时,您看不到不可见的边框。但是,当我尝试垂直最大化(通过将窗口顶部拖动到屏幕顶部)时,会显示不可见的边框。我已经 try catch 所有消息,但找不到用于垂直最大化的单独消息。对于这种情况,我该如何去除不可见的边框?

private static void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam) {
MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));

// Adjust the maximized size and position to fit the work area of the correct monitor
int MONITOR_DEFAULTTONEAREST = 0x00000002;
System.IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);

if (monitor != System.IntPtr.Zero) {
MONITORINFO monitorInfo = new MONITORINFO();
GetMonitorInfo(monitor, monitorInfo);
RECT rcWorkArea = monitorInfo.rcWork;
RECT rcMonitorArea = monitorInfo.rcMonitor;
mmi.ptMaxPosition.x = Math.Abs (rcWorkArea.left - rcMonitorArea.left) - borderThickness;
mmi.ptMaxPosition.y = Math.Abs (rcWorkArea.top - rcMonitorArea.top) - borderThickness;
mmi.ptMaxSize.x = Math.Abs (rcWorkArea.right - rcWorkArea.left) + 2 * borderThickness;
mmi.ptMaxSize.y = Math.Abs (rcWorkArea.bottom - rcWorkArea.top) + 2 * borderThickness;
}

Marshal.StructureToPtr(mmi, lParam, true);
}

最佳答案

这条消息对于评论来说有点大,所以我把它贴在这里。

这里有一些关于 AeroSnap 的有用信息:Handling AeroSnap message in WndProc这个问题说明垂直最大化和其他类似功能(Win+Left、Win+Right 等)没有单独的消息。但是您仍然可以分析其他消息(WM_SIZEWM_WINDOWPOSCHANGING)以过滤掉最大化并同时找到可能意味着使用 AeroSnap 的位置和大小的变化。然后您将能够调整窗口大小。

这是您可以修改代码以开始分析其他消息的方法:

public const int WM_WINDOWPOSCHANGING = 0x0046;
public const int WM_SIZE = 0x0005;

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

...

switch (msg)
{
case 0x0024: /* WM_GETMINMAXINFO */
WmGetMinMaxInfo(hwnd, lParam);
handled = true;
break;
case WM_WINDOWPOSCHANGING:
_counter++;
WmWindowPosChanging(lParam);
break;
}


private static void WmWindowPosChanging(System.IntPtr lparam)
{
WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(lparam, typeof(WINDOWPOS));

System.Diagnostics.Trace.WriteLine(string.Format("x: {0}, y: {1}, cx: {2}, cy: {3}, flags: {4:X}", pos.x, pos.y, pos.cx, pos.cy, pos.flags));
if (pos.x == 0)
{
pos.x = -thickness;
pos.cx += 2*thickness;
}

if (pos.y == 0)
{
pos.y = -thickness;
pos.cy += 2*thickness;
}

Marshal.StructureToPtr(pos, lparam, true);
}

此代码不处理 Win+Right 停靠并且不过滤掉最大化和最小化,但我相信这是一个很好的起点。WM_SIZE 可以根据需要以相同的方式添加。

您也可以尝试禁用 AeroSnap:How do you disable Aero Snap in an application? .

关于c# - Windows WPF 中的垂直最大化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25041416/

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