WPF自定义窗体(一)(WindowChrome类)
应为这给类的功能实际上是在原有窗体上覆盖了一层,和装饰器有的类似;
使用WPF做开发我想没几个人用默认的窗体吧?
一般都使用:
<Window x:Class="FamilySystem.Client.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" xmlns:mui="http://firstfloorsoftware.com/ModernUI" xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell" xmlns:local="clr-namespace:FamilySystem.Client" Title="MainWindow" Height="600" Width="800" WindowStyle="None" AllowsTransparency="True"> <Grid> </Grid> </Window>
得到无边框的窗体然后再模仿系统的窗体来实现;
WindowStyle="None" AllowsTransparency="True">
<!--在使用动画的时候性能会下降,要是用户GPU不好的话还会卡,所以我一般都不使用,后面说下我自己开发中用的方法-->
接下来说说我自己使用的方法:
刚刚开始在用的时候发现按网上的方法使用会造成窗体显示不全;
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:shell="clr-namespace:Microsoft.Windows.Shell;assembly=Microsoft.Windows.Shell" x:Class="Test1.MainWindow" x:Name="Window" Title="MainWindow" Width="640" Height="480"> <shell:WindowChrome.WindowChrome> <shell:WindowChrome GlassFrameThickness="1" ResizeBorderThickness="1" CaptionHeight="33" CornerRadius="0"/> </shell:WindowChrome.WindowChrome> <Grid > </Grid> </Window>
这样设置过后,里面的Grid就覆盖到整个窗体了。
后来研究了下Microsoft.Windows.Shell这个命名空间
有一个类
| SystemParameters2 | 包含可用来查询窗口的非工作区的系统设置的属性。 |
中有个属性
| Current |
获取 SystemParameters2 类的实例。 |
得到了它的实例后再获取工作区的大小
| WindowNonClientFrameThickness | 获取窗体非工作区的大小。 |
现在Grid就是窗体了,把获得的工作区的Thickness赋值给Grid的Margin属性
Margin="{Binding Source={x:Static shell:SystemParameters2.Current}, Path=WindowNonClientFrameThickness}"
再试试完全达到要求不用再担心在显示不全了。
未完待续……
作者:JK6830020 出处:(http://www.cnblogs.com/YanJingEnious) 版权声明:本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任。
浙公网安备 33010602011771号