wpf 不规则窗体

WPF布局(1)-简单不规则窗体

如果想制作一个不规则形状的窗体可以采取以下步骤:

1、设置窗体属性

this.WindowStyle = WindowStyle.None;//设置成无边框形式
this.Background = null;//设置背景色为空(即黑色)。
this.AllowsTransparency = true;//设置窗体允许透明,这样我们就可以通过透明度:Opacity或者Opacitymask 对它进行透明度设置。

2、设置一个border作为窗体的content

如:

 Border m_border = new Border();

this.Content = m_border;//设置border内容
 m_border.CornerRadius = new CornerRadius(10);//设置Borer圆角

m_border.Background = Brushes.White;//设置border背景色
m_border.BorderBrush = Brushes.Linen;//设置border边框颜色
m_border.BorderThickness = new Thickness(3);//设置border边框厚度

3、窗体的拖动

WPF提供了DragMove方法,只须在相应某控件的MouseLeftButton事件函数中调用此函数即可,实现窗体的拖动。

4、窗体的关闭

由于此窗体为None,无边框形式,所以可以制作一个小的按钮作为关闭按钮,点击时相应this.Close方法即可。


wpf 生成不规则窗体

 

来源:http://www.cnblogs.com/DragonInSea/archive/2009/04/10/1432956.html

 

使用异形窗体,可以将窗体的背景设置为透明,边框设置为空,然后利用控件做出异形的窗体,例如:

另外,还要把窗体背景设置为 null, Background="{x:Null}"

XAML:

   1: <Window x:Class="WpfWindow.CustomerWindow"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     Title="NonRectangularWindowSample" SizeToContent="WidthAndHeight" 
   5:     MouseLeftButtonDown="NonRectangularWindow_MouseLeftButtonDown"
   6:     WindowStyle="None" 
   7:     AllowsTransparency="True" 
   8:     Background="Transparent">
   9:     <Canvas Width="200" Height="200" >
  10:         <Path Stroke="DarkGray" StrokeThickness="2">
  11:             <Path.Fill>
  12:                 <LinearGradientBrush StartPoint="0.2,0" EndPoint="0.8,1" >
  13:                     <GradientStop Color="White"  Offset="0"></GradientStop>
  14:                     <GradientStop Color="White"  Offset="0.45"></GradientStop>
  15:                     <GradientStop Color="LightBlue" Offset="0.9"></GradientStop>
  16:                     <GradientStop Color="Gray" Offset="1"></GradientStop>
  17:                 </LinearGradientBrush>
  18:             </Path.Fill>
  19:             <Path.Data>
  20:                 <PathGeometry>
  21:                     <PathFigure StartPoint="40,20" IsClosed="True">
  22:                         <LineSegment Point="160,20"></LineSegment>
  23:                         <ArcSegment Point="180,40" Size="20,20" SweepDirection="Clockwise"></ArcSegment>
  24:                         <LineSegment Point="180,80"></LineSegment>
  25:                         <ArcSegment Point="160,100" Size="20,20" SweepDirection="Clockwise"></ArcSegment>
  26:                         <LineSegment Point="90,100"></LineSegment>
  27:                         <LineSegment Point="90,150"></LineSegment>
  28:                         <LineSegment Point="60,100"></LineSegment>
  29:                         <LineSegment Point="40,100"></LineSegment>
  30:                         <ArcSegment Point="20,80" Size="20,20" SweepDirection="Clockwise"></ArcSegment>
  31:                         <LineSegment Point="20,40"></LineSegment>
  32:                         <ArcSegment Point="40,20" Size="20,20" SweepDirection="Clockwise"></ArcSegment>
  33:                     </PathFigure>
  34:                 </PathGeometry>
  35:             </Path.Data>
  36:         </Path>
  37:         <Label Width="200" Height="120" FontSize="15" HorizontalContentAlignment="Center" 
VerticalContentAlignment="Center">Drag Me</Label>
  38:         <Button Canvas.Left="155" Canvas.Top="30" Click="closeButtonRectangle_Click">
  39:             <Button.Template>
  40:                 <ControlTemplate>
  41:                     <Canvas>
  42:                         <Rectangle Width="15" Height="15" Stroke="Black" RadiusX="3" RadiusY="3">
  43:                             <Rectangle.Fill>
  44:                                 <SolidColorBrush x:Name="myAnimatedBrush" Color="Red" />
  45:                             </Rectangle.Fill>
  46:                         </Rectangle>
  47:                         <Line X1="3" Y1="3" X2="12" Y2="12" Stroke="White" StrokeThickness="2"></Line>
  48:                         <Line X1="12" Y1="3" X2="3" Y2="12" Stroke="White" StrokeThickness="2"></Line>
  49:                     </Canvas>
  50:                 </ControlTemplate>
  51:             </Button.Template>
  52:         </Button>
  53:     </Canvas>
  54: </Window>

posted on 2010-02-23 20:29  大宝pku  阅读(2181)  评论(0编辑  收藏  举报

导航