WPF RibbonWindow + Ribbon = 标题超出屏幕?

32
我正在尝试将Ribbon控件与RibbonWindow相结合,但是即使在微不足道的实验中也失败了。
  1. 创建新的WPF应用程序
  2. 将代码更改为MSDN示例
  3. 添加对System.Windows.Controls.Ribbon的引用并消除ribbon:前缀(为什么示例过时了?)。
  4. 添加两个图标(16x16和32x32)。
  5. 执行应用程序并看到此内容(以记事本为参考):

我已经看到很多问题:

  1. 边框很小。普通窗口具有大边框,WPF Ribbon应用程序具有小边框。标题高度也较小。
  2. 边框模糊。当普通窗口获得焦点时,其边框为黑色。WPF应用程序的边框为灰色(在角落处可以看到黑色;某些内容绘制在边框上?)。
  3. 应用程序图标位置错误。它粘贴在左上角。
  4. 应用程序标题位置错误。它粘贴在顶部。

让我们将工具栏移动到底部。现在我们看到了这个:

按钮在工具栏外面。

最后,让我们将窗口最大化:

窗口的一半标题消失在屏幕之外(从技术上讲,窗口的每一侧都超出了8个像素,但其他应用程序并没有被这个问题困扰)。

我正在使用Windows 7,Aero,单个显示器,没有什么特别的。我不敢在Windows 8上测试应用程序...

有什么办法可以解决这个问题吗?


1
微软 Bug: http://connect.microsoft.com/VisualStudio/feedback/details/775972/wpf-ribbon-window-the-border-is-too-thin - jbe
1
链接已经失效:“您访问的是Microsoft Connect门户网站。经过近10年的运营,Microsoft Connect已被淘汰并被新工具和服务所取代。” - AgostinoX
5个回答

31

真正的问题

在底层,WindowChrome 类将其 ResizeBorderThickness 绑定到 SystemParameters.WindowResizeBorderThickness,后者则使用 Win32 API GetSystemMetrics 来确定系统边框大小。

然而,该方法的行为取决于可执行文件 PE header 中设置的子系统版本。如果仅编译用于 Windows Vista 及更高版本(版本 >= 6.0),它将返回比旧操作系统编译更薄的边框。有关此信息,请参阅此 SO 答案。

当针对 .NET 4.5 进行编译时,C# 编译器将此版本设置为 6.0,因为 .NET 4.5 不能在 XP 上使用。然而,WindowChrome 类似乎依赖于传统的行为,因此无法在 Windows Vista 和 7 上正确计算玻璃大小。

解决方案

使用.NET 4

您可以编译针对.NET 4,强制编译器将其子系统版本值设置为4.0。Ribbon 可在 WPF 4 中作为单独的下载使用。请注意,即使使用此解决方案,您也应该在项目属性中取消选中“启用 Visual Studio 托管进程”以进行调试。否则,将使用 vshost.exe 进程,该进程标记有子系统版本6.0。

更改子系统版本

编辑:Olly 在评论中提供了一种方法:

在项目文件中添加一个属性 <subsystemversion>5.01</subsystemversion>,这会错误地表示代码可以在 Windows XP 上运行。

忽略系统

您可以更改窗口上的WindowChrome.WindowChrome附加属性,并使用您想要的值,从而完全忽略系统值。您永远不应该这样做,但是您可以这样做。

提交错误

关于GetSystemMetrics的行为变化,Connect上已经有一个现有的bug,但这归结于子系统版本,因此从微软的角度来看,这实际上是一种特性。然而,WindowChrome类应该真正被修复以在Vista/7下正确工作,特别是它现在内置于.NET 4.5中。


