WPF-一个简单登录界面

一个简单登录界面


文章目录


一、效果展示

二、准备代码

创建一个WPF工程,创建名为 Login5 的WPF项目。

添加Nuget包

MaterialDesignThemes

界面的整体布局和样式代码

xml 复制代码
<Window x:Class="Login5.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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:local="clr-namespace:Login5"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        ResizeMode="NoResize" WindowStartupLocation="CenterScreen"
        WindowStyle="None">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Red.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />

            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid MouseDown="Grid_MouseDown">
        <Grid.Background>
            <LinearGradientBrush StartPoint="0.1,0" EndPoint="0.9,1">
                <GradientStop Offset="1" Color="#FFE63070"/>
                <GradientStop Offset="0" Color="#FFFE8704"/>
            </LinearGradientBrush>
        </Grid.Background>
        <Border Height="390" VerticalAlignment="Top" 
                Background="#100E17" CornerRadius="0 0 180 0">
            <StackPanel Orientation="Horizontal">
                <StackPanel Width="350">
                    <Image Width="300" Height="300" Margin="30" 
                           VerticalAlignment="Top" Stretch="Fill" Source="pack://application:,,,/Login5;component/Images/left.png"/>
                </StackPanel>
                <StackPanel Width="350">
                    <StackPanel Margin="20,40">
                        <TextBlock Margin="20" FontFamily="Great Vibes"
                                   FontSize="38" Foreground="White" TextAlignment="Center">
                            用户登录
                        </TextBlock>
                        <StackPanel Margin="10" Orientation="Horizontal">
                            <materialDesign:PackIcon
                                Width="25" Height="25" Foreground="White"
                                Kind="User"/>
                            <TextBox x:Name="txtUserName" Width="250"
                                     Margin="10,0" BorderBrush="White"
                                     CaretBrush="#FFD94448" Foreground="White"
                                     SelectionBrush="#FFD94448"
                                     materialDesign:HintAssist.Hint="输入 用户名 / 邮箱"/>
                        </StackPanel>
                        <StackPanel Margin="10" Orientation="Horizontal">
                            <materialDesign:PackIcon
                                 Width="25" Height="25" Foreground="White"
                                 Kind="Lock"/>
                             <PasswordBox x:Name="txtPassword" Width="250"
                                      Margin="10,0" BorderBrush="White"
                                      CaretBrush="#FFD94448" Foreground="White"
                                      SelectionBrush="#FFD94448"
                                      materialDesign:HintAssist.Hint="********"/>
                        </StackPanel>
                        <StackPanel Margin="10" HorizontalAlignment="Center">
                            <Button  x:Name="btnLogin" Width="100" Height="40"
                                     materialDesign:ButtonAssist.CornerRadius="10"
                                     Background="#D94448" BorderBrush="#D94448" BorderThickness="2"
                                     Content="登录" Foreground="White" ToolTip="登录"
                                     Style="{StaticResource MaterialDesignRaisedButton}"/>
                        </StackPanel>
                    </StackPanel>
                </StackPanel>
                <StackPanel Width="100">
                    <Button x:Name="btnExit" Margin="10,20" Background="{x:Null}"
                            Click="btnExit_Click" Style="{StaticResource MaterialDesignFloatingActionButton}"
                            ToolTip="关闭">
                        <materialDesign:PackIcon Width="30" Height="30" 
                                                 Foreground="White" Kind="Close"/>
                    </Button>
                </StackPanel>
            </StackPanel>

        </Border>
    </Grid>
</Window>

MainWindow.xaml.cs

csharp 复制代码
using System.Windows;
using System.Windows.Input;

namespace Login5
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if(Mouse.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }
    }
}

相关推荐
hez20102 小时前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr
mudtools16 小时前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫19 小时前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools1 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
大飞pkz2 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
唐青枫2 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务2 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther2 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
阿幸软件杂货间2 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
sali-tec2 天前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#