自定义窗体,简简单单实现

自定义窗体实现与使用方法详解
本文详细介绍了如何创建并使用一个自定义窗体类`BaseWindow`,该类继承自`Window`,提供了丰富的自定义样式和事件处理功能,包括标题栏的最小化、最大化、关闭按钮,以及窗口的拖动和双击放大效果。通过继承`BaseWindow`类,开发者可以快速创建出功能完善、风格统一的对话框或小窗体。

style文件xmal:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <ControlTemplate x:Key="WindowTemplateKey" TargetType="{x:Type Window}">
        <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
            <Grid>
                <AdornerDecorator>
                    <ContentPresenter />
                </AdornerDecorator>
                <ResizeGrip x:Name="WindowResizeGrip" Visibility="Collapsed" IsTabStop="false" HorizontalAlignment="Right" VerticalAlignment="Bottom" />
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="ResizeMode" Value="CanResizeWithGrip" />
                    <Condition Property="WindowState" Value="Normal" />
                </MultiTrigger.Conditions>
                <Setter Property="Visibility" TargetName="WindowResizeGrip" Value="Visible" />
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    <ControlTemplate x:Key="BaseWindowControlTemplate" TargetType="{x:Type Window}">
        <DockPanel LastChildFill="True">
            <!--外边框-->
            <Border x:Name="borderTitle" DockPanel.Dock="Top" Height="30" BorderBrush="#FFA9A9A9" BorderThickness="0,0,2,0">
                <Border BorderBrush="#FF494949" BorderThickness="1,1,1,0">
                    <Border.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FFF4F4F4" Offset="0"/>
                            <GradientStop Color="#FFEBEBEB" Offset="1"/>
                        </LinearGradientBrush>
                    </Border.Background>
                    <Grid>
                        <TextBlock x:Name="Title" VerticalAlignment="Center" Margin="20,0,0,0"></TextBlock>
                        <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
                            <!--最小化按钮-->
                            <Button x:Name="btnMin" Margin="2,4,0,4" Height="20" Width="30" Style="{DynamicResource HomeBtnStyle}">
                                <Image Source="/WisHotel;component/Images/HomePageImages/最小化图标.png" Height="18" Width="18" SnapsToDevicePixels="True"/>
                            </Button>
                            <!--最大化按钮-->
                            <Button x:Name="btnMax" Margin="2,4,0,4" Height="20" Width="30" Style="{DynamicResource HomeBtnStyle}">
                                <Image Source="/WisHotel;component/Images/HomePageImages/窗口图标.png" Height="18" Width="18"/>
                            </Button>
                            <!--关闭按钮-->
                            <Button x:Name="btnClose" Margin="2,4,10,4" Height="20" Width="30" Style="{DynamicResource HomeBtnStyle}">
                                <Image Source="/WisHotel;component/Images/HomePageImages/关闭图标.png" Height="18" Width="18"/>
                            </Button>
                        </StackPanel>
                    </Grid>
                </Border>
            </Border>
            <Border BorderBrush="#FFA9A9A9" BorderThickness="0,0,2,2">
                <Border BorderBrush="#FF494949" Background="#FFEBEBEB" BorderThickness="1,0,1,1">
                    <Border BorderBrush="#FFEBEBEB" BorderThickness="2,1">
                        <Border BorderBrush="#FF494949" BorderThickness="1">
                            <Border Background="White">
                                <AdornerDecorator>
                                    <ContentPresenter />
                                </AdornerDecorator>
                            </Border>
                        </Border>
                    </Border>
                </Border>
            </Border>
        </DockPanel>
    </ControlTemplate>
    <Style x:Key="BaseWindowStyle" TargetType="{x:Type Window}">
        <Setter Property="Template" Value="{StaticResource BaseWindowControlTemplate}"/>
        <Setter Property="AllowsTransparency" Value="True" />
        <Setter Property="WindowStyle" Value="None" />
        <Style.Triggers>
            <Trigger Property="ResizeMode" Value="CanResizeWithGrip">
                <Setter Property="Template" Value="{StaticResource WindowTemplateKey}" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

继承类cs:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WisHotel.Common
{
    public class BaseWindow : Window
    {
        public BaseWindow()
        {
            //居中显示
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //初始化样式
            this.Style = (Style)App.Current.Resources["BaseWindowStyle"];
       //下面两句是做的这个基类是用来做类似弹出编辑的小窗体和MessageBox
this.ShowInTaskbar = false;//不在任务栏显示 this.Owner = Application.Current.MainWindow;//绑定主窗口
this.Loaded += delegate { InitializeEvent(); }; } private void InitializeEvent() { ControlTemplate baseWindowTemplate = (ControlTemplate)App.Current.Resources["BaseWindowControlTemplate"]; TextBlock TitleTextBlock = (TextBlock)baseWindowTemplate.FindName("Title", this); TitleTextBlock.Text = this.Title; Button minBtn = (Button)baseWindowTemplate.FindName("btnMin", this); minBtn.Click += delegate { this.WindowState = WindowState.Minimized; }; Button maxBtn = (Button)baseWindowTemplate.FindName("btnMax", this); maxBtn.Click += delegate { this.WindowState = (this.WindowState == WindowState.Normal ? WindowState.Maximized : WindowState.Normal); }; Button closeBtn = (Button)baseWindowTemplate.FindName("btnClose", this); closeBtn.Click += delegate { this.Close(); }; Border borderTitle = (Border)baseWindowTemplate.FindName("borderTitle", this); borderTitle.MouseMove += delegate(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { this.DragMove(); } }; borderTitle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e) { if (e.ClickCount >= 2) { //maxBtn.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));//双击放大 } }; } } }

 要使用这个自定义窗体,继承即可:

<src:BaseWindow x:Class="xxx.yyy"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:src="clr-namespace:xxx"
             Width="450" Height="480">
<Grid/>
</src:BaseWindow>

 

转载于:https://www.cnblogs.com/Events/p/3794950.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值