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

2020-12-13 03:06

阅读:533

标签:style   class   blog   code   http   tar   

style文件xmal:

ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    "WindowTemplateKey" TargetType="{x:Type Window}">
        "{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
            "WindowResizeGrip" Visibility="Collapsed" IsTabStop="false" HorizontalAlignment="Right" VerticalAlignment="Bottom" />
            "ResizeMode" Value="CanResizeWithGrip" />
                    "WindowState" Value="Normal" />
                "Visibility" TargetName="WindowResizeGrip" Value="Visible" />
            "BaseWindowControlTemplate" TargetType="{x:Type Window}">
        "True">
            
            "borderTitle" DockPanel.Dock="Top" Height="30" BorderBrush="#FFA9A9A9" BorderThickness="0,0,2,0">
                "#FF494949" BorderThickness="1,1,1,0">
                    "0.5,1" StartPoint="0.5,0">
                            "#FFF4F4F4" Offset="0"/>
                            "#FFEBEBEB" Offset="1"/>
                        "Title" VerticalAlignment="Center" Margin="20,0,0,0">"Right" Orientation="Horizontal">
                            
                            
                            
                            
                            
                            
                        "#FFA9A9A9" BorderThickness="0,0,2,2">
                "#FF494949" Background="#FFEBEBEB" BorderThickness="1,0,1,1">
                    "#FFEBEBEB" BorderThickness="2,1">
                        "#FF494949" BorderThickness="1">
                            "White">
                                
                                    

继承类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));//双击放大 } }; } } }

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

"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">

 

自定义窗体,简简单单实现,搜素材,soscw.com

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

标签:style   class   blog   code   http   tar   

原文地址:http://www.cnblogs.com/Events/p/3794950.html


评论


亲,登录后才可以留言!