WPF开发登录窗口之——窗口布局

e29d2dce5b4286847d2571217af622c5.png

WPF开发者QQ群: 340500857  | 微信群 -> 进入公众号主页 加入组织

f65a655ebe2cc352b4d0ed2084cab386.png 

01

代码如下

一、创建项目,命名为LoginWindow,然后创建以下目录结构:

53c5e05318ff42799a088c79d1df7d37.png

二、导入图片

8198704276b9a789582b1c2ae2bb060a.png

三、创建资源字典。一共创建四个:ButtonStyle、WindowStyle、

TextBoxStyle、PasswordBoxStyle:

73ef8ccf5cc9b79eda8a4ac2bc2aecef.png

四、引用资源。打开“App.xaml”,添加资源引用:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- 按钮样式 -->
            <ResourceDictionary Source="CustomStyle/ButtonStyle.xaml"/>
            <!-- 窗口样式 -->
            <ResourceDictionary Source="CustomStyle/WindowStyle.xaml"/>
            <!-- 文本框样式 -->
            <ResourceDictionary Source="CustomStyle/TextBoxStyle.xaml"/>
            <!-- 密码框样式 -->
            <ResourceDictionary Source="CustomStyle/PasswordBoxStyle.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

五、添加按钮样式本项目只需要两个按钮样式:关闭按钮与登录/注册按钮。打开“ButtonStyle.xaml”,添加以下样式:

<!-- 关闭按钮 -->
<Style x:Key="CloseButtonStyle" TargetType="Button">
    <Setter Property="Opacity" Value="0.5"/>
    <Setter Property="Cursor" Value="Hand"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border Name="Border" Background="Transparent">
                    <Canvas Width="10" Height="10" ClipToBounds="True">
                        <Line x:Name="Line01" Stroke="White" X1="0" Y1="0" X2="10" Y2="10"/>
                        <Line x:Name="Line02" Stroke="White" X1="10" Y1="0" X2="0" Y2="10"/>
                    </Canvas>
                </Border>
                <!-- 触发器 -->
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Opacity" Value="0.75"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Opacity" Value="1"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<!-- 登录/注册按钮 -->
<Style TargetType="Button">
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Cursor" Value="Hand"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border x:Name="Border" Background="#4370F5">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <!-- 触发器 -->
                <ControlTemplate.Triggers>
                    <!-- 鼠标悬停时 -->
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="#5B84FE"/>
                    </Trigger>
                    <!-- 鼠标按下时 -->
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="#3761DF"/>
                    </Trigger>
                    <!-- 禁用时 -->
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Opacity" Value="0.3"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

六、添加窗口样式打开“WindowStyle.xaml”,添加以下样式:

<Style x:Key="NormalWindow" TargetType="Window">
    <Setter Property="Background" Value="#80222324"/>
    <Setter Property="FontFamily" Value="NSimSun"/>
    <Setter Property="FontSize" Value="12"/>
    <Setter Property="TextOptions.TextFormattingMode" Value="Display"/>
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="WindowChrome.WindowChrome">
        <Setter.Value>
            <WindowChrome GlassFrameThickness="1" UseAeroCaptionButtons="False" ResizeBorderThickness="0" CaptionHeight="100" CornerRadius="0"/>
        </Setter.Value>
    </Setter>
    <!-- 模板 -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Window">
                <!-- 外观 -->
                <Grid>
                    <!-- 分区 -->
                    <Grid.RowDefinitions>
                        <!-- 标题栏 -->
                        <RowDefinition Height="100"/>
                        <!-- 客户区 -->
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <!-- 第一块:标题栏 -->
                    <Grid>
                        <!-- 背景色 -->
                        <Grid Height="200" VerticalAlignment="Center" Opacity="0.9">
                            <Grid.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                    <GradientStop Offset="0" Color="#4370F5"/>
                                    <GradientStop Offset="1" Color="#AE088D"/>
                                </LinearGradientBrush>
                            </Grid.Background>
                        </Grid>
                        <!-- 背景图 -->
                        <Image Source="Image/Background.png" Width="360" VerticalAlignment="Bottom"/>
                        <!-- 标识 -->
                        <Image Source="Image/Logo.png" Width="114" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,20,0,0"/>
                        <!-- 关闭按钮 -->
                        <Button x:Name="CloseButton" Width="30" Height="30" HorizontalAlignment="Right" VerticalAlignment="Top"
                                Style="{StaticResource CloseButtonStyle}" WindowChrome.IsHitTestVisibleInChrome="True"/>
                    </Grid>
                    <!-- 第二块:客户区 -->
                    <Grid Grid.Row="1" Background="White">
                        <AdornerDecorator>
                            <ContentPresenter/>
                        </AdornerDecorator>
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

七、这个模板结构很简单,就是普通的上下结构,唯一需要解释的布局是“背景色”,标题栏高度是100,背景色的高度是200,且垂直居中,这种布局的效果如下图:

14c2e7ab53978b6c08f2067731333241.png

渐变上下部分都被裁剪掉了

八、应用窗口样式打开“MainWindow.xaml”,设置以下属性:

<Window x:Class="LoginWindow.MainWindow"
        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:LoginWindow"
        mc:Ignorable="d"
        Title="登录" Height="372" Width="360" ResizeMode="NoResize"
        FontFamily="NSimSun" FontSize="13px" WindowStartupLocation="CenterScreen"
        Style="{StaticResource NormalWindow}">
    <Grid>


    </Grid>
</Window>

4c4a5b6c07d6877666a25edded7d3a8d.png

源码地址如下

http://www.gjiang.club/article?ID=6157079391ee9e52232fddcf

github:https://github.com/yanjinhuagood/WPFDevelopers.git

gitee:https://gitee.com/yanjinhua/WPFDevelopers.git

WPF开发者QQ群: 340500857 

blogs: https://www.cnblogs.com/yanjinhua

Github:https://github.com/yanjinhuagood

出处:https://www.cnblogs.com/yanjinhua

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

转载请著名作者 出处 https://github.com/yanjinhuagood

5d24c589a24af779a7c9efd0896cb1be.png

扫一扫关注我们,

52e0977dedf87afaabcbab1e2b90fa6d.gif

更多技能早知道!

a083bc3cb597f0cd7a57bf417dcb863a.gif

点击阅读原文可跳转至源代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值