1. 程式人生 > >WPF設計の不規則窗體

WPF設計の不規則窗體

cnblogs sele 我們 sel width ren arp 9.png pan

原文:WPF設計の不規則窗體

  我們在工作中,經常會需要畫一些不規則的窗體,現在總結如下。

一、利用VisualBrush實現。這依賴於VisualBrush的特性,任何控件可以作為畫刷,而畫刷又可以作為背景。

此種方法可以用於實現一些文字窗體等。(註意設置窗體的透明屬性)

  例如:

<Window x:Class="IconFontTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml
" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:IconFontTest" mc:Ignorable="d" AllowsTransparency="True" WindowStyle="None" WindowStartupLocation="CenterScreen
" Title="Window1" Height="400" Width="600"> <Window.Background> <VisualBrush> <VisualBrush.Visual> <TextBlock>好好學習</TextBlock> </VisualBrush.Visual> </VisualBrush> </Window.Background> </Window>

實現的效果:技術分享圖片

二、使用透明背景的png圖片實現。(當然註意設置透明屬性)

<Window x:Class="WPFSharpWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="412" Width="528" 
        AllowsTransparency="True" WindowStyle="None" OpacityMask="White" Background="Transparent">
    <Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown">
        <Image Stretch="Fill" Source="/WPFSharpWindow;component/cow.png" />
    </Grid>
</Window>

三、利用windows的Clip。

給window的Clip屬性賦Path值。

在XAML中定義一個Path,如下:

<Path Visibility="Hidden" x:Name="clipPath"
          Data="M 55,100 A 50,50 0 1 1 100,60 A 110,95 0 0 1 200, 60 A 50,50 0 1 1 250,100 A 110,95 0 1 1 55,100 Z"/>
賦值:

window1.Clip = clipPath.Data;

當然也可以在前臺賦值。

四、添加Border實現

以上的本質都是將窗體設置成透明然後添加不規則窗體。

WPF設計の不規則窗體