【问题标题】:WPF Window fixed width mouse cursorWPF Window 固定宽度鼠标光标
【发布时间】:2014-04-08 09:06:35
【问题描述】:

我正在构建一个 WPF 4.5 应用程序,它具有使用户能够“锁定”和“解锁”应用程序高度的控件。

为了锁定高度,我关注这个StackOverflow answer regarding setting the MinHeight and MaxHeight to the same value

为了解锁高度,我设置了MinHeight=0MaxHeight=double.PositiveInfinity

这一切似乎都运行良好。

我遇到的无法解决的问题是,当高度“锁定”时,当我将鼠标悬停在应用程序窗口的右边缘时,光标变成了水平调整大小光标。

有没有办法可以禁用它以使光标保持为 WPF 中的常规指针?

我使用的是 WPF 4.5。

我看到这篇文章的答案显示了如何在 Win32 中执行此操作:WPF: Make window unresizeable, but keep the frame?

这篇文章已有 3 年多的历史了,我只是想知道(希望)WPF 从那时起可能已经发展了。

非常感谢您!

菲利普

【问题讨论】:

  • 您的应用在启动时会启动一个窗口吗?
  • @TheRedLou 是的。我的App.xaml 指定StartupUri="MainWindow.xaml",这是一个Window。
  • @TheRedLou 嗯,这是很好的信息,但它并没有解决我的问题。原因是我无法将其设置为 NoResize。我并没有完全禁用调整大小,我只想禁用水平调整大小或垂直调整大小(但不能同时禁用两者)。不过谢谢你,我确实赞成你的回答。

标签: c# wpf resize window-resize


【解决方案1】:

您需要设置 MinWidth = MaxWidth = Width = 您想要的宽度,如 StackOverflow answer regarding setting the MinHeight and MaxHeight to the same value 中所述。

此外,您需要为您的窗口挂钩 winproc 并处理 WM_NCHITTEST 消息。

#region Vertical Resize Only

// ReSharper disable InconsistentNaming
private const int WM_NCHITTEST = 0x0084;
private const int HTBORDER = 18;
private const int HTBOTTOM = 15;
private const int HTBOTTOMLEFT = 16;
private const int HTBOTTOMRIGHT = 17;
private const int HTLEFT = 10;
private const int HTRIGHT = 11;
private const int HTTOP = 12;
private const int HTTOPLEFT = 13;
private const int HTTOPRIGHT = 14;

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr DefWindowProc(
    IntPtr hWnd,
    int msg,
    IntPtr wParam,
    IntPtr lParam);

// ReSharper restore InconsistentNaming

#endregion Vertical Resize Only

public CanConfigurationDialog()
{
    InitializeComponent();
    Loaded += MainWindowLoaded;
}


#region Vertical Resize Only

private void MainWindowLoaded(object sender, RoutedEventArgs e)
{
    try
    {
        // Obtain the window handle for WPF application
        var mainWindowPtr = new WindowInteropHelper(this).Handle;
        var mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
        mainWindowSrc?.AddHook(WndProc);
    }
    catch (Exception)
    {
        ;
    }
}

private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    // Override the window hit test
    // and if the cursor is over a resize border,
    // return a standard border result instead.
    if (msg == WM_NCHITTEST)
    {
        handled = true;
        var htLocation = DefWindowProc(hwnd, msg, wParam, lParam).ToInt32();
        switch (htLocation)
        {
            case HTTOP:
            case HTTOPLEFT:
            case HTTOPRIGHT:
                htLocation = HTTOP;
                break;

            case HTBOTTOM:
            case HTBOTTOMLEFT:
            case HTBOTTOMRIGHT:
                htLocation = HTBOTTOM;
                break;

            case HTLEFT:
            case HTRIGHT:
                htLocation = HTBORDER;
                break;
        }

        return new IntPtr(htLocation);
    }

    return IntPtr.Zero;
}

#endregion Vertical Resize Only

这将阻止显示水平调整大小光标! Q.E.D.

【讨论】:

    【解决方案2】:

    在您的启动窗口 (MainWindow.xaml) 上,尝试为 Window 的 ResizeMode 属性进行绑定,然后在您不希望它可调整大小时将其修改为“NoResize”。要使其可调整大小,请将其更改为“CanResize”。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 2010-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      相关资源
      最近更新 更多