欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WPF自定义Window窗体样式

程序员文章站 2022-05-11 14:28:44
资源文件代码: ......

资源文件代码:

WPF自定义Window窗体样式
<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!-- 窗体按钮模板 -->
    <controltemplate x:key="tmplwindowbtn" targettype="{x:type button}">
        <border x:name="bd" width="28" height="18" background="transparent"  >
            <grid>
                <image x:name="img" stretch="fill" source="{templatebinding tag}"  />
                <border x:name="bdmask" opacity="0.3" visibility="collapsed" background="#001122" margin="1 0 1 1" cornerradius="0 0 3 3"></border>
            </grid>
        </border>
        <controltemplate.triggers>
            <trigger property="ismouseover" value="true">
                <setter targetname="bdmask" property="visibility" value="visible"/>
            </trigger>
            <trigger property="ispressed" value="true">
            </trigger>
        </controltemplate.triggers>
    </controltemplate>
    <!-- 窗体模板 -->
    <controltemplate x:key="tmplwindowex" targettype="{x:type window}">
        <border>
            <border cornerradius="5" background="#0998b8" margin="{binding bordermargin}">
                <grid>
                    <grid.rowdefinitions>
                        <rowdefinition height="26"></rowdefinition>
                        <rowdefinition></rowdefinition>
                    </grid.rowdefinitions>
                    <textblock text="{templatebinding title}" margin="10 0 0 0" fontfamily="微软雅黑,黑体" fontsize="12" foreground="#fff" verticalalignment="center"></textblock>
                    <stackpanel orientation="horizontal" background="#0998b8" horizontalalignment="{binding btnpanelhorizontalalignment}" width="100" margin="88 0 0 0">
                        <stackpanel orientation="horizontal" margin="10 0 5 0" verticalalignment="top" horizontalalignment="right">
                            <button x:name="btnminimize" template="{staticresource tmplwindowbtn}" windowchrome.ishittestvisibleinchrome="true" command="{binding datacontext.windowbtncommand, relativesource={relativesource ancestortype=window}}" commandparameter="1" visibility="{binding btnminimizevisibility}" >
                                <button.tag>
                                    <bitmapimage urisource="/suncreate.common.controls;component/windowex/images/btnwindowmin.png"/>
                                </button.tag>
                            </button>
                            <button x:name="btnmaximize" template="{staticresource tmplwindowbtn}" windowchrome.ishittestvisibleinchrome="true" command="{binding datacontext.windowbtncommand, relativesource={relativesource ancestortype=window}}" commandparameter="2" visibility="{binding btnmaximizevisibility}" >
                                <button.tag>
                                    <bitmapimage urisource="/suncreate.common.controls;component/windowex/images/btnwindowmax.png"/>
                                </button.tag>
                            </button>
                            <button x:name="btnclose" template="{staticresource tmplwindowbtn}" windowchrome.ishittestvisibleinchrome="true" command="{binding datacontext.windowbtncommand, relativesource={relativesource ancestortype=window}}" commandparameter="3">
                                <button.tag>
                                    <bitmapimage urisource="/suncreate.common.controls;component/windowex/images/btnwindowclose.png"/>
                                </button.tag>
                            </button>
                        </stackpanel>
                    </stackpanel>
                    <border background="#d6e7f1" cornerradius="3 0 3 3" grid.row="1" margin="3" >
                        <contentpresenter ></contentpresenter>
                    </border>
                </grid>
            </border>
        </border>
    </controltemplate>
    <!-- 窗体样式 -->
    <style x:key="stlwindowex" targettype="{x:type window}">
        <setter property="template" value="{staticresource tmplwindowex}"/>
        <setter property="allowstransparency" value="true"></setter>
        <setter property="background" value="transparent"></setter>
        <setter property="borderthickness" value="0" />
        <setter property="borderbrush" value="transparent" />
        <setter property="resizemode" value="noresize" />
        <setter property="windowstyle" value="none" />
        <setter property="windowchrome.windowchrome">
            <setter.value>
                <windowchrome cornerradius="5 5 0 0"
                              captionheight="35"
                              glassframethickness="0"
                              useaerocaptionbuttons="false"
                              nonclientframeedges="none">
                </windowchrome>
            </setter.value>
        </setter>
    </style>
