当WPF中的“Allow Transparent”为True时,如何调整窗口大小

7

如您所知,当允许透明属性为真时,窗口的边框会隐藏,我们无法重新调整窗口大小。只有在设置ResizeMode为CanResizeWithGrip时,我们才能重新调整窗口大小,但这个解决方案并不好,因为我们只能在窗口最大化时通过窗口右下角而不是所有四个角来调整窗口大小。那么问题来了,如何在WPF中在允许透明的情况下重新调整窗口大小而不使用CanResizeWithGrip?

提前感谢!

~ Thi

3个回答

11

我希望设置窗口可以通过隐藏边框来调整大小,所以最终我找到了一个处理方案,您可以按照以下代码进行操作:

我的期望是设置窗口可以通过隐藏边框来调整大小,因此我最终找到了一个解决方案来处理这个问题,您可以按照下面的代码进行操作:

<WindowChrome.WindowChrome>
    <WindowChrome     
        CaptionHeight="1"  
        CornerRadius ="0"
        ResizeBorderThickness="4"         
        GlassFrameThickness="0">
    </WindowChrome>
</WindowChrome.WindowChrome>
请在需要更多信息时添加更多讨论。

1
这是我认为最好的方法。我们在自己的解决方案中也是这样做的。在你发帖之前,我正要建议这个方法。 :) 我建议的一件事是,如果你有完整的窗口拖动代码,如 MouseLeftButtonDown += (sender, e) => DragMove();,请确保 ResizeBorderThickness 被充分测试,以便足够并且不会与窗口拖动冲突。 - Dax Pandhi
太棒了!谢谢你! - Ryan

2

很好,这其实非常简单。 Window 上有一个名为 ResizeMode 的属性,如果将其设置为 "CanResizeWithGrip",则会自动在您的窗口上放置一个 ResizeGrip 控件,如果运行应用程序,则会正确地调整您的窗口大小。


谢谢你的回答,@Ahmed。但是我想在允许透明的情况下重新调整窗口大小,而不使用CanResizeWithGrip。你有什么解决方案吗? - Thi Le

2

有一个类似的问题。
我认为它在你的情况下也可以使用。

如何创建一个没有边框但可以通过拖动手柄调整大小的WPF窗口?

总结一下,
在主窗口中添加可隐藏的手柄,可以是矩形或边框。
将以下方法附加到手柄的事件上。

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    [DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();

    //Attach this to the MouseDown event of your drag control to move the window in place of the title bar
    private void WindowDrag(object sender, MouseButtonEventArgs e) // MouseDown
    {
        ReleaseCapture();
        SendMessage(new WindowInteropHelper(this).Handle,
            0xA1, (IntPtr)0x2, (IntPtr)0);
    }

    //Attach this to the PreviewMousLeftButtonDown event of the grip control in the lower right corner of the form to resize the window
    private void WindowResize(object sender, MouseButtonEventArgs e) //PreviewMousLeftButtonDown
    {
        HwndSource hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
        SendMessage(hwndSource.Handle, 0x112, (IntPtr)61448, IntPtr.Zero);
    }

然而,如果您将AllowsTransparency设置为true,则会使默认的WPF WebBrowser不可见,同时还有其他缺点。

以上链接中已经很好地解释了这一点。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接