C# WPF RibbonWindow+;Ribbon=屏幕外的标题?
我正在尝试将C# WPF RibbonWindow+;Ribbon=屏幕外的标题?,c#,.net,wpf,windows,ribbon,C#,.net,Wpf,Windows,Ribbon,我正在尝试将Ribbon控件与RibbonWindow结合使用,但即使在琐碎的实验中,它们也失败了 创建了新的WPF应用程序 将代码更改为 添加了对System.Windows.Controls.Ribbon的引用,并删除了Ribbon:前缀(为什么示例过时?) 添加了两个图标(16x16和32x32) 执行应用程序并看到以下内容(参考记事本): 我已经看到许多问题: 边界很小。普通窗口有一个大边框,WPF Ribbon应用程序有一个小边框。标题的高度也较小 边界模糊。当普通窗口聚焦时,其边框
Ribbon
控件与RibbonWindow
结合使用,但即使在琐碎的实验中,它们也失败了
System.Windows.Controls.Ribbon
的引用,并删除了Ribbon:
前缀(为什么示例过时?)ResizeBorderThickness
绑定到SystemParameters.WindowResizeBorderThickness
,然后使用Win32 API确定系统边框大小
但是,此方法的行为会根据可执行PE标头中设置的子系统版本而变化。如果仅为Windows Vista及更高版本(版本>=6.0)编译,它将返回比为旧操作系统编译时更薄的边框
在针对.NET 4.5进行编译时,C#编译器会将此版本设置为6.0,因为.NET 4.5不能在XP上使用。然而,WindowChrome
类似乎依赖于传统行为,因此无法在WindowsVista和Windows7上正确计算玻璃尺寸
解决
使用.NET4
您可以根据.NET 4进行编译,以强制编译器使用4.0作为其子系统版本值。功能区可作为一个组件用于WPF 4。请注意,即使使用此解决方案,出于调试目的,也应该在项目属性中取消选中“启用Visual Studio宿主进程”。否则,将使用vshost.exe进程,该进程标记为子系统版本6.0
更改子系统版本
编辑:奥利在评论中提供了一种方法:
在项目文件中添加属性
5.01
错误地表示
该代码可以在Windows XP上运行
忽略系统
您可以更改窗口上的WindowChrome.WindowChrome
attached属性,并使用所需的值,从而完全忽略系统值。你不应该那样做,但你可以
填补漏洞
在Connect上存在一个关于的bug,但这都归结为子系统版本,所以从Microsoft的角度来看,这是一个功能。然而,WindowChrome类确实应该在Vista/7下正常工作,特别是因为它现在内置于.NET 4.5中。对于任何阅读此问题的人,我自己来回答。忘记可怕的捆绑功能区控件,使用其他控件。在这里寻找一些备选方案:(就像所有好问题一样,它是封闭的) 到目前为止,看起来是我最好的免费选择。基本功能可以正常工作(边界和最大化没有问题,调整窗口大小也不慢,等等)。它有办公风格,如果玻璃被禁用,它会保留这些风格(这意味着你在Metro中看不到Windows9x-ish窗口)。它的界面(后台,QAT)更像Office2010
也许在遥远的将来,微软会修复它的Ribbon,但现在,寻找替代方案。这不是一个解决方案,甚至可能不是一个解决办法,而是一个拙劣的黑客,我希望在框架中解决问题之前只使用一段时间 代码主要是从这个问题复制+粘贴 我更改了允许的屏幕位置,这似乎有助于解决问题,而不是修复问题 调用在代码背后是这样的
InitializeComponent();
RibbonWindowService.FixMaximizedWindowTitle(this);
公共静态类RibbonWindowsService
{
公共静态无效FixMaximizedWindowTitle(窗口)
{
window.SourceInitialized+=WinSourceInitialized;
}
[DllImport(“user32”)]
内部静态外部布尔GetMonitorInfo(IntPtr hMonitor,MONITORINFO lpmi);
[DllImport(“User32”)]
内部静态外部IntPtr监视器FromWindow(IntPtr句柄、int标志);
私有静态IntPtr WindowProc(IntPtr hwnd、int msg、IntPtr wParam、IntPtr lParam、ref bool handled)
{
开关(msg)
{
案例0x0024:
WmGetMinMaxInfo(hwnd,LPRAM);
已处理=正确;
打破
}
返回(IntPtr)0;
}
私有静态void WmGetMinMaxInfo(IntPtr hwnd、IntPtr lParam)
{
MINMAXINFO mmi=(MINMAXINFO)Marshal.PtrToStructure(lParam,typeof(MINMAXINFO));
//调整最大尺寸和位置,以适合正确显示器的工作区域
int MONITOR\u DEFAULTTONEAREST=0x00000002;
IntPtr monitor=MonitorFromWindow(hwnd,monitor\u DEFAULTTONEAREST);
如果(监视器!=IntPtr.Zero)
{
MONITORINFO MONITORINFO=新的MONITORINFO();
GetMonitorInfo(监视器,monitorInfo);
RECT rcWorkArea=monitorInfo.rcWork;
RECT rcMonitorArea=monitorInfo.rcMonitor;
//上下偏移1个像素改善了这种情况
rcMonitorArea.top+=1;
rcMonitorArea.left+=1;
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;
}
}
<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">
<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>