</resourcedictionary>
    
view code

自定义窗体封装windowex类代码:

WPF自定义Window窗体样式
using system;
using system.collections.generic;
using system.componentmodel;
using system.linq;
using system.reflection;
using system.resources;
using system.text;
using system.threading.tasks;
using system.windows;
using system.windows.input;
using system.windows.resources;

namespace suncreate.common.controls
{
    /// <summary>
    /// 窗体封装
    /// </summary>
    public class windowex : window, inotifypropertychanged
    {
        public event propertychangedeventhandler propertychanged;

        private icommand _windowbtncommand;
        /// <summary>
        /// 窗体按钮命令
        /// </summary>
        public icommand windowbtncommand
        {
            get
            {
                return _windowbtncommand;
            }
            set
            {
                _windowbtncommand = value;
                onpropertychanged("windowbtncommand");
            }
        }

        private thickness _bordermargin = new thickness(0, 0, 0, 0);
        public thickness bordermargin
        {
            get
            {
                return _bordermargin;
            }
            set
            {
                _bordermargin = value;
                onpropertychanged("bordermargin");
            }
        }

        private horizontalalignment _btnpanelhorizontalalignment = horizontalalignment.right;
        /// <summary>
        /// 窗体按钮的panel位置
        /// </summary>
        public horizontalalignment btnpanelhorizontalalignment
        {
            get
            {
                return _btnpanelhorizontalalignment;
            }
            set
            {
                _btnpanelhorizontalalignment = value;
                onpropertychanged("btnpanelhorizontalalignment");
            }
        }

        private visibility _btnminimizevisibility = visibility.visible;
        /// <summary>
        /// 窗体最小化按钮的显示状态
        /// </summary>
        public visibility btnminimizevisibility
        {
            get
            {
                return _btnminimizevisibility;
            }
            set
            {
                _btnminimizevisibility = value;
                onpropertychanged("btnminimizevisibility");
            }
        }

        private visibility _btnmaximizevisibility = visibility.visible;
        /// <summary>
        /// 窗体最大化按钮的显示状态
        /// </summary>
        public visibility btnmaximizevisibility
        {
            get
            {
                return _btnmaximizevisibility;
            }
            set
            {
                _btnmaximizevisibility = value;
                onpropertychanged("btnmaximizevisibility");
            }
        }

