WPF自定义控件(三)——Window

  一样!先来看看效果吧:

  

  怎么样?效果很好吧,而且不只是样式哟!所有系统窗体有的交互操作都可以实现!

  但可惜。。。有很多和系统API有关的东西本人了解得并不多,所以这个窗体是基于他人的成果上产生的。关于窗体交互的东西还是看看原作者的吧:

  http://blog.csdn.net/dlangu0393/article/details/12548731

  接下来说说我在这上面多加的功能:可设置标题栏可见性,可扩展标题栏下拉按钮,可全屏并拦截键盘,另外还多公布了一些属性

  再来一张图:

  

  这就是没有标题栏的,只是设置一个属性就可以了哟!

  废话不多说了!再来看看我代码,只发一些我加了的吧,上面的链接里有的我就不发了,想看完整的,之后我会发源码的。

  先是Xaml的:

  1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3     xmlns:ctrl="clr-namespace:KAN.WPF.XCtrl.Controls" >
  4     <Style x:Key="XWindowStyle" TargetType="ctrl:XWindow">
  5         <Setter Property="AllowsTransparency" Value="True"/>
  6         <Setter Property="Background" Value="Transparent"/>
  7         <Setter Property="WindowStyle" Value="None"/>
  8         <!--解决图片模糊-->
  9         <Setter Property="UseLayoutRounding" Value="True"/>
 10         <Setter Property="RenderOptions.BitmapScalingMode" Value="NearestNeighbor" />
 11         <Setter Property="TextOptions.TextFormattingMode" Value="Display" />
 12         <!--改字体-->
 13         <Setter Property="FontFamily" Value="Microsoft YaHei" />
 14         <Setter Property="Template">
 15             <Setter.Value>
 16                 <ControlTemplate TargetType="ctrl:XWindow">
 17                     <Border x:Name="bdrWindow" CornerRadius="5" Margin="8" Background="White" 
 18                             BorderBrush="#6A6A6A" BorderThickness="1" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"  >
 19                         <Border.Effect>
 20                             <DropShadowEffect BlurRadius="8" ShadowDepth="0" Color="#00000000"/>
 21                         </Border.Effect>
 22                         <Grid>
 23                             <Grid.RowDefinitions>
 24                                 <RowDefinition Height="Auto"/>
 25                                 <RowDefinition Height="*"/>
 26                             </Grid.RowDefinitions>
 27                             <Border Name="bdrTitleBar" Height="28" CornerRadius="5,5,0,0" Panel.ZIndex="10" >
 28                                 <Border.Background>
 29                                     <ImageBrush ImageSource="/KAN.WPF.Xctrl;component/Images/XWindow/WndTitleBar.png" />
 30                                 </Border.Background>
 31                                 <DockPanel Margin="0" >
 32                                     <Image DockPanel.Dock="Left" Name="imgIcon" VerticalAlignment="Top" Width="15" Height="15"
 33                                                Margin="5 5 0 0" Source="{TemplateBinding Icon}" Style="{DynamicResource ImgIconStyle}">
 34                                     </Image>
 35                                     <TextBlock DockPanel.Dock="Left" Name="txbTitle" VerticalAlignment="Top" 
 36                                                Margin="5 5 0 0"  FontSize="12" FontWeight="Bold" Foreground="#FF101010" Text="{TemplateBinding Title}" />
 37                                     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" 
 38                                                 DockPanel.Dock="Right" Height="28" VerticalAlignment="Top">
 39                                         <ctrl:XButton x:Name="btnDropDown" Width="28" Height="28" BorderThickness="0" Visibility="Collapsed">
 40                                             <ctrl:XButton.Background>
 41                                                 <ImageBrush ImageSource="/KAN.WPF.Xctrl;component/Images/XWindow/WndBtnDropDown.png" />
 42                                             </ctrl:XButton.Background>
 43                                             <ctrl:XButton.XMoverBrush>
 44                                                 <ImageBrush ImageSource="/KAN.WPF.Xctrl;component/Images/XWindow/WndBtnDropDownMove.png" />
 45                                             </ctrl:XButton.XMoverBrush>
 46                                             <ctrl:XButton.XEnterBrush>
 47                                                 <ImageBrush ImageSource="/KAN.WPF.Xctrl;component/Images/XWindow/WndBtnDropDownMove.png" />
 48                                             </ctrl:XButton.XEnterBrush>
 49                                         </ctrl:XButton>
 50                                         <ctrl:XButton x:Name="btnMin" Width="28" Height="28" BorderThickness="0" >
 51                                             <ctrl:XButton.Background>
 52                                                 <ImageBrush ImageSource="/KAN.WPF.Xctrl;component/Images/XWindow/WndBtnMin.png" />
 53                                             </ctrl:XButton.Background>
 54                                             <ctrl:XButton.XMoverBrush>
 55                                                 <ImageBrush ImageSource="/KAN.WPF.Xctrl;component/Images/XWindow/WndBtnMinMove.png" />
 56                                             </ctrl:XButton.XMoverBrush>
 57                                             <ctrl:XButton.XEnterBrush>
 58                                                 <ImageBrush ImageSource="/KAN.WPF.Xctrl;component/Images/XWindow/WndBtnMinEnter.png" />
 59                                             </ctrl:XButton.XEnterBrush>
 60                                         </ctrl:XButton>
 61                                         <ctrl:XButton x:Name="btnMax" Width="28" Height="28" BorderThickness="0" >
 62                                             <ctrl:XButton.Background>
 63                                                 <ImageBrush ImageSource="/KAN.WPF.Xctrl;component/Images/XWindow/WndBtnMax.png" />
 64                                             </ctrl:XButton.Background>
 65                                             <ctrl:XButton.XMoverBrush>
 66                                                 <ImageBrush ImageSource="/KAN.WPF.Xctrl;component/Images/XWindow/WndBtnMaxMove.png" />
 67                                             </ctrl:XButton.XMoverBrush>
 68                                             <ctrl:XButton.XEnterBrush>
 69                                                 <ImageBrush ImageSource="/KAN.WPF.Xctrl;component/Images/XWindow/WndBtnMaxEnter.png" />
 70                                             </ctrl:XButton.XEnterBrush>
 71                                         </ctrl:XButton>
 72                                         <ctrl:XButton x:Name="btnClose" Width="28" Height="28" BorderThickness="0" >
 73                                             <ctrl:XButton.Background>
 74                                                 <ImageBrush ImageSource="/KAN.WPF.Xctrl;component/Images/XWindow/WndBtnClose.png" />
 75                                             </ctrl:XButton.Background>
 76                                             <ctrl:XButton.XMoverBrush>
 77                                                 <ImageBrush ImageSource="/KAN.WPF.Xctrl;component/Images/XWindow/WndBtnCloseMove.png" />
 78                                             </ctrl:XButton.XMoverBrush>
 79                                             <ctrl:XButton.XEnterBrush>
 80                                                 <ImageBrush ImageSource="/KAN.WPF.Xctrl;component/Images/XWindow/WndBtnCloseEnter.png" />
 81                                             </ctrl:XButton.XEnterBrush>
 82                                         </ctrl:XButton>
 83                                     </StackPanel>
 84                                 </DockPanel>
 85                             </Border>
 86                             <!--标题栏下的阴影-->
 87                             <AdornerDecorator Grid.Row="1" Height="Auto" Width="Auto">
 88                                 <Grid>
 89                                     <Border Name="bdrShadow" Height="5" VerticalAlignment="Top" Panel.ZIndex="100" HorizontalAlignment="Stretch">
 90                                         <Border.Background>
 91                                             <ImageBrush ImageSource="/KAN.WPF.Xctrl;component/Images/XWindow/WndTitleShadow.png"  />
 92                                         </Border.Background>
 93                                     </Border>
 94                                     <ContentPresenter/>
 95                                 </Grid>
 96                             </AdornerDecorator>
 97                         </Grid>
 98                     </Border>
 99                     <ControlTemplate.Triggers>
