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

[WPF 学习] 9.自定义一个Window的样子

程序员文章站 2022-05-23 18:49:29
基本思路是把原来的WindowStyle设置为None,然后自己弄一个标题栏 一、xmal 二、后台代码(几个事件) ......

基本思路是把原来的windowstyle设置为none,然后自己弄一个标题栏

一、xmal

<window x:class="ykcore.windowsetup" name="winmain"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ykcore"
        mc:ignorable="d"
        windowstate="maximized"
        windowstyle="none" sizechanged="winmain_sizechanged"
        height="450" width="800" borderthickness="0" background="{dynamicresource backgroundcolor}"
    xmlns:theme="clr-namespace:microsoft.windows.themes;assembly=presentationframework.aero2">
   
        <!-- 最大化按钮形状 -->
        <pathgeometry x:key="pathmaximize">
            <pathgeometry.figures>
                m1,1  l1 ,11 l11,11 l11,1 z m0,0 l12,0 l12,12 l0,12 z
            </pathgeometry.figures>
        </pathgeometry>
        <!-- 还原按钮形状 -->
        <pathgeometry x:key="pathrestore">
            <pathgeometry.figures>
                m1,3 l1,11 l9,11 l9,3 z m3,1 l3,2 l10,2 l10,9 l11,9 l11,1 z m2 ,0 l12,0 l12,10 l10,10 l10,12 l0,12 l0,2 l2 ,2 z
            </pathgeometry.figures>
        </pathgeometry>

    </window.resources>
    <grid>
        <grid.rowdefinitions>
            <rowdefinition height="50"></rowdefinition>
            <rowdefinition height="*"></rowdefinition>
        </grid.rowdefinitions>
        <border borderbrush="{dynamicresource forecolor}" borderthickness="2,2,2,0" padding="10,0">
            <grid>
<!--自己画个图标,也就是一个圆角矩形里面写了个字母m-->
                <rectangle width="30" height="28" stroke="yellow" strokethickness="1" horizontalalignment="left" radiusx="5" radiusy="5" />
                <textblock text="m" foreground="yellow" margin="10,0,0,0">
                    <textblock.rendertransform>
                        <skewtransform anglex="-15"/>
                    </textblock.rendertransform>
                </textblock>


                <stackpanel orientation="horizontal" verticalalignment="center" horizontalalignment="right">

                    <button x:name="btnmin"  width="28" height="28"   click="btnmin_click" >
                        <path  data="m0,5 l12,5 l12,6 l0,6 z" fill="{dynamicresource forecolor}" width="12" height="12"/>
                    </button>
                    <button x:name="btnmax"  width="28" height="28"  click="btnmax_click">
                        <path x:name="pmax" fill="{dynamicresource forecolor}" width="12" height="12"/>
                    </button>
                    <button x:name="btnclose"  width="28" height="28"  click="btnclose_click" >
                        <button.content>
                            <path data="m1,0 l6,5 l11,0 l12,1 l7,6 l12,11 l11,12 l6,7 l1,12 l0,11 l5,6 l0,1 z" fill="{dynamicresource forecolor}" width="12" height="12"/>
                        </button.content>
                    </button>
                </stackpanel>
            </grid>
        </border>
        <border grid.row="1" borderbrush="{dynamicresource forecolor}" borderthickness="2" padding="10">
<!--原来的window的content-->
        </border>
    </grid>
</window>

二、后台代码(几个事件)

        private void btnclose_click(object sender, routedeventargs e)
        {
            application.current.shutdown();
        }

        private void btnmax_click(object sender, routedeventargs e)
        {
            this.windowstate = this.windowstate != windowstate.maximized ? windowstate.maximized : windowstate.normal;
        }

        private void btnmin_click(object sender, routedeventargs e)
        {
            this.windowstate = windowstate.minimized;
        }

        private void winmain_sizechanged(object sender, sizechangedeventargs e)
        {
            pmax.data = this.windowstate == windowstate.maximized ? resources["pathrestore"] as geometry : resources["pathmaximize"] as geometry;
        }