        /// <summary>
        /// 窗体 构造函数
        /// </summary>
        public windowex()
        {
            this.datacontext = this;
            this.showintaskbar = false;

            #region 窗体样式设置
            uri uri = new uri("/suncreate.common.controls;component/windowex/windowexresource.xaml", urikind.relative);
            resourcedictionary rd = new resourcedictionary();
            rd.source = uri;
            this.style = rd["stlwindowex"] as style;
            #endregion

            #region 窗体按钮事件
            windowbtncommand windowbtncommand = new windowbtncommand();
            windowbtncommand.doaction = (parameter) =>
            {
                if (parameter == 1) //最小化
                {
                    this.bordermargin = new thickness(1, 0, 0, 0);
                    btnpanelhorizontalalignment = horizontalalignment.left;
                    btnminimizevisibility = visibility.collapsed;
                    this.windowstate = windowstate.minimized;
                }
                if (parameter == 2) //窗口还原、最大化
                {
                    if (this.windowstate == windowstate.normal)
                    {
                        double taskbarheight = systemparameters.primaryscreenheight - systemparameters.workarea.height;
                        double taskbarwidth = systemparameters.primaryscreenwidth - systemparameters.workarea.width;
                        if (taskbarwidth > 0)
                        {
                            this.bordermargin = new thickness(0, 0, taskbarwidth, 0);
                        }
                        if (taskbarheight > 0)
                        {
                            this.bordermargin = new thickness(0, 0, 0, taskbarheight);
                        }
                        btnpanelhorizontalalignment = horizontalalignment.right;
                        btnminimizevisibility = visibility.visible;
                        this.windowstate = windowstate.maximized;
                    }
                    else if (this.windowstate == windowstate.maximized)
                    {
                        this.bordermargin = new thickness(0, 0, 0, 0);
                        btnpanelhorizontalalignment = horizontalalignment.right;
                        btnminimizevisibility = visibility.visible;
                        this.windowstate = windowstate.normal;
                    }
                    else if (this.windowstate == windowstate.minimized)
                    {
                        this.bordermargin = new thickness(0, 0, 0, 0);
                        btnpanelhorizontalalignment = horizontalalignment.right;
                        btnminimizevisibility = visibility.visible;
                        this.windowstate = windowstate.normal;
                    }
                }
                if (parameter == 3) //关闭窗口
                {
                    this.close();
                }
            };
            this.windowbtncommand = windowbtncommand;
            this.statechanged += (s, e) =>
            {
                if (this.windowstate == windowstate.maximized)
                {
                    btnpanelhorizontalalignment = horizontalalignment.right;
                    btnminimizevisibility = visibility.visible;
                    double taskbarheight = systemparameters.primaryscreenheight - systemparameters.workarea.height;
                    double taskbarwidth = systemparameters.primaryscreenwidth - systemparameters.workarea.width;
                    if (taskbarwidth > 0)
                    {
                        this.bordermargin = new thickness(0, 0, taskbarwidth, 0);
                    }
                    if (taskbarheight > 0)
                    {
                        this.bordermargin = new thickness(0, 0, 0, taskbarheight);
                    }
                }
                if (this.windowstate == windowstate.normal)
                {
                    this.bordermargin = new thickness(0, 0, 0, 0);
                    btnpanelhorizontalalignment = horizontalalignment.right;
                    btnminimizevisibility = visibility.visible;
                }
            };
            #endregion

        }

        protected void onpropertychanged(string name)
        {
            if (propertychanged != null)
            {
                propertychanged(this, new propertychangedeventargs(name));
            }
        }
    }
}
view code

窗体最小化、最大化、关闭按钮的命令windowbtncommand:

WPF自定义Window窗体样式
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.input;

namespace suncreate.common.controls
{
    public class windowbtncommand : icommand
    {
        public action<int> doaction { get; set; }

        public event eventhandler canexecutechanged
        {
            add { commandmanager.requerysuggested += value; }
            remove { commandmanager.requerysuggested -= value; }
        }

        public bool canexecute(object parameter)
        {
            return true;
        }

        public void execute(object parameter)
        {
            if (doaction != null)
            {
                doaction(convert.toint32(parameter));
            }
        }
    }
}
view code

使用windowex类的示例代码:

WPF自定义Window窗体样式
<ui:windowex x:class="suncreate.common.controls.demo.mywindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ui="clr-namespace:suncreate.common.controls;assembly=suncreate.common.controls"
        title="视频播放视频播放abcdefg" height="300" width="500" windowstartuplocation="centerscreen"
        btnminimizevisibility="visible" btnmaximizevisibility="visible" >
    <window.resources>
        <resourcedictionary>
            <resourcedictionary.mergeddictionaries>
                <resourcedictionary source="/suncreate.common.controls;component/themes/scrollviewer.xaml"/>
                <resourcedictionary source="/suncreate.common.controls;component/themes/controlsresource.xaml"/>
            </resourcedictionary.mergeddictionaries>
        </resourcedictionary>
    </window.resources>
    <grid>
        <stackpanel>
            <border margin="10">
                <button height="30" width="80" content="测试" style="{staticresource stltxtbtn}" horizontalalignment="left" click="button_click" />
            </border>
            <border margin="10">
                <textblock text="测试内容abc"></textblock>
            </border>
        </stackpanel>
    </grid>
</ui:windowex>
view code

效果图:

WPF自定义Window窗体样式

窗体最小化效果图:

WPF自定义Window窗体样式