【问题标题】:WPF Borderless window resizeWPF 无边框窗口调整大小
【发布时间】:2013-07-12 18:59:56
【问题描述】:

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

我的所有 WndProc 消息和结果都有一个 NativeMethods 类:

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。我做错了什么?

【问题讨论】:

  • 为什么不使用ResizeMode = "CanResizeWithGrip"?您正在尝试实现这种效果(只是在没有Grip 的情况下围绕窗口调整大小)?
  • 因为我仍然希望能够像正常功能一样从任何角落或侧面调整窗口大小。我发现了这个问题,我现在发布答案。

标签: c# .net wpf window-resize mouse-cursor


【解决方案1】:

也许分配 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">

在隐藏的代码中,您必须为 window 设置 Window Chrome。你可以这样做:

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

您可以将 setter 用于窗口样式,例如:

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

MSDN link for more information

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

【讨论】:

  • 感谢您的回复。我不知道 System.Windows.Shell 命名空间可以为我节省不少时间。
【解决方案2】:

我在另一篇文章中写了一个解决方案,可以调整窗口大小,需要使用.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>

你可以去这里查看完整的帖子。

Solution

这是之前和之后

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2011-09-22
      • 1970-01-01
      • 2021-06-18
      • 2013-06-19
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多