谢谢,但为了使用一个控件而费这么大的劲显然不值得。使用旧框架显然不是一个选项。转换到旧系统可能会引起各种问题(考虑到我计划使用Vista/7的功能)。猜测系统设置将会在不同的操作系统版本中引起问题。基于我的经验,希望微软很快修复一个错误是很愚蠢的(它花了几年时间来修复URL解析...)。我正在尝试使用Fluent Ribbon,到目前为止,它工作得更好(它确实有一些错误,但至少基本功能可以正常工作)。啊! - Athari
如果您获取Ribbon正在使用的WindowChrome实例并将ResizeBorderThickness更改为正确的值,则应该可以正常工作。它不必是硬编码的,而是可以获取适当的系统指标。我不确定默认的Ribbon模板在所有方面都使用了什么,因此可能需要更改其他一些属性。话虽如此,如果您对流畅版本感到满意,那也可以 :) - Joe Castro
5
对于C#,您可以设置编译器的/subsystemversion开关。(http://msdn.microsoft.com/en-us/library/vstudio/hh965708.aspx) 我在项目文件中添加了一个属性<subsystemversion>5.01</subsystemversion>,虚假地表示代码可以在Windows XP上运行。然后使用Shim,带菜单栏的窗口看起来还不错。 - Olly
6
有人尝试在VS2012 Update2上使用吗?无论是 <SubsystemVersion>5.01</SubsystemVersion> 还是更改 WindowChrome 上的任何属性都似乎没有任何效果。 - Marcin Wisnicki
我之前一直使用<subsystemversion>5.01</subsystemversion>的解决方案,直到我升级到Windows 10。在Windows 10中,这种解决方法会在窗口上添加丑陋的边框。如果您不需要标题栏上的特殊功能区按钮,则Yannick KcinnaY的解决方法更好。这是链接: https://dev59.com/V2cs5IYBdhLWcg3w0HEa#27730148 - Vladimir
显示剩余2条评论

12

这里有另一个解决方案,非常简单易行。 只需在工具栏上添加负边距即可。 您需要保留原始的窗口类而不是RibbonWindow!

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Application Name" Height="350" Width="525" Loaded="Window_Loaded" SizeChanged="Window_SizeChanged">

只需将此边距添加到功能区标题即可。
<Ribbon Title="" Foreground="#333333" Margin="0,-22,0,0">

现在当您最大化窗口时,所有内容都会保持正确。

这个解决方法对我来说是最好的,但前提是你不需要在标题栏上有特殊的功能按钮。 - Vladimir
2
是的,如果您不需要快速访问工具栏,这是一个快速且简单的解决方案。 - dotnetzen
1
应该为不同的显示配置计算边距。在Windows 10中尝试使用100%、125%、150%的缩放比例,您会发现它是不同的。请参见下面的代码,它正在计算它。 - astef
@astef - 这个解决方案已经测试过,可以在100%、125%和150%的情况下正常工作。 - Yannick Turbang
使用Window类时不需要负边距。标题是固定的,但在运行时会出现绑定错误。 - Toni
显示剩余3条评论

9
对于阅读此问题的任何人,我自己回答。忘记那个可怕的捆绑式缩略带控件,使用其他东西。在这里寻找一些替代方案: 什么是最好的WPF缩略带控件套件?(像所有好问题一样,它已关闭)。
到目前为止, Fluent Ribbon Control Suite 对我来说似乎是最好的免费选择。基本功能正常运行(没有边框和最大化的问题,调整窗口大小也不会慢得要死)。它具有Office风格,并且如果禁用玻璃,则保留它们(这意味着您在Metro中不会看到Windows9x-ish窗口)。其界面(后台,QAT)更像Office 2010。
也许在遥远的将来,微软会修复其缩略带,但现在,寻找替代方案。

如果您不需要将按钮放置在标题栏中,则可以使用普通的窗口,而不是功能区RibbonWindow。 - Double_A

4

我曾遇到在RibbonWindow中设置标题的问题,我通过设置RibbonTitlePanel中TextBlock的全局样式来解决。

    <Style TargetType="{x:Type TextBlock}"> 
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type primitives:RibbonTitlePanel}},Path=Visibility}" Value="Visible"></Condition>
                <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type RibbonWindow}},Path=WindowState}" Value="Maximized"></Condition>
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter Property="VerticalAlignment" Value="Center"></Setter>
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

3
抱歉,我有一个问题:样式应该放在哪里? - Xam

1
这不是一个解决方案,甚至可能不算一个变通方法,而是一个较差的hack。我希望只在问题在框架中被修复之前短暂使用它。
代码主要是从这个问题 https://dev59.com/M2w15IYBdhLWcg3wA28A#8082816 中复制的。
我已经更改了允许的屏幕位置,这似乎有助于问题,但并没有完全解决。
代码后台调用方式如下:
        InitializeComponent();
        RibbonWindowService.FixMaximizedWindowTitle(this);


public static class RibbonWindowService
{
    public static void FixMaximizedWindowTitle(Window window)
    {
        window.SourceInitialized += WinSourceInitialized;
    }

