01.WPF中制作无边框窗体
[引用:]http://blog.csdn.net/johnsuna/article/details/1893319
眾所周知,在WinForm中,如果要制作一個無邊框窗體,可以將窗體的FormBorderStyle屬性設置為None來完成。
如果要制作成異形窗體,則需要使用圖片或者使用GDI+自定義繪制。
那么,在WPF中,我們怎樣制作一個無邊框窗體呢?
答案是將Window的WindowStyle屬性設置為None,即WindowStyle="None" 。如果是非矩形的異形窗體,則需要將背景設為Null,將允許透明設置為True,也就是:Background="{x:Null}"? AllowsTransparency="True",可能有些人還希望這個窗口可以拖來拖去,那么,就還需要設置MouseLeftButtonDown事件,比如:MouseLeftButtonDown="DragWindow",這里DragWindow由Window的DragMove()來完成。想關閉窗口?那就自己做一個按鈕,然后使用Window本身的Close()方法吧。
下面是效果:
這里右上角有個圓形的X按鈕,是用Button,但將它的樣式設置成了圓角矩形制作的。具體代碼見下:
XAML代碼:
// Window1.xaml
<Window x:Class="BorderlessWindow.Window1"
??? xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
??? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
??? Title="BorderlessWindow" Height="300" Width="300"
??WindowStyle="None" Background="{x:Null}"? AllowsTransparency="True"
??MouseLeftButtonDown="DragWindow"
??? >
?<Window.Resources>
??<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
???<Setter Property="Foreground" Value="White"/>
???<Setter Property="Template">
????<Setter.Value>
<!--設置樣式 -->
?????<ControlTemplate TargetType="{x:Type Button}">
??????<Grid>
???????<Rectangle x:Name="Rectangle" Stroke="#FFFFFFFF" StrokeMiterLimit="1.000000" StrokeThickness="0.500000" RadiusX="10" RadiusY="10" Fill="#FF777777">
???????</Rectangle>
???????<ContentPresenter x:Name="ContentPresenter" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
???????????????????????? VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
??????</Grid>
<!-- 設置鼠標移到關閉按鈕上的效果?-->
??????<ControlTemplate.Triggers>
???????<Trigger Property="IsMouseOver" Value="true">
????????<Setter Property="Fill" TargetName="Rectangle">
?????????<Setter.Value>
??????????<SolidColorBrush Color="White"></SolidColorBrush>
?????????</Setter.Value>
????????</Setter>
????????<Setter Property="Foreground" Value="Black"></Setter>
???????</Trigger>???????
??????</ControlTemplate.Triggers>
?????</ControlTemplate>
????</Setter.Value>
???</Setter>
??</Style>
?</Window.Resources>
<!-- 窗體中的內容 -->
?<Grid>
<!--?
窗體的邊框,底色設置,注意將CornerRadius與左上角“X”叉形按鈕的設置保持一致或約大于叉形按鈕的RadiusX/Y設置?-->
?<Border CornerRadius="10,10,10,10" Background="Orange" Height="Auto" BorderBrush="Teal" BorderThickness="1"> </Border>
<!--左上角的“X”叉形按鈕-->
?<Button Name="Button1" Style="{StaticResource ButtonStyle}" Click="CloseWindow" Width="15" Height="15" Content="X" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="3,3,3,3"></Button>
??? <Button Height="23" Margin="96,101,121,0" Name="button2" VerticalAlignment="Top">Test Button</Button>
? </Grid>
</Window>
C#代碼:
// Window1.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace BorderlessWindow
{
??? /// <summary>
??? /// Interaction logic for Window1.xaml
??? /// </summary>
??? public partial class Window1 : System.Windows.Window
??? {
??????? public Window1()
??????? {
??????????? InitializeComponent();
??????? }
??????? public void?DragWindow(object sender, MouseButtonEventArgs args)
??????? {
????????????this.DragMove();
??????? }
??????? public void?CloseWindow(object sender, RoutedEventArgs args)
??????? {
????????????this.Close();
??????? }
??? }
}
轉載于:https://www.cnblogs.com/Tmc-Blog/p/4214309.html
總結
以上是生活随笔為你收集整理的01.WPF中制作无边框窗体的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DatagramSocket总是发送UD
- 下一篇: [整理]ASP.NET MVC 5