WPF 无边框窗口调整大小

10

我正在WPF中设计自己的定制窗口,一直在尝试实现之前WinForms中使用过的调整大小功能。但是出于某种原因,我的WndProc返回值没有给我正确的结果。

我有一个NativeMethods类来处理所有的WndProc消息和结果:

public class NativeMethods
{
    public const int WM_NCHITTEST  = 0x84;
    public const int HTCAPTION     = 2;
    public const int HTLEFT        = 10;
    public const int HTRIGHT       = 11;
    public const int HTTOP         = 12;
    public const int HTTOPLEFT     = 13;
    public const int HTTOPRIGHT    = 14;
    public const int HTBOTTOM      = 15;
    public const int HTBOTTOMLEFT  = 16;
    public const int HTBOTTOMRIGHT = 17;
}

这是我的窗口后面的代码:

public partial class MainWindow : Window
{
    const int GripSize   = 16;
    const int BorderSize = 7;

    public MainWindow()
    {
        InitializeComponent();
    }

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        IntPtr windowHandle = new WindowInteropHelper(this).Handle;
        HwndSource windowSource = HwndSource.FromHwnd(windowHandle);
        windowSource.AddHook(WndProc);
    }

    private IntPtr WndProc(IntPtr hwnd, int msg,
        IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if (msg == NativeMethods.WM_NCHITTEST)
        {
            int x = lParam.ToInt32() << 16 >> 16, y = lParam.ToInt32() >> 16;
            Point pos = PointFromScreen(new Point(x, y));

            if (pos.X > GripSize && 
                pos.X < ActualWidth - GripSize &&
                pos.Y >= ActualHeight - BorderSize)
            {
                return (IntPtr)NativeMethods.HTBOTTOM; // This doesn't work?
            }

            // Top, Left, Right, Corners, Etc.
        }

        return IntPtr.Zero;
    }
}

我希望光标能够变成“向下调整大小箭头”,并且调整大小功能像在我的WinForms项目中一样正常工作。我设置了断点,当光标在预期位置时,HTBOTTOM返回正在触发。在XAML中,我将ResizeMode设置为CanResize,WindowStyle设置为None。我做错了什么?


1
为什么不使用 ResizeMode = "CanResizeWithGrip"?您想要实现这种效果(只是在窗口周围调整大小而没有 Grip)? - Anatoliy Nikolaev
1
因为我仍然希望能够像正常功能一样从任何角落或边缘调整窗口大小。我找出了问题并现在发布答案。 - Cris McLaughlin
3个回答

17

也许更简单的方法是分配WindowChrome。根据您的评论,您必须能够从所有侧面以及使用手柄调整大小。通过将WindowStyle设置为None并将ResizeMode设置为CanResizeWithGrip或CanResize(无论您希望实现什么),您可以完成所有这些操作。

<Window x:Class="MVVMProtoType.View.Test.Test"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Test" Height="300" Width="300" WindowStyle="None" AllowsTransparency="False" ResizeMode="CanResizeWithGrip">

在代码后台,你必须为窗口设置窗口Chrome。你可以这样做:

WindowChrome.SetWindowChrome(this, new WindowChrome());

或者,您可以使用设置器来设置窗口样式,例如:

<Setter Property="WindowChrome.WindowChrome">
        <Setter.Value>
            <WindowChrome CornerRadius="0" GlassFrameThickness="1" UseAeroCaptionButtons="False"/>
        </Setter.Value>
</Setter>

点击此处获取更多信息的MSDN链接

请注意,WindowChrome类是.NET 4.5 Framework的一部分。对于.NET 4.0用户,请查看archive.msdn.microsoft.com/WPFShell


1
谢谢您的回复。我不知道System.Windows.Shell命名空间,这本来可以节省我很多时间。 - Cris McLaughlin

9

我在另一篇帖子中写了一个解决方案,你可以调整窗口大小,需要使用.NET 4.5或WPFShell。

你也可以直接将WindowChrome代码放在MainWindow.xaml上,这样就可以完美地工作,而不需要放置setter。

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Concursos"
    mc:Ignorable="d"
    Title="Concuros" Height="350" Width="525"
    WindowStyle="None"
    WindowState="Normal" 
    ResizeMode="CanResize"
    >
<WindowChrome.WindowChrome>
    <WindowChrome 
        CaptionHeight="0"
        ResizeBorderThickness="5" />
</WindowChrome.WindowChrome>

    <Grid>

    <YOUR CODE HERE

</Grid>

您可以在这里查看完整的帖子。

解决方案

以下是改进前和改进后的情况:

挑战 解决方案


2

这是一个愚蠢的错误。在返回结果之前,我忘记添加handled = true;。现在窗口正常运行了。请注意,如果将ResizeMode设置为NoResize,则此代码将完全无效。


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