100                         <!--根据设置,隐藏最大、最小化按钮-->
101                         <Trigger Property="ResizeMode" Value="NoResize">
102                             <Setter TargetName="btnMin" Property="Visibility" Value="Collapsed"></Setter>
103                             <Setter TargetName="btnMax" Property="Visibility" Value="Collapsed"></Setter>
104                         </Trigger>
105                         <Trigger Property="ResizeMode" Value="CanMinimize">
106                             <Setter TargetName="btnMax" Property="Visibility" Value="Collapsed"></Setter>
107                         </Trigger>
108                         <!--是否显示标题栏-->
109                         <Trigger Property="XIsShowBdrTitleBar" Value="false">
110                             <Setter TargetName="bdrTitleBar" Property="Visibility" Value="Collapsed"></Setter>
111                         </Trigger>
112                         <!--是否显示下拉按钮-->
113                         <Trigger Property="XIsShowBtnDropDown" Value="true">
114                             <Setter TargetName="btnDropDown" Property="Visibility" Value="Visible"></Setter>
115                         </Trigger>
116                     </ControlTemplate.Triggers>
117                 </ControlTemplate>
118             </Setter.Value>
119         </Setter>
120     </Style>
121 </ResourceDictionary>

  还有CS的:

  1         /// <summary>
  2         /// 静态构造方法
  3         /// </summary>
  4         static XWindow()
  5         {
  6             XWindow.XIsShowBdrTitleBarProperty = DependencyProperty.Register("XIsShowBdrTitleBar", typeof(bool), typeof(XWindow),
  7                 new PropertyMetadata(true));
  8             XWindow.XIsShowBtnDropDownProperty = DependencyProperty.Register("XIsShowBtnDropDown", typeof(bool), typeof(XWindow),
  9                 new PropertyMetadata(false));
 10             FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(XWindow), new FrameworkPropertyMetadata(typeof(XWindow)));
 11         }    
 12 ///////////////////////////////////////////////////////////////////////////////////
 13 
 14 public void InitializeEvent()
 15         {
 16 
 17             //解决没有图标时标题不顶头问题
 18             if (imgIcon.Source == null)
 19             {
 20                 imgIcon.Visibility = Visibility.Collapsed;
 21             }
 22 
 23             //防止窗体一启动时就最大化引发的边框和按钮问题
 24             if (WindowState == WindowState.Maximized)
 25             {
 26                 bdrWindow.Margin = new Thickness(0);
 27                 ImageBrush img = new ImageBrush();
 28                 ImageBrush imgE = new ImageBrush();
 29                 ImageBrush imgM = new ImageBrush();
 30                 img.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnRestore.png"));
 31                 btnMax.Background = img;
 32                 imgE.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnRestoreEnter.png"));
 33                 btnMax.XEnterBrush = imgE;
 34                 imgM.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnRestoreMove.png"));
 35                 btnMax.XMoverBrush = imgM;
 36             }
 37             else
 38             {
 39                 bdrWindow.Margin = new Thickness(customMargin);
 40                 ImageBrush img = new ImageBrush();
 41                 ImageBrush imgE = new ImageBrush();
 42                 ImageBrush imgM = new ImageBrush();
 43                 img.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnMax.png"));
 44                 btnMax.Background = img;
 45                 imgE.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnMaxEnter.png"));
 46                 btnMax.XEnterBrush = imgE;
 47                 imgM.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnMaxMove.png"));
 48                 btnMax.XMoverBrush = imgM;
 49             }
 50 
 51             //最小化窗体
 52             btnMin.Click += delegate
 53             {
 54                 this.WindowState = WindowState.Minimized;
 55             };
 56 
 57             //最大化窗体
 58             btnMax.Click += delegate
 59             {
 60                 if (this.WindowState == WindowState.Maximized)
 61                 {
 62                     this.WindowState = WindowState.Normal;
 63                 }
 64                 else
 65                 {
 66                     this.WindowState = WindowState.Maximized;
 67                 }
 68             };
 69 
 70             //关闭窗体
 71             btnClose.Click += delegate
 72             {
 73                 Exit();
 74             };
 75 
 76             //双击标题栏最大化窗体
 77             bdrTitleBar.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e)
 78             {
 79                 if ((e.ClickCount >= 2 && ResizeMode == System.Windows.ResizeMode.CanResize))
 80                 {
 81                     btnMax.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
 82                 }
 83             };
 84 
 85             //
 86             bdrTitleBar.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e)
 87             {
 88                 Window_MouseLeftButtonDown(sender, e);
 89             };
 90         }
 91 ////////////////////////////////////////////////////////////////////////
 92 
 93 /// <summary>
 94         /// 处理最大化时阴影边框问题,和最大化按钮变化
 95         /// </summary>
 96         /// <param name="sender"></param>
 97         /// <param name="e"></param>
 98         void Window_StateChanged(object sender, EventArgs e)
 99         {
100             if (WindowState == WindowState.Maximized)
101             {
102                 bdrWindow.Margin = new Thickness(0);
103                 ImageBrush img = new ImageBrush();
104                 ImageBrush imgE = new ImageBrush();
105                 ImageBrush imgM = new ImageBrush();
106                 img.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnRestore.png"));
107                 btnMax.Background = img;
108                 imgE.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnRestoreEnter.png"));
109                 btnMax.XEnterBrush = imgE;
110                 imgM.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnRestoreMove.png"));
111                 btnMax.XMoverBrush = imgM;
112             }
113             else
114             {
115                 bdrWindow.Margin = new Thickness(customMargin);
116                 ImageBrush img = new ImageBrush();
117                 ImageBrush imgE = new ImageBrush();
118                 ImageBrush imgM = new ImageBrush();
119                 img.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnMax.png"));
120                 btnMax.Background = img;
121                 imgE.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnMaxEnter.png"));
122                 btnMax.XEnterBrush = imgE;
123                 imgM.ImageSource = new BitmapImage(new Uri("pack://application:,,,/KAN.WPF.Xctrl;Component/Images/XWindow/WndBtnMaxMove.png"));
124                 btnMax.XMoverBrush = imgM;
125             }
126         }
127 ////////////////////////////////////////////////////////////////////
128 
129 /// <summary>
130         /// 键盘捕捉
131         /// </summary>
132         /// <param name="hookStruct"></param>
133         /// <param name="handle"></param>
134         public void OnKeyPress(Hook.HookStruct hookStruct, out bool handle)
135         {
136             //默认不屏蔽
137             handle = false;
138 
139             //屏蔽键Win
140             if (hookStruct.vkCode == (int)WinForms.Keys.LWin || hookStruct.vkCode == (int)WinForms.Keys.RWin)
141             {
142                 handle = true;
143             }
144 
145         }
146 
147 ////////////////////////////////////////////////////////////////////////
148  #region 公布方法
149         /// <summary>
150         /// 进入全屏
151         /// </summary>
152         public void XFullScreen()
153         {
154             //解决最大化后,全屏不覆盖任务栏的问题
155             this.WindowState = WindowState.Normal;
156             bdrTitleBar.Visibility = Visibility.Collapsed;
157             bdrWindow.Margin = new Thickness(0);
158 
159             this.ResizeMode = System.Windows.ResizeMode.NoResize;
160 
161             this.Left = 0.0;
162             this.Top = 0.0;
163             this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
164             this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
165 
166             //屏蔽鼠标拖动
167             this.MouseLeftButtonDown -= new MouseButtonEventHandler(Window_MouseLeftButtonDown);
168 
169             //安装钩子
170             keyboardHook = new Hook();
171             keyboardHook.InstallHook(this.OnKeyPress);
172         }
173 
174         /// <summary>
175         /// 退出全屏
176         /// </summary>
177         public void XExitFullScreen(double width, double height, double x, double y)
178         {
179             bdrTitleBar.Visibility = Visibility.Visible;
180             bdrWindow.Margin = new Thickness(customMargin);
181 
182             //窗体恢复位置
183             this.ResizeMode = ResizeMode.CanResize;
184             this.WindowState = WindowState.Normal;
185             this.Width = width;
186             this.Height = height;
187             this.Left = x;
188             this.Top = y;
189 
190             //添加鼠标拖动事件
191             this.MouseLeftButtonDown += new MouseButtonEventHandler(Window_MouseLeftButtonDown);
192 
193             //取消钩子
194             keyboardHook.UninstallHook();
195         }
196 
197         /// <summary>
198         /// 退出方法
199         /// </summary>
200         public virtual void Exit()
201         {
202             this.Close();
203         }
204         #endregion
205 
206         #region 公布属性
207         /// <summary>
208         /// 公布属性XIsShowBdrTitleBar(是否显示标题栏)
209         /// </summary>
210         public bool XIsShowBdrTitleBar
211         {
212             get
213             {
214                 return (bool)base.GetValue(XWindow.XIsShowBdrTitleBarProperty);
215             }
216             set
217             {
218                 base.SetValue(XWindow.XIsShowBdrTitleBarProperty, value);
219             }
220         }
221 
222         /// <summary>
223         /// 公布属性XIsShowBtnDropDown(是否显示下拉按钮)
224         /// </summary>
225         public bool XIsShowBtnDropDown
226         {
227             get
228             {
229                 return (bool)base.GetValue(XWindow.XIsShowBtnDropDownProperty);
230             }
231             set
232             {
233                 base.SetValue(XWindow.XIsShowBtnDropDownProperty, value);
234             }
235         }
236         #endregion
237 
238         #region 重写方法
239         /// <summary>
240         /// 应用样式
241         /// </summary>
242         public override void OnApplyTemplate()
243         {
244             base.OnApplyTemplate();
245             btnMin = GetTemplateChild("btnMin") as XButton;
246             btnMax = GetTemplateChild("btnMax") as XButton;
247             btnClose = GetTemplateChild("btnClose") as XButton;
248             btnDropDown = GetTemplateChild("btnDropDown") as XButton;
249             imgIcon = GetTemplateChild("imgIcon") as Image;
250             bdrWindow = GetTemplateChild("bdrWindow") as Border;
251             bdrTitleBar = GetTemplateChild("bdrTitleBar") as Border;
252         }
253         #endregion

  好啦!太多了,有点乱是吧!待会我把源码发上来吧!大家看看!多多提意见啊!

posted @ 2014-11-04 23:46  覃宇琨  阅读(1657)  评论(1编辑  收藏  举报