zoukankan      html  css  js  c++  java
  • 自定义窗体,简简单单实现

    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>
  • 相关阅读:
    关于32位操作系统和64位操作系统对InstallShield打包的影响
    NEWS: Symantec宣布Wise Package Studio将终止
    InstallShield 2012新功能试用(2) 调用MsiGetProperty等MSI API发生变化
    Basic INFO 在命令行Build InstallShield安装包工程获得压缩安装包
    NEWS InstallShield 2012 Service Pack 1发布
    Basic INFO InstallShield Basic MSI工程中如何在SetupCompleteSuccess界面中启动Readme
    Basic INFO InstallShield的脚本编辑器中如何显示代码行号
    Basic INFO 关于在InstallShield制作的安装包界面中删除InstallShield文字的厂商回复
    Basic INFO InstallShield工程中如何让产品的快捷方式名称始终与产品名保持一致
    Basic INFO: 创建隐藏文件夹
  • 原文地址:https://www.cnblogs.com/Events/p/3794950.html
Copyright © 2011-2022 走看看