    [DllImport("user32")]
    internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);

    [DllImport("User32")]
    internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);

    private static IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        switch (msg)
        {
            case 0x0024:
                WmGetMinMaxInfo(hwnd, lParam);
                handled = true;
                break;
        }

        return (IntPtr)0;
    }

    private static void WmGetMinMaxInfo(IntPtr hwnd, 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;
        IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);

        if (monitor != IntPtr.Zero)
        {
            MONITORINFO monitorInfo = new MONITORINFO();
            GetMonitorInfo(monitor, monitorInfo);
            RECT rcWorkArea = monitorInfo.rcWork;
            RECT rcMonitorArea = monitorInfo.rcMonitor;

            // Offset top and left 1 pixel improves the situation
            rcMonitorArea.top += 1;
            rcMonitorArea.left += 1;

            mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
            mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
            mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
            mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
        }

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

    private static void WinSourceInitialized(object sender, EventArgs e)
    {
        IntPtr handle = (new WinInterop.WindowInteropHelper((Window)sender)).Handle;
        WinInterop.HwndSource.FromHwnd(handle).AddHook(WindowProc);
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct MINMAXINFO
    {
        public POINT ptReserved;
        public POINT ptMaxSize;
        public POINT ptMaxPosition;
        public POINT ptMinTrackSize;
        public POINT ptMaxTrackSize;
    };

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        /// <summary>
        /// x coordinate of point.
        /// </summary>
        public int x;

        /// <summary>
        /// y coordinate of point.
        /// </summary>
        public int y;

        /// <summary>
        /// Construct a point of coordinates (x,y).
        /// </summary>
        public POINT(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }

    [StructLayout(LayoutKind.Sequential, Pack = 0)]
    public struct RECT
    {
        /// <summary> Win32 </summary>
        public int left;

        /// <summary> Win32 </summary>
        public int top;

        /// <summary> Win32 </summary>
        public int right;

        /// <summary> Win32 </summary>
        public int bottom;

        /// <summary> Win32 </summary>
        public static readonly RECT Empty = new RECT();

        /// <summary> Win32 </summary>
        public int Width
        {
            get { return Math.Abs(right - left); } // Abs needed for BIDI OS
        }

        /// <summary> Win32 </summary>
        public int Height
        {
            get { return bottom - top; }
        }

        /// <summary> Win32 </summary>
        public RECT(int left, int top, int right, int bottom)
        {
            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;
        }

        /// <summary> Win32 </summary>
        public RECT(RECT rcSrc)
        {
            left = rcSrc.left;
            top = rcSrc.top;
            right = rcSrc.right;
            bottom = rcSrc.bottom;
        }

        /// <summary> Win32 </summary>
        public bool IsEmpty
        {
            get
            {
                // BUGBUG : On Bidi OS (hebrew arabic) left > right
                return left >= right || top >= bottom;
            }
        }

        /// <summary> Return a user friendly representation of this struct </summary>
        public override string ToString()
        {
            if (this == Empty)
            {
                return "RECT {Empty}";
            }
            return "RECT { left : " + left + " / top : " + top + " / right : " + right + " / bottom : " + bottom + " }";
        }

        /// <summary> Determine if 2 RECT are equal (deep compare) </summary>
        public override bool Equals(object obj)
        {
            if (!(obj is Rect))
            {
                return false;
            }
            return (this == (RECT)obj);
        }

        /// <summary>Return the HashCode for this struct (not garanteed to be unique)</summary>
        public override int GetHashCode()
        {
            return left.GetHashCode() + top.GetHashCode() + right.GetHashCode() + bottom.GetHashCode();
        }

        /// <summary> Determine if 2 RECT are equal (deep compare)</summary>
        public static bool operator ==(RECT rect1, RECT rect2)
        {
            return (rect1.left == rect2.left && rect1.top == rect2.top && rect1.right == rect2.right && rect1.bottom == rect2.bottom);
        }

        /// <summary> Determine if 2 RECT are different(deep compare)</summary>
        public static bool operator !=(RECT rect1, RECT rect2)
        {
            return !(rect1 == rect2);
        }
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public class MONITORINFO
    {
        /// <summary>
        /// </summary>            
        public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));

        /// <summary>
        /// </summary>            
        public RECT rcMonitor = new RECT();

        /// <summary>
        /// </summary>            
        public RECT rcWork = new RECT();

        /// <summary>
        /// </summary>            
        public int dwFlags = 0;
    }
}

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