Wpf自定义窗口,集成了皮肤,配色方案和透明度管理(一)
新的一年就快到了,忙碌的一年就要过去了,提前预祝大家新年快乐,同时恭喜我的女朋友在过年回家之前成功买到了车票,哈哈.好了,进入正题.
先看效果图
这个是使用的固定大小窗口模板,后面蓝色的是我得桌面哦.

看到讨厌的window logo了吧

再来一张,全裸的,呵呵.
发现我最小化按钮和关闭按钮位置没对好--!. 大家看看样子就好了.最后一张,改变大小了的... 这个貌似看不到什么效果了.
上面的最小化,最大化和关闭按钮不是在改变窗口大小过程中被挡住了,是我在这个窗口模板中没加如控制按钮:)
好吧,下面是... 楼主我一会还有事, 就不说思路了,直接上关键代码说明吧.
代码
<ResourceDictionary x:Class="net.Library.Resources.CodeBehind.WindowTemplateCode"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:net.Library.Resources.Converters">
<converters:OpacityMaskConverter x:Key="_opacityMaskConv"/>
<ControlTemplate x:Key="_fixedWndTemplate" TargetType="{x:Type Window}">
<Border Name="_wndContent" CornerRadius="6" Padding="0" Margin="2" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Border.Effect>
<DropShadowEffect ShadowDepth="0" BlurRadius="6" Opacity="0.6" />
</Border.Effect>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Grid.RowSpan="2" Fill="{TemplateBinding Background}">
<Rectangle.OpacityMask>
<SolidColorBrush Color="{Binding WindowOpacity, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Converter={StaticResource _opacityMaskConv}}"/>
</Rectangle.OpacityMask>
</Rectangle>
<TextBlock Margin="4,0,0,0" Grid.Row="0" Text="logo" />
<TextBlock Margin="50,0,0,0" Grid.Row="0" Text="{TemplateBinding Title}" Background="Transparent"/>
<Border Grid.Row="0" CornerRadius="6,6,0,0" ClipToBounds="True" Background="#01ffffff" MouseLeftButtonDown="OnCaptionMouseDown">
</Border>
<Image Grid.Row="0" HorizontalAlignment="Right" Margin="0,0,40,0" Source="pack://Application:,,,/Resources;component/Images/min.png" MouseLeftButtonDown="OnMinimizeWindow"/>
<Image Grid.Row="0" HorizontalAlignment="Right" Margin="0" Source="pack://Application:,,,/Resources;component/Images/close.png" MouseLeftButtonDown="OnCloseWindow"/>
<ContentPresenter Grid.Row="1" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
</Grid>
</Border>
</ControlTemplate>
<!--固定大小的窗口样式规则-->
<Style x:Key="window@2" TargetType="{x:Type Window}">
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="BorderBrush" Value="#bfccd9"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template" Value="{StaticResource _fixedWndTemplate}"/>
</Style>
这个就是固定大小的窗口模板的主要代码了,注意名称空间中的x:Class 是为了处理最大最小化,拖放,改变大小等功能加的处理类.xmlns:converters定义了模板中需要使用的转换器.
代码
public static class ThemeManager
{
private static byte WIN_OPACITY = 0xff;
private static SeeyaSkin _curSkin;
private static SeeyaColorScheme _curColorScheme;
public static byte Opacity
{
get { return WIN_OPACITY; }
set
{
WIN_OPACITY = (value > 1)? value : (byte)1;
foreach (Window window in Application.Current.Windows)
{
RsitWindow rsitWin = window as RsitWindow;
if (null != rsitWin)
rsitWin.WindowOpacity = WIN_OPACITY;
}
}
}
public static void ChangeSkin(string skinId)
{
if (null != _curSkin)
Application.Current.Resources.MergedDictionaries.Remove(_curSkin.ResourceObject);
_curSkin = SeeyaThemes.GetSkin(skinId);
SetColorScheme(_curSkin.SchemeId);
Application.Current.Resources.MergedDictionaries.Add(_curSkin.ResourceObject);
}
public static void SetColorScheme(string schemeId)
{
if (null != _curColorScheme)
Application.Current.Resources.MergedDictionaries.Remove(_curColorScheme.ResourceObject);
_curColorScheme = SeeyaThemes.GetColorScheme(schemeId);
Application.Current.Resources.MergedDictionaries.Add(_curColorScheme.ResourceObject);
}
}
这里是控制皮肤,配色方案和透明度部分的主要代码了.
先贴这么多.下次再补上源代码和实现要点.
大家新年快乐!

浙公网安备 33010602011771号