现在的位置: 首页 > 综合 > 正文

WPF 自定义窗体

2014年09月05日 ⁄ 综合 ⁄ 共 27772字 ⁄ 字号 评论关闭

转自:http://blog.csdn.net/jian200801/article/details/12619379


一个WPF自定义窗体的例子,我封装成一个WPF自定义控件库

 

新建的Window窗体继承XianJian.Controls.Window,如下:

 

可以设置窗体的背景样式,可以是绿色,蓝色或使用背景图片。

 

运行效果:

 

以下代码:

Window.xaml

[html] view
plain
copy

  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  2.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">  
  3.     <Style x:Key="WindowStyle" TargetType="Window">  
  4.         <Setter Property="AllowsTransparency" Value="True"/>  
  5.         <Setter Property="Background" Value="Transparent"/>  
  6.         <Setter Property="WindowStyle" Value="None"/>  
  7.     </Style>  
  8.   
  9.     <ControlTemplate x:Key="WindowTemplate" TargetType="Window">  
  10.         <!--若不设置margin,阴影无法显示(被挡住了)-->  
  11.         <Border x:Name="FussWindowBorder" CornerRadius="5" Margin="8" Background="White" BorderBrush="#6A6A6A" BorderThickness="1" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"  >  
  12.             <Border.Effect>  
  13.                 <DropShadowEffect BlurRadius="8" ShadowDepth="0" Color="#00000000"/>  
  14.             </Border.Effect>  
  15.             <Border x:Name="BorderBack"  CornerRadius="5">  
  16.                 <Grid>  
  17.                     <Grid.RowDefinitions>  
  18.                         <RowDefinition Height="Auto"/>  
  19.                         <RowDefinition Height="*"/>  
  20.                     </Grid.RowDefinitions>  
  21.                     <Border Name="TitleBar" Height="25" CornerRadius="5">  
  22.                         <DockPanel Margin="0">  
  23.                             <Image DockPanel.Dock="Left" Name="ImgApp" VerticalAlignment="Top" Margin="10 5 0 0" Width="20" Height="20"></Image>  
  24.                             <TextBlock DockPanel.Dock="Left" Name="TitleText" VerticalAlignment="Top" Margin="2 5 0 0"  FontSize="14" FontWeight="Bold" Foreground="#101010"/>  
  25.                             <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" DockPanel.Dock="Right" Height="25" VerticalAlignment="Top">  
  26.                                 <Rectangle Style="{DynamicResource Splitter}"/>  
  27.                                 <Button Name="MiniButton" Width="30" Template="{DynamicResource MiniButton}"/>  
  28.                                 <Rectangle Style="{DynamicResource Splitter}"/>  
  29.                                 <Button Name="MaxButton" Width="30" Style="{DynamicResource WinNormalButton}"/>  
  30.                                 <Rectangle Name="MaxSplitter"  Style="{DynamicResource Splitter}"/>  
  31.                                 <Button Name="CloseButton" Width="35" Style="{DynamicResource CloseButton}"/>  
  32.                             </StackPanel>  
  33.                         </DockPanel>  
  34.                     </Border>  
  35.                     <AdornerDecorator Grid.Row="1" Height="Auto" Width="Auto">  
  36.                         <ContentPresenter/>  
  37.                     </AdornerDecorator>  
  38.                 </Grid>  
  39.             </Border>  
  40.         </Border>  
  41.     </ControlTemplate>  
  42.   
  43.     <Style x:Key="BackStyleWhite" TargetType="Border">  
  44.         <Setter Property="Background" Value="White"></Setter>  
  45.     </Style>  
  46.     <Style x:Key="BackStyleImage" TargetType="Border">  
  47.         <Setter Property="Background" Value="Transparent"></Setter>  
  48.     </Style>  
  49.   
  50.     <Style x:Key="HeadStyleGreen" TargetType="Border">  
  51.         <Setter Property="Background">  
  52.             <Setter.Value>  
  53.                 <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">  
  54.                     <GradientStop Color="#29B9AC" Offset="0.3"/>  
  55.                     <GradientStop Color="#00FFFFFF" Offset="1"/>  
  56.                 </LinearGradientBrush>  
  57.             </Setter.Value>  
  58.         </Setter>  
  59.     </Style>  
  60.   
  61.     <Style x:Key="HeadStyleBlue" TargetType="Border">  
  62.         <Setter Property="Background">  
  63.             <Setter.Value>  
  64.                 <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">  
  65.                     <GradientStop Color="#431EC2" Offset="0.3"/>  
  66.                     <GradientStop Color="#00FFFFFF" Offset="1"/>  
  67.                 </LinearGradientBrush>  
  68.             </Setter.Value>  
  69.         </Setter>  
  70.     </Style>  
  71.   
  72.     <Style x:Key="HeadStyleTransparent" TargetType="Border">  
  73.         <Setter Property="Background" Value="Transparent"></Setter>  
  74.     </Style>  
  75.   
  76.     <Style x:Key="Splitter" TargetType="Rectangle">  
  77.         <Setter Property="Width" Value="1"/>  
  78.         <Setter Property="Stroke" Value="Transparent"/>  
  79.         <Setter Property="StrokeThickness" Value="0"/>  
  80.         <Setter Property="Fill">  
  81.             <Setter.Value>  
  82.                 <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">  
  83.                     <LinearGradientBrush.GradientStops>  
  84.                         <GradientStop Color="#FF000000" Offset="0"/>  
  85.                         <GradientStop Color="#80000000" Offset="0.6"/>  
  86.                         <GradientStop Color="#00000000" Offset="1.0"/>  
  87.                     </LinearGradientBrush.GradientStops>  
  88.                 </LinearGradientBrush>  
  89.             </Setter.Value>  
  90.         </Setter>  
  91.     </Style>  
  92.   
  93.     <LinearGradientBrush x:Key="MinMaxBrush" StartPoint="0.5,0" EndPoint="0.5,1">  
  94.         <LinearGradientBrush.GradientStops>  
  95.             <GradientStop Color="#F0FFFFFF" Offset="0"/>  
  96.             <GradientStop Color="#00FFFFFF" Offset="1"/>  
  97.         </LinearGradientBrush.GradientStops>  
  98.     </LinearGradientBrush>  
  99.   
  100.     <LinearGradientBrush x:Key="MinMaxPressedBrush" StartPoint="0.5,0" EndPoint="0.5,1">  
  101.         <LinearGradientBrush.GradientStops>  
  102.             <GradientStop Color="#E0BBBBBB" Offset="0"/>  
  103.             <GradientStop Color="#00BBBBBB" Offset="1"/>  
  104.         </LinearGradientBrush.GradientStops>  
  105.     </LinearGradientBrush>  
  106.   
  107.     <DrawingBrush x:Key="CloseButtonBrush">  
  108.         <DrawingBrush.Drawing>  
  109.             <GeometryDrawing Geometry="M 36.0396,7.62939e-006L -6.10352e-005,7.62939e-006L -6.10352e-005,25L 39.9999,25L 39.9999,3.90626C 39.9999,1.74885 38.2269,7.62939e-006 36.0396,7.62939e-006 Z ">  
  110.                 <GeometryDrawing.Brush>  
  111.                     <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">  
  112.                         <LinearGradientBrush.GradientStops>  
  113.                             <GradientStop Color="#FFFF0000" Offset="0"/>  
  114.                             <GradientStop Color="#00FF0000" Offset="1"/>  
  115.                         </LinearGradientBrush.GradientStops>  
  116.                     </LinearGradientBrush>  
  117.                 </GeometryDrawing.Brush>  
  118.             </GeometryDrawing>  
  119.         </DrawingBrush.Drawing>  
  120.     </DrawingBrush>  
  121.   
  122.     <DrawingBrush x:Key="CloseButtonPressBrush">  
  123.         <DrawingBrush.Drawing>  
  124.             <GeometryDrawing Geometry="M 36.0396,7.62939e-006L -6.10352e-005,7.62939e-006L -6.10352e-005,25L 39.9999,25L 39.9999,3.90626C 39.9999,1.74885 38.2269,7.62939e-006 36.0396,7.62939e-006 Z ">  
  125.                 <GeometryDrawing.Brush>  
  126.                     <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">  
  127.                         <LinearGradientBrush.GradientStops>  
  128.                             <GradientStop Color="#FF5A022F" Offset="0"/>  
  129.                             <GradientStop Color="#005A022F" Offset="1"/>  
  130.                         </LinearGradientBrush.GradientStops>  
  131.                     </LinearGradientBrush>  
  132.                 </GeometryDrawing.Brush>  
  133.             </GeometryDrawing>  
  134.         </DrawingBrush.Drawing>  
  135.     </DrawingBrush>  
  136.   
  137.     <ControlTemplate x:Key="MiniButton" TargetType="Button">  
  138.         <!--一定要初始化Grid的Background属性-->  
  139.         <Grid x:Name="MiniGrid" Background="Transparent">  
  140.             <Path Data="F1 M 14.7587,4.59057L 0.5,4.59057L 0.5,0.499992L 14.7587,0.499992L 14.7587,4.59057 Z "  
  141.                        Fill="#FFFCFCFD" StrokeLineJoin="Round" Stroke="#FF4A4B4D" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0 5 0 0" />  
  142.         </Grid>  
  143.         <ControlTemplate.Triggers>  
  144.             <Trigger Property="IsMouseOver" Value="True">  
  145.                 <Setter Property="Background" TargetName="MiniGrid" Value="{StaticResource MinMaxBrush}"/>  
  146.             </Trigger>  
  147.             <Trigger Property="IsPressed" Value="True">  
  148.                 <Setter Property="Background" TargetName="MiniGrid" Value="{StaticResource MinMaxPressedBrush}"/>  
  149.             </Trigger>  
  150.         </ControlTemplate.Triggers>  
  151.     </ControlTemplate>  
  152.   
  153.     <Style x:Key="WinMaxButton" TargetType="Button">  
  154.         <Setter Property="Template">  
  155.             <Setter.Value>  
  156.                 <ControlTemplate TargetType="Button">  
  157.                     <Grid x:Name="MaxButton" Background="Transparent">  
  158.                         <Canvas Width="15" Height="15" VerticalAlignment="Center" HorizontalAlignment="Center" >  
  159.                             <Canvas.Background>  
  160.                                 <DrawingBrush>  
  161.                                     <DrawingBrush.Drawing>  
  162.                                         <DrawingGroup>  
  163.                                             <DrawingGroup.Children>  
  164.                                                 <GeometryDrawing Brush="#FFFCFCFD" Geometry="M 3.06549,0.500031L 15.5,0.500031L 15.5,12.9346L 12.4866,12.9346L 12.4866,3.6337L 3.06549,3.6337L 3.06549,0.500031 Z ">  
  165.                                                     <GeometryDrawing.Pen>  
  166.                                                         <Pen LineJoin="Round" Brush="#FF040404"/>  
  167.                                                     </GeometryDrawing.Pen>  
  168.                                                 </GeometryDrawing>  
  169.                                                 <GeometryDrawing Brush="#FFFCFCFD" Geometry="M 0.5,3.06546L 12.9345,3.06546L 12.9345,15.5L 0.5,15.5L 0.5,3.06546 Z M 3.60864,6.1741L 3.60864,12.3914L 9.82587,12.3914L 9.82587,6.1741L 3.60864,6.1741 Z ">  
  170.                                                     <GeometryDrawing.Pen>  
  171.                                                         <Pen LineJoin="Round" Brush="#FF040404"/>  
  172.                                                     </GeometryDrawing.Pen>  
  173.                                                 </GeometryDrawing>  
  174.                                             </DrawingGroup.Children>  
  175.                                         </DrawingGroup>  
  176.                                     </DrawingBrush.Drawing>  
  177.                                 </DrawingBrush>  
  178.                             </Canvas.Background>  
  179.                         </Canvas>  
  180.                     </Grid>  
  181.                     <ControlTemplate.Triggers>  
  182.                         <Trigger Property="IsMouseOver" Value="True">  
  183.                             <Setter Property="Background" TargetName="MaxButton" Value="{StaticResource MinMaxBrush}"/>  
  184.                         </Trigger>  
  185.                         <Trigger Property="IsPressed" Value="True">  
  186.                             <Setter Property="Background" TargetName="MaxButton" Value="{StaticResource MinMaxPressedBrush}"/>  
  187.                         </Trigger>  
  188.                     </ControlTemplate.Triggers>  
  189.                 </ControlTemplate>  
  190.             </Setter.Value>  
  191.         </Setter>  
  192.     </Style>  
  193.   
  194.     <Style x:Key="WinNormalButton" TargetType="Button">  
  195.         <Setter Property="Template">  
  196.             <Setter.Value>  
  197.                 <ControlTemplate TargetType="Button">  
  198.                     <Grid x:Name="MaxButton" Background="Transparent">  
  199.                         <Canvas Width="15" Height="15" VerticalAlignment="Center" HorizontalAlignment="Center" >  
  200.                             <Canvas.Background>  
  201.                                 <DrawingBrush>  
  202.                                     <DrawingBrush.Drawing>  
  203.                                         <DrawingGroup>  
  204.                                             <DrawingGroup.Children>  
  205.                                                 <GeometryDrawing Brush="#FFFCFCFD" Geometry="M 0.5,3.06546L 12.9345,3.06546L 12.9345,15.5L 0.5,15.5L 0.5,3.06546 Z M 3.60864,6.1741L 3.60864,12.3914L 9.82587,12.3914L 9.82587,6.1741L 3.60864,6.1741 Z ">  
  206.                                                     <GeometryDrawing.Pen>  
  207.                                                         <Pen LineJoin="Round" Brush="#FF040404"/>  
  208.                                                     </GeometryDrawing.Pen>  
  209.                                                 </GeometryDrawing>  
  210.                                             </DrawingGroup.Children>  
  211.                                         </DrawingGroup>  
  212.                                     </DrawingBrush.Drawing>  
  213.                                 </DrawingBrush>  
  214.                             </Canvas.Background>  
  215.                         </Canvas>  
  216.                     </Grid>  
  217.                     <ControlTemplate.Triggers>  
  218.                         <Trigger Property="IsMouseOver" Value="True">  
  219.                             <Setter Property="Background" TargetName="MaxButton" Value="{StaticResource MinMaxBrush}"/>  
  220.                         </Trigger>  
  221.                         <Trigger Property="IsPressed" Value="True">  
  222.                             <Setter Property="Background" TargetName="MaxButton" Value="{StaticResource MinMaxPressedBrush}"/>  
  223.                         </Trigger>  
  224.                     </ControlTemplate.Triggers>  
  225.                 </ControlTemplate>  
  226.             </Setter.Value>  
  227.         </Setter>  
  228.     </Style>  
  229.   
  230.     <Style x:Key="CloseButton" TargetType="Button">  
  231.         <Setter Property="Template">  
  232.             <Setter.Value>  
  233.                 <ControlTemplate TargetType="Button">  
  234.                     <Grid x:Name="CloseGrid" Background="Transparent">  
  235.                         <Path Data="F1 M 8,3.21448L 6.02069,0.5L 0.507629,0.5L 5.24347,6.99478L 0.5,13.5L 6.013,13.5L 8,10.7751L 9.987,13.5L 15.5,13.5L 10.7565,6.99478L 15.4924,0.5L 9.97937,0.5L 8,3.21448 Z "  
  236.                                    Fill="#FFFCFCFD" StrokeLineJoin="Round" Stroke="#FF4A4B4D" HorizontalAlignment="Center" VerticalAlignment="Center"/>  
  237.                     </Grid>  
  238.                     <ControlTemplate.Triggers>  
  239.                         <Trigger Property="IsMouseOver" Value="True">  
  240.                             <Setter Property="Background" TargetName="CloseGrid" Value="{StaticResource CloseButtonBrush}"/>  
  241.                         </Trigger>  
  242.                         <Trigger Property="IsPressed" Value="True">  
  243.                             <Setter Property="Background" TargetName="CloseGrid" Value="{StaticResource CloseButtonPressBrush}"/>  
  244.                         </Trigger>  
  245.                     </ControlTemplate.Triggers>  
  246.                 </ControlTemplate>  
  247.             </Setter.Value>  
  248.         </Setter>  
  249.     </Style>  
  250. </ResourceDictionary>  


Window.cs

[csharp] view
plain
copy

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Interop;  
  8. using System.Windows.Media;  
  9. using System.Windows.Media.Imaging;  
  10. using System.Windows.Shapes;  
  11.   
  12. namespace XianJian.Controls  
  13. {  
  14.     /// <summary>  
  15.     /// 自定义窗口  
  16.     /// </summary>  
  17.     public class Window : System.Windows.Window  
  18.     {  
  19.         private readonly ResourceDictionary mWindowResouce = new ResourceDictionary();  
  20.         private readonly ControlTemplate mTemplate;  
  21.   
  22.         private const int WM_NCHITTEST = 0x0084;  
  23.         private const int WM_GETMINMAXINFO = 0x0024;  
  24.   
  25.         private const int CORNER_WIDTH = 12; //拐角宽度  
  26.         private const int MARGIN_WIDTH = 4; // 边框宽度  
  27.         private Point mMousePoint = new Point(); //鼠标坐标  
  28.         private Button mMaxButton;  
  29.         private bool mIsShowMax = true;  
  30.   
  31.         /// <summary>  
  32.         /// 是否显示最大化按钮  
  33.         /// </summary>  
  34.         public bool IsShowMax  
  35.         {  
  36.             get  
  37.             {  
  38.                 return mIsShowMax;  
  39.             }  
  40.             set  
  41.             {  
  42.                 mIsShowMax = value;  
  43.             }  
  44.         }  
  45.   
  46.         private BackGroundType mBackGroundType;  
  47.         /// <summary>  
  48.         /// 背景类型  
  49.         /// </summary>  
  50.         public BackGroundType BackGroundType  
  51.         {  
  52.             get  
  53.             {  
  54.                 return mBackGroundType;  
  55.             }  
  56.             set  
  57.             {  
  58.                 mBackGroundType = value;  
  59.             }  
  60.         }  
  61.   
  62.         private ImageSource mBackImage;  
  63.         /// <summary>  
  64.         /// 背景图片  
  65.         /// </summary>  
  66.         public ImageSource BackImage  
  67.         {  
  68.             get  
  69.             {  
  70.                 return mBackImage;  
  71.             }  
  72.             set  
  73.             {  
  74.                 mBackImage = value;  
  75.             }  
  76.         }  
  77.   
  78.         /// <summary>  
  79.         /// 窗口  
  80.         /// </summary>  
  81.         public Window()  
  82.         {  
  83.             mWindowResouce.Source = new Uri("XJControls;component/Themes/Window.xaml", UriKind.Relative);  
  84.             this.Resources.MergedDictionaries.Add(mWindowResouce);  
  85.             this.Style = (Style)mWindowResouce["WindowStyle"];  
  86.             var windowTemplate = (ControlTemplate)mWindowResouce["WindowTemplate"];  
  87.             this.Template = windowTemplate;  
  88.             mTemplate = windowTemplate;  
  89.             this.Loaded += new RoutedEventHandler(WindowBase_Loaded);  
  90.             this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;  
  91.             this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;  
  92.             mBackGroundType = BackGroundType.Blue;  
  93.         }  
  94.   
  95.         private void WindowBase_Loaded(object sender, RoutedEventArgs e)  
  96.         {  
  97.             ((Border)mTemplate.FindName("FussWindowBorder"this)).MouseLeftButtonDown += (s1, e1) => this.DragMove();  
  98.             ((TextBlock)mTemplate.FindName("TitleText"this)).Text = this.Title;  
  99.             ((Image)mTemplate.FindName("ImgApp"this)).Source = this.Icon;  
  100.   
  101.             var backBorder = (Border)mTemplate.FindName("BorderBack"this);  
  102.             var headBorder = (Border)mTemplate.FindName("TitleBar"this);  
  103.             switch (mBackGroundType)  
  104.             {  
  105.                 case BackGroundType.Green:  
  106.                     backBorder.Style = (Style)mWindowResouce["BackStyleWhite"];  
  107.                     headBorder.Style = (Style)mWindowResouce["HeadStyleGreen"];  
  108.                     break;  
  109.                 case BackGroundType.Blue:  
  110.                     backBorder.Style = (Style)mWindowResouce["BackStyleWhite"];  
  111.                     headBorder.Style = (Style)mWindowResouce["HeadStyleBlue"];  
  112.                     break;  
  113.                 case BackGroundType.Image:  
  114.                     backBorder.Style = (Style)mWindowResouce["BackStyleImage"];  
  115.                     backBorder.Background = new ImageBrush(mBackImage);  
  116.                     headBorder.Style = (Style)mWindowResouce["HeadStyleTransparent"];  
  117.                     break;  
  118.                 default:  
  119.                     backBorder.Style = (Style)mWindowResouce["BackStyleWhite"];  
  120.                     headBorder.Style = (Style)mWindowResouce["HeadStyleGreen"];  
  121.                     break;  
  122.             }  
  123.   
  124.             mMaxButton = (Button)mTemplate.FindName("MaxButton"this);  
  125.             if (!IsShowMax)  
  126.             {  
  127.                 mMaxButton.Visibility = Visibility.Collapsed;  
  128.                 var rectangle = mTemplate.FindName("MaxSplitter"thisas Rectangle;  
  129.                 if (rectangle != null)  
  130.                     rectangle.Visibility = Visibility.Collapsed;  
  131.             }  
  132.             else mMaxButton.Visibility = Visibility.Visible;  
  133.   
  134.             this.SizeChanged += (s1, e1) =>  
  135.             {  
  136.                 if (this.WindowState == WindowState.Normal)  
  137.                 {  
  138.                     mMaxButton.Style = (Style)mWindowResouce["WinNormalButton"];  
  139.                 }  
  140.                 else if (mMaxButton.Style != (Style)mWindowResouce["WinMaxButton"]  
  141.                     && this.WindowState == WindowState.Maximized)  
  142.                 {  
  143.                     mMaxButton.Style = (Style)mWindowResouce["WinMaxButton"];  
  144.                 }  
  145.             };  
  146.               
  147.             ((Button)mTemplate.FindName("MiniButton"this)).Click += (s2, e2) =>  
  148.             {  
  149.                 this.WindowState = WindowState.Minimized;  
  150.             };  
  151.             mMaxButton.Click += (s3, e3) =>  
  152.             {  
  153.                 this.WindowState = (this.WindowState == WindowState.Normal) ? WindowState.Maximized : WindowState.Normal;  
  154.             };  
  155.   
  156.             ((Button)mTemplate.FindName("CloseButton"this)).Click += (s4, e4) => this.Close();  
  157.   
  158.             var hwndSource = PresentationSource.FromVisual(thisas HwndSource;  
  159.             if (hwndSource != null)  
  160.             {  
  161.                 hwndSource.AddHook(new HwndSourceHook(WndProc));  
  162.             }  
  163.         }  
  164.         /// <summary>  
  165.         /// 窗口消息  
  166.         /// </summary>  
  167.         /// <param name="hwnd"></param>  
  168.         /// <param name="msg"></param>  
  169.         /// <param name="wParam"></param>  
  170.         /// <param name="lParam"></param>  
  171.         /// <param name="handled"></param>  
  172.         /// <returns></returns>  
  173.         protected virtual IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)  
  174.         {  
  175.             switch (msg)  
  176.             {  
  177.                 case WM_NCHITTEST:  
  178.                     mMousePoint.X = (lParam.ToInt32() & 0xFFFF);  
  179.                     mMousePoint.Y = (lParam.ToInt32() >> 16);  
  180.                     handled = true;  
  181.                     //窗体为最大化时(如果最大化,Left、Top属性都会造成影响)  
  182.                     if (this.WindowState == WindowState.Normal)  
  183.                     {  
  184.                         #region 拖拽改变窗体大小  
  185.                         //左上角  
  186.                         if ((mMousePoint.Y - Top <= CORNER_WIDTH) && (mMousePoint.X - Left <= CORNER_WIDTH))  
  187.                         {  
  188.                             return new IntPtr((int)HitTest.HTTOPLEFT);  
  189.                         }  
  190.                         //左下角  
  191.                         if ((this.ActualHeight + this.Top - this.mMousePoint.Y <= CORNER_WIDTH) && (this.mMousePoint.X - this.Left <= CORNER_WIDTH))  
  192.                         {  
  193.                             return new IntPtr((int)HitTest.HTBOTTOMLEFT);  
  194.                         }  
  195.                         //右上角  
  196.                         if ((this.mMousePoint.Y - this.Top <= CORNER_WIDTH) && (this.ActualWidth + this.Left - this.mMousePoint.X <= CORNER_WIDTH))  
  197.                         {  
  198.                             return new IntPtr((int)HitTest.HTTOPRIGHT);  
  199.                         }  
  200.                         //右下角  
  201.                         if ((this.ActualWidth + this.Left - this.mMousePoint.X <= CORNER_WIDTH) && (this.ActualHeight + this.Top - this.mMousePoint.Y <= CORNER_WIDTH))  
  202.                         {  
  203.                             return new IntPtr((int)HitTest.HTBOTTOMRIGHT);  
  204.                         }  
  205.                         //左侧  
  206.                         if (this.mMousePoint.X - (this.Left + 4) <= MARGIN_WIDTH)  
  207.                         {  
  208.                             return new IntPtr((int)HitTest.HTLEFT);  
  209.                         }  
  210.                         //右侧  
  211.                         if (this.ActualWidth + this.Left - 4 - this.mMousePoint.X <= MARGIN_WIDTH)  
  212.                         {  
  213.                             return new IntPtr((int)HitTest.HTRIGHT);  
  214.                         }  
  215.                         //上侧  
  216.                         if (this.mMousePoint.Y - (this.Top + 4) <= MARGIN_WIDTH)  
  217.                         {  
  218.                             return new IntPtr((int)HitTest.HTTOP);  
  219.                         }  
  220.                         //下侧  
  221.                         if (this.ActualHeight + this.Top - 4 - this.mMousePoint.Y <= MARGIN_WIDTH)  
  222.                         {  
  223.                             return new IntPtr((int)HitTest.HTBOTTOM);  
  224.                         }  
  225.                         #endregion  
  226.                         //正常情况下移动窗体  
  227.                         if (this.mMousePoint.Y - this.Top > 0 && this.mMousePoint.Y - this.Top < 25 && this.Left + this.ActualWidth - mMousePoint.X > 100)  
  228.                         {  
  229.                             return new IntPtr((int)HitTest.HTCAPTION);  
  230.                         }  
  231.                     }  
  232.                     //最大化时移动窗体,让窗体正常化  
  233.                     if (this.WindowState == WindowState.Maximized)  
  234.                     {  
  235.                         if (this.mMousePoint.Y < 40 && this.ActualWidth - mMousePoint.X > 110)  
  236.                         {  
  237.                             return new IntPtr((int)HitTest.HTCAPTION);  
  238.                         }  
  239.                     }  
  240.                     //将q其他区域设置为客户端,避免鼠标事件被屏蔽  
  241.                     return new IntPtr((int)HitTest.HTCLIENT);  
  242.             }  
  243.             return IntPtr.Zero;  
  244.         }  
  245.     }  
  246.   
  247.     /// <summary>  
  248.     /// 包含了鼠标的各种消息  
  249.     /// </summary>  
  250.     public enum HitTest : int  
  251.     {  
  252.         /// <summary>  
  253.         /// HTERROR  
  254.         /// </summary>  
  255.         HTERROR = -2,  
  256.         /// <summary>  
  257.         /// HTTRANSPARENT  
  258.         /// </summary>  
  259.         HTTRANSPARENT = -1,  
  260.         /// <summary>  
  261.         /// HTNOWHERE  
  262.         /// </summary>  
  263.         HTNOWHERE = 0,  
  264.         /// <summary>  
  265.         /// HTCLIENT  
  266.         /// </summary>  
  267.         HTCLIENT = 1,  
  268.         /// <summary>  
  269.         /// HTCAPTION  
  270.         /// </summary>  
  271.         HTCAPTION = 2,  
  272.         /// <summary>  
  273.         /// HTSYSMENU  
  274.         /// </summary>  
  275.         HTSYSMENU = 3,  
  276.         /// <summary>  
  277.         /// HTGROWBOX  
  278.         /// </summary>  
  279.         HTGROWBOX = 4,  
  280.         /// <summary>  
  281.         /// HTSIZE  
  282.         /// </summary>  
  283.         HTSIZE = HTGROWBOX,  
  284.         /// <summary>  
  285.         /// HTMENU  
  286.         /// </summary>  
  287.         HTMENU = 5,  
  288.         /// <summary>  
  289.         /// HTHSCROLL  
  290.         /// </summary>  
  291.         HTHSCROLL = 6,  
  292.         /// <summary>  
  293.         /// HTVSCROLL  
  294.         /// </summary>  
  295.         HTVSCROLL = 7,  
  296.         /// <summary>  
  297.         /// HTMINBUTTON  
  298.         /// </summary>  
  299.         HTMINBUTTON = 8,  
  300.         /// <summary>  
  301.         /// HTMAXBUTTON  
  302.         /// </summary>  
  303.         HTMAXBUTTON = 9,  
  304.         /// <summary>  
  305.         /// HTLEFT  
  306.         /// </summary>  
  307.         HTLEFT = 10,  
  308.         /// <summary>  
  309.         /// HTRIGHT  
  310.         /// </summary>  
  311.         HTRIGHT = 11,  
  312.         /// <summary>  
  313.         /// HTTOP  
  314.         /// </summary>  
  315.         HTTOP = 12,  
  316.         /// <summary>  
  317.         /// HTTOPLEFT  
  318.         /// </summary>  
  319.         HTTOPLEFT = 13,  
  320.         /// <summary>  
  321.         /// HTTOPRIGHT  
  322.         /// </summary>  
  323.         HTTOPRIGHT = 14,  
  324.         /// <summary>  
  325.         /// HTBOTTOM  
  326.         /// </summary>  
  327.         HTBOTTOM = 15,  
  328.         /// <summary>  
  329.         /// HTBOTTOMLEFT  
  330.         /// </summary>  
  331.         HTBOTTOMLEFT = 16,  
  332.         /// <summary>  
  333.         /// HTBOTTOMRIGHT  
  334.         /// </summary>  
  335.         HTBOTTOMRIGHT = 17,  
  336.         /// <summary>  
  337.         /// HTBORDER  
  338.         /// </summary>  
  339.         HTBORDER = 18,  
  340.         /// <summary>  
  341.         /// HTREDUCE  
  342.         /// </summary>  
  343.         HTREDUCE = HTMINBUTTON,  
  344.         /// <summary>  
  345.         /// HTZOOM  
  346.         /// </summary>  
  347.         HTZOOM = HTMAXBUTTON,  
  348.         /// <summary>  
  349.         /// HTSIZEFIRST  
  350.         /// </summary>  
  351.         HTSIZEFIRST = HTLEFT,  
  352.         /// <summary>  
  353.         /// HTSIZELAST  
  354.         /// </summary>  
  355.         HTSIZELAST = HTBOTTOMRIGHT,  
  356.         /// <summary>  
  357.         /// HTOBJECT  
  358.         /// </summary>  
  359.         HTOBJECT = 19,  
  360.         /// <summary>  
  361.         /// HTCLOSE  
  362.         /// </summary>  
  363.         HTCLOSE = 20,  
  364.         /// <summary>  
  365.         /// HTHELP  
  366.         /// </summary>  
  367.         HTHELP = 21  
  368.     }  
  369.     /// <summary>  
  370.     /// 背景类型  
  371.     /// </summary>  
  372.     public enum BackGroundType  
  373.     {  
  374.         /// <summary>  
  375.         /// 绿色调  
  376.         /// </summary>  
  377.         Green,  
  378.         /// <summary>  
  379.         /// 蓝色调  
  380.         /// </summary>  
  381.         Blue,  
  382.         /// <summary>  
  383.         /// 背景图片  
  384.         /// </summary>  
  385.         Image  
  386.     }  
  387. }  
[csharp] view
plain
copy

  1.    

抱歉!评论已关闭.