首页 > 代码库 > D23_03_非矩形窗口 (自定义窗体)

D23_03_非矩形窗口 (自定义窗体)

image

 

<Window x:Class="demo.TransparentBackground"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="TransparentBackground" Height="300" Width="300"        AllowsTransparency="True" WindowStyle="None"        MouseLeftButtonDown="window_MouseLeftButtonDown"        >    <!--AllowsTransparency:启用窗体透明对,WindowStyle:设置WindowStyle为None,MouseLeftButtonDown:定义鼠标左键    按下时的处理事件,Window.Background设置Window的背景-->    <Window.Background>        <ImageBrush ImageSource="shapes.png"></ImageBrush>    </Window.Background>    <Grid>        <Grid.RowDefinitions>            <RowDefinition></RowDefinition>            <RowDefinition></RowDefinition>            <RowDefinition></RowDefinition>            <RowDefinition></RowDefinition>        </Grid.RowDefinitions>        <Button Margin="20">A Sample Button</Button>        <Button Margin="20" Grid.Row="2" Click="cmdClose_Click" >Close</Button>    </Grid></Window>

 

TransparentBackground (窗体类)

using System;using System.Collections.Generic;using System.Linq;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 demo{    /// <summary>    /// TransparentBackground.xaml 的交互逻辑    /// </summary>    public partial class TransparentBackground : Window    {        public TransparentBackground()        {            InitializeComponent();        }        private void cmdClose_Click(object sender, RoutedEventArgs e)        {            this.Close();        }        private void window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)        {            this.DragMove();        }    }}

D23_03_非矩形窗口 (自定义窗体)