walterlv / BlogComments

3 stars 0 forks source link

post/wpf-simulate-native-window-title-bar-buttons #17

Open utterances-bot opened 5 years ago

utterances-bot commented 5 years ago

WPF 应用完全模拟 UWP 的标题栏按钮 - walterlv

WPF 应用完全模拟 UWP 的标题栏按钮

https://blog.walterlv.com/post/wpf-simulate-native-window-title-bar-buttons.html

AkiraVoid commented 5 years ago

为什么我的grid会占用标题栏?

jyh583854536 commented 5 years ago

窗体图标怎么显示,请大神指点下。

walterlv commented 5 years ago

@AkiraVoid 如果使用了 WindowChrome,那么窗口的所有客户区内容就会覆盖到非客户区,也就是你看到的 Grid 占用了标题栏的现象。如果要做不占用标题栏的,需要在 Grid 上写一个 Margin。在我的博客的设置下,Margin 设置为 0 32 0 0 最佳。

walterlv commented 5 years ago

@jyh583854536 表示这个还没有怎么研究,不过可以考虑使用一个 Image,将 Source 绑定到 WindowIcon 属性上。(话说模拟 UWP 应用的话,就没有图标了)

jyh583854536 commented 5 years ago

谢谢

jyh583854536 commented 5 years ago

为什么标题栏的三个按钮点击不起作用?

jyh583854536 commented 5 years ago

弄错了,可以,麻烦大神了

walterlv commented 5 years ago

@jyh583854536 哈哈

MingsonZheng commented 4 years ago

牛批

walterlv commented 4 years ago

@MINGSON666 哇

CamelKing1997 commented 4 years ago

苦恼我好久的问题,无比感谢大神

melodicule commented 3 years ago

同样遇到标题栏按钮不起作用的问题,百思不得其解,有人知道怎么解决吗

melodicule commented 3 years ago

看来被子控件遮挡是无解的,如果要把布局拓展到标题栏,自己仿照写一个窗口才是王道

walterlv commented 3 years ago

@melodicule

如果是希望在标题栏区域的控件能交互,在控件上面设置 WindowChrome.IsHitTestVisibleInChrome="True" 即可。

如果是希望布局扩展到标题栏后,标题栏原本的按钮还能交互,就需要修改 WindowBackground 设置为 {x:Null}(注意不是 Transparent,然后子控件还要注意不要覆盖掉标题栏按钮(你可以覆盖左边,但请在右边留下空白空间出来)。

不明白可以继续沟通。

jzhang000 commented 3 years ago

为什么我的窗口非最大化的时候有个白色边框

cugbcc commented 3 years ago

Walter前辈您好,我想请教一下,按照您的代码自定义的窗体,在125%的DPI下,三大键的图标也随之放大并且不能与实体像素对齐,因而产生发虚的情况,设置SnapsToDevicePixels后出现最大化图标线条粗细不一的情况,但诸如Edge、各种UWP等应用并不会,想请教一下有解决方案吗,或者想请教一下Win32 API修改三大键背景色的方法。谢谢!

walterlv commented 3 years ago

@cugbcc 我也发现了这个问题,感谢!我研究下调整样式。另外,我没找到 Win32 API 修改的方法。

cugbcc commented 3 years ago

@cugbcc 我也发现了这个问题,感谢!我研究下调整样式。另外,我没找到 Win32 API 修改的方法。

歪打正着有了解决方法:在制作按钮时不使用MDL字体或者path,转而使用border来绘制内容,从而像素就可以对齐到显示器像素了。

1357310795 commented 2 years ago

Walter大神好,请问用了这个Window.Universal.xaml之后,输出报错

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=(0).InactiveForegroundColor; DataItem=null; target element is 'SolidColorBrush' (HashCode=36518691); target property is 'Color' (type 'Color')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=(0).ButtonForegroundColor; DataItem=null; target element is 'SolidColorBrush' (HashCode=20490669); target property is 'Color' (type 'Color')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=(0).ButtonInactiveForegroundColor; DataItem=null; target element is 'SolidColorBrush' (HashCode=20226985); target property is 'Color' (type 'Color')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=(0).ButtonHoverForeground; DataItem=null; target element is 'SolidColorBrush' (HashCode=35025494); target property is 'Color' (type 'Color')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=(0).ButtonHoverBackground; DataItem=null; target element is 'SolidColorBrush' (HashCode=21522166); target property is 'Color' (type 'Color')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=(0).ButtonPressedForeground; DataItem=null; target element is 'SolidColorBrush' (HashCode=9886408); target property is 'Color' (type 'Color')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=(0).ButtonPressedBackground; DataItem=null; target element is 'SolidColorBrush' (HashCode=37564172); target property is 'Color' (type 'Color')

找了StackOverFlow上的解决方法,加上x:Name或者加上FallbackValue,仍然报错😭😭😭……怎么办?

walterlv commented 2 years ago

@1357310795 目前我也遇到了这个绑定错误,不过不用管它后面就正常了……仍未找到原因……

LHTXQ commented 2 years ago

我在Window.Universal.xaml里的textblock和三个button里添加了tooltip,鼠标移到控件上时,textblock不显示tooltip,其他三个按钮显示不到一秒就消失了,请问怎么解决? (目的是使title过长时通过tooltip显示完整的title)

walterlv commented 2 years ago

@LHTXQ 给 TextBlock 设置一下 WindowChrome.IsHitTestVisible 属性?

LHTXQ commented 2 years ago

谢谢,但设置了还是没用:

<TextBlock x:Name="TitleTextBlock" FontSize="12" Text="{TemplateBinding Title}" ToolTip="{TemplateBinding Title}" WindowChrome.IsHitTestVisibleInChrome="True" Margin="2 0 140 0" Width="auto" MaxWidth="200" HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="{StaticResource Brush.TitleBar.Foreground}" />

<StackPanel x:Name="TitleBarButtonPanel" Orientation="Horizontal" Margin="0 -1 0 0" HorizontalAlignment="Right">

<Button x:Name="MinimizeButton" ToolTip="{DynamicResource Minimize}" WindowChrome.IsHitTestVisibleInChrome="True" Content="&#xE921;" themes:UniversalWindowStyle.TitleBarButtonState="Minimized" />

其中窗口标题的ToolTip="{TemplateBinding Title}" WindowChrome.IsHitTestVisibleInChrome="True"和最小化按钮的ToolTip="{DynamicResource Minimize}" WindowChrome.IsHitTestVisibleInChrome="True"是我在你代码基础上添加的。

另外,窗口三个按钮ToolTip闪现问题还是没解决。

walterlv commented 2 years ago

@LHTXQ 你好,我可能需要更多的信息才能帮你找到问题:

直接用下面的代码就可以完成最原生风格的标题栏;如果想了解里面的实现,可以阅读里面的实现代码。

<wpfui:TitleBar
    Title="WPF UI - Editor"
    Grid.Row="0"
    Icon="pack://application:,,,/Assets/mwpf_icon.png" />

https://github.com/lepoco/wpfui

Neuropathy1 commented 1 year ago

将标题栏按钮的Panel.ZIndex="99"就可以设置窗体的背景颜色了,按钮还不会被遮挡

Neuropathy1 commented 1 year ago

吕毅大哥,请问一下为什么窗口在最大化过程中变大的区域是黑色的呢,有办法解决吗