【问题标题】:WPF RibbonWindow + Ribbon = Title outside screen?WPF RibbonWindow + Ribbon = 屏幕外的标题?
【发布时间】:2012-09-16 01:20:28
【问题描述】:

我正在尝试将Ribbon 控制与RibbonWindow 结合使用,但是即使在微不足道的实验中它们也失败了。

  1. 创建了新的 WPF 应用程序
  2. 将代码更改为example from MSDN
  3. 添加了对System.Windows.Controls.Ribbon 的引用并去掉了ribbon: 前缀(为什么示例已过时?)。
  4. 添加了两个图标(16x16 和 32x32)。
  5. 执行了应用程序并看到了这个(记事本供参考):

我已经看到了很多问题:

  1. 边框很小。普通窗口的边框很大,WPF 功能区应用程序的边框很小。标题高度也更小。
  2. 边界模糊。当一个普通窗口被聚焦时,它的边框是黑色的。 WPF 应用的边框是灰色的(在角落可以看到黑色;在边框上绘制了一些东西?)。
  3. 应用程序图标放错了位置。它粘在左上角。
  4. 应用程序标题放错了位置。它粘在顶部。

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

按钮在工具栏之外。

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

标题的一半消失在屏幕之外(从技术上讲,窗口在屏幕之外每边各有 8 个像素,但其他应用不会因此而混淆)。

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

有机会解决这个问题吗?

【问题讨论】:

标签: c# .net wpf windows ribbon


【解决方案1】:

真正的问题

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

但是,此方法的行为会根据可执行 PE 标头中设置的子系统版本而改变。如果只为 Windows Vista 和更高版本(版本 >= 6.0)编译,它将返回比为旧操作系统编译的更细的边框。 More information on this in this SO answer.

针对 .NET 4.5 进行编译时,C# 编译器将此版本设置为 6.0,因为 .NET 4.5 不能在 XP 上使用。但是,WindowChrome 类似乎依赖于旧行为,因此无法在 Windows Vista 和 7 上正确计算玻璃尺寸。

解决方案

使用 .NET 4

您可以针对 .NET 4 进行编译,以强制编译器使用 4.0 作为其子系统版本值。功能区可用于 WPF 4,为 separate download。请注意,即使使用此解决方案,您也应取消选中项目属性中的“启用 Visual Studio 托管进程”以进行调试。否则,将使用 vshost.exe 进程,其子系统版本为 6.0。

更改子系统版本

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

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

忽略系统

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

补一个bug

Connect 上存在一个关于 change in behavior of GetSystemMetrics 的错误,但这一切都归结为子系统版本,因此从 Microsoft 的角度来看,它是一项功能。然而,WindowChrome 类确实应该被修复以在 Vista/7 下正常工作,特别是因为它现在是在 .NET 4.5 中构建的。

【讨论】:

  • 谢谢,但是只使用一个控件太麻烦了。使用旧框架显然不是一种选择。切换到旧系统可能会引起各种麻烦(考虑到我打算使用Vista / 7的功能)。猜测系统设置会导致不同操作系统版本出现问题。根据我的经验(修复 URL 解析需要数年时间......),希望微软能尽快修复一个错误是很愚蠢的。我正在试用 Fluent Ribbon,到目前为止,它工作得更好(它确实有错误,但至少基本功能可以正常工作)。啊!
  • 如果您使用 Ribbon 正在使用的 WindowChrome 实例并将 ResizeBorderThickness 更改为正确的值,这应该可以正常工作。它不必进行硬编码,您只需获取适当的系统指标即可。我不确定默认功能区模板用于所有内容,因此可能需要更改其他几个属性。也就是说,如果您对 Fluent 版本感到满意,那么它也可以使用 :)
  • 对于 C#,您可以设置编译器 /subsystemversion 开关。 (msdn.microsoft.com/en-us/library/vstudio/hh965708.aspx) 我在项目文件 5.01 中添加了一个属性,该属性错误地表明代码可以在 Windows XP 上运行。然后使用垫片,功能区窗口看起来不错。
  • @Olly 这真是太棒了!用那个编辑了我的答案,看起来很完美。
  • 有人用 VS2012 Update2 试过这个吗? 5.01 或更改 WindowChrome 上的任何属性似乎都没有任何效果。
【解决方案2】:

对于阅读此问题的任何人,我将自己回答。忘记可怕的捆绑功能区控件并使用其他东西。在这里寻找一些替代方案:What is the Best WPF Ribbon Control Suite?(就像所有好问题一样,虽然它已关闭)。

到目前为止,Fluent Ribbon Control Suite 对我来说似乎是最好的免费选择。基本功能正常工作(边框和最大化没有问题,调整窗口并不慢等)。它具有 Office 样式并在玻璃被禁用时保留它们(这意味着您不会在 Metro 中看到 Windows9x-ish 窗口)。它的界面(后台、QAT)更像Office 2010。

也许在遥远的将来,微软会修复它的功能区,但现在,寻找替代方案。

【讨论】:

    【解决方案3】:

    这是另一个 WorkAround,非常简单的方法。 只需在工具栏上添加一个负边距。 您需要保留原始的 Window 类而不是 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">
    

    现在,当您最大化窗口时,一切正常

    【讨论】:

    • 这种解决方法对我来说是最好的,也就是说,如果您不需要标题栏上的特殊功能区按钮。
    • 是的,如果您不需要 QuickAccessToolBar,这是一个快速简便的解决方案。
    • 应针对不同的显示配置计算保证金。在 Windows 10 中尝试 100%、125%、150% 的比例,你会发现它是不同的。请参阅下面我的答案以获取正在计算的代码
    • @astef - 此解决方案适用于 100%、125%、150%,它已经过测试并且适用于所有情况。
    • 对不起,我以为你是用margin来解决window chrome定位问题,这是这个问题的原始问题。但是似乎您只是使用标准窗口并将功能区的一部分隐藏在它下面。顺便说一句,如果您逐像素检查,标准窗口镶边在调整大小后并不理想
    【解决方案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>
    

    【讨论】:

    • 对不起,我有一个问题:我应该把样式放在哪里?
    【解决方案5】:

    这不是一个解决方案,甚至可能不是一个变通方案,而是一个糟糕的 hack,我希望只使用很短的时间,直到问题在框架中得到解决。

    代码主要是从这个问题https://stackoverflow.com/a/8082816/44726复制+粘贴

    我更改了允许的屏幕位置,这似乎有助于解决问题,而不是解决问题。

    后面的代码中调用是这样的

            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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 2011-12-18
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多