WPF登录窗体

简介:           利用闲暇时间帮一个亲戚做一个库存管理系统,由于是一个C/S应用,于是用WPF来做,也是第一次用WPF真正意义上的实践吧,下面是登录窗口相关代码: ...

          利用闲暇时间帮一个亲戚做一个库存管理系统,由于是一个C/S应用,于是用WPF来做,也是第一次用WPF真正意义上的实践吧,下面是登录窗口相关代码:

<Window x:Class="David.WPF.Login"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="登录" 
    WindowStartupLocation="CenterScreen"
    AllowsTransparency="True"
    Background="Transparent"
    WindowStyle="None"
    ShowInTaskbar="False"
    SizeToContent="WidthAndHeight"
    FocusManager.FocusedElement="{Binding ElementName=txtUserName}">
    <Window.Resources>
        <Style TargetType="Label">
            <Setter Property="Margin"
              Value="4"></Setter>
        </Style>
        <Style TargetType="TextBox">
            <Setter Property="Margin"
              Value="4"></Setter>
            <Setter Property="MinWidth"
              Value="200"></Setter>
            <Setter Property="HorizontalAlignment"
              Value="Left"></Setter>
        </Style>
        <Style TargetType="PasswordBox">
            <Setter Property="Margin"
              Value="4"></Setter>
            <Setter Property="MinWidth"
              Value="200"></Setter>
            <Setter Property="HorizontalAlignment"
              Value="Left"></Setter>
        </Style>
        <Style TargetType="Button">
            <Setter Property="Margin"
              Value="6"></Setter>
            <Setter Property="Padding"
              Value="4"></Setter>
            <Setter Property="MinWidth"
              Value="50"></Setter>
        </Style>
    </Window.Resources>
    <Border CornerRadius="10"
          BorderBrush="Gray"
          BorderThickness="3"
          Background="Beige"
          Margin="24"
          Padding="4">
        <Border.Effect>
            <DropShadowEffect Color="Gray"
                        Opacity=".50"
                        ShadowDepth="16" />
        </Border.Effect>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60" />
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <StackPanel Grid.Column="0"
                  Grid.Row="0"
                  Grid.RowSpan="4">
            	<Image x:Name="imgKey"
            		Margin="8"
            		Source="/David.WPF;component/skin/Key.jpg" Height="155">
            		<Image.Effect>
            			<DropShadowEffect Color="Gray"
            				Opacity=".50"
            				ShadowDepth="8" />
            		</Image.Effect>
            	</Image>
            </StackPanel>
            <Label Grid.Column="1"
             Grid.Row="0"
             Grid.ColumnSpan="2"
             FontSize="18.667"
             Margin="10,10,58,10" FontWeight="Bold" FontFamily="Microsoft YaHei" HorizontalAlignment="Center" VerticalAlignment="Center" Content="合肥佳诚物资库存管理系统V1.0" Foreground="#FFFF003A"/>
            <Label Grid.Column="1"
             Grid.Row="1" Margin="36,4,4,4" FontFamily="Microsoft YaHei" FontSize="13.333" FontWeight="Bold" Content="用户名 :"/>
            <TextBox Grid.Column="2"
               Grid.Row="1"
               ToolTip="请输入用户名"
               Name="txtUserName" Margin="4,4,0,4" Width="161.851" />
            <Label Grid.Column="1"
             Grid.Row="2" Margin="34,4,2,4" Content="密    码 :" FontSize="13.333" FontFamily="Microsoft YaHei" FontWeight="Bold"/>
            <PasswordBox Grid.Column="2"
                   Grid.Row="2"
                   ToolTip="请输入密码"
                   Name="txtPassword" Margin="4,4,0,0" Width="161.851" />
            <StackPanel Grid.Column="2"
                  Grid.Row="3"
                  Margin="10"
                  HorizontalAlignment="Center"
                  Orientation="Horizontal">
                <Button Name="btnLogin"
                IsDefault="True"
                Content="登 录"
                Click="btnLogin_Click" Foreground="#FF006E2A" FontFamily="Microsoft YaHei" FontWeight="Bold" Cursor="Hand">
                    <Button.Effect>
                        <DropShadowEffect Color="Gray"
                              Opacity=".50"
                              ShadowDepth="8" />
                    </Button.Effect>
                </Button>
                <Button x:Name="btnCancel"
                IsCancel="True"
                Content="退 出"
                Click="btnCancel_Click" RenderTransformOrigin="2.64,0.164" Foreground="Red" FontFamily="Microsoft YaHei" FontWeight="Bold" Cursor="Hand">
                    <Button.Effect>
                        <DropShadowEffect Color="Gray"
                              Opacity=".50"
                              ShadowDepth="8" />
                    </Button.Effect>
                </Button>
            </StackPanel>
        </Grid>
    </Border>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Syit.Model;
using Syit.BLL;
using System.Data;

namespace David.WPF
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Window
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            string uname = this.txtUserName.Text.Trim();
            string password = this.txtPassword.Password;
            tb_UsersBLL bll = new tb_UsersBLL();
            DataTable dt = bll.GetData(" UserName='" + uname + "'", "");
            if (dt.Rows.Count > 0)
            {
                //用户名存在
                if (dt.Rows[0]["UserPwd"].ToString().TrimEnd() == password)
                {
                    //密码正确
                    MainWindow mw = new MainWindow(dt.Rows[0]["UserID"].ToString().TrimEnd());
                    this.Close();
                    mw.Show();
                }
                else
                {
                    //密码不正确
                    MessagePopup.Show(this, "密码错误,请重新输入!",250);
                }
            }
            else
            {
                //用户名不存在
                MessagePopup.Show(this, "用户名不存在,请重新输入!", 250);

            }

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            //退出系统
            this.Close();
            Environment.Exit(0); //强制终止应用程序进程
        }
    }
}

这里使用了一个弹出提示窗口的操作类,调用方法是:

MessagePopup.Show(this, "用户名不存在,请重新输入!", 250);
                                                            

该MessagePopup类代码如下:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Threading;

namespace David.WPF
{
    public sealed class MessagePopup
    {
        public static void Show(UIElement parent, string message,double width)
        {
            parent.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() =>
                {
                    Popup popup = new Popup();
                    popup.StaysOpen = true;
                    popup.PlacementTarget = parent;
                    popup.Placement = PlacementMode.Center;

                    Border aroundBorder = new Border();
                    aroundBorder.BorderThickness = new Thickness(2);
                    aroundBorder.BorderBrush = Brushes.SteelBlue;

                    StackPanel aroundStackPanel = new StackPanel();
                    aroundStackPanel.MinWidth = width;
                    aroundStackPanel.MinHeight = 120;
                    aroundStackPanel.MaxWidth = 480;
                    aroundStackPanel.Background = Brushes.White;
                    aroundStackPanel.Orientation = Orientation.Vertical;
                    aroundStackPanel.FlowDirection = FlowDirection.LeftToRight;

                    DockPanel headDockPanel = new DockPanel();
                    headDockPanel.VerticalAlignment = VerticalAlignment.Top;
                    headDockPanel.Background = Brushes.SteelBlue;

                    TextBlock headTextBlock = new TextBlock();
                    headTextBlock.Margin = new Thickness(10, 0, 0, 0);
                    headTextBlock.Text = "提示";
                    headTextBlock.Height = 26;
                    headTextBlock.HorizontalAlignment = HorizontalAlignment.Left;
                    headTextBlock.VerticalAlignment = VerticalAlignment.Center;
                    headTextBlock.FontSize = 16;
                    headTextBlock.Focusable = false;
                    headTextBlock.IsHitTestVisible = false;
                    headTextBlock.Background = Brushes.SteelBlue;
                    headTextBlock.Foreground = Brushes.White;

                    TextBlock messageTextBlock = new TextBlock();
                    messageTextBlock.Text = message;
                    messageTextBlock.Margin = new Thickness(20, 20, 20, 10);
                    messageTextBlock.MinHeight = 28;
                    messageTextBlock.VerticalAlignment = VerticalAlignment.Top;
                    messageTextBlock.HorizontalAlignment = HorizontalAlignment.Left;
                    messageTextBlock.TextWrapping = TextWrapping.Wrap;

                    Button okButton = new Button();
                    okButton.Content = "OK";
                    okButton.Margin = new Thickness(0, 0, 20, 10);
                    okButton.Padding = new Thickness(25, 3, 25, 3);
                    okButton.HorizontalAlignment = HorizontalAlignment.Right;
                    okButton.Click += delegate
                    {
                        popup.IsOpen = false;
                        parent.IsEnabled = true;
                    };

                    headDockPanel.Children.Add(headTextBlock);
                    aroundStackPanel.Children.Add(headDockPanel);
                    aroundStackPanel.Children.Add(messageTextBlock);
                    aroundStackPanel.Children.Add(okButton);
                    aroundBorder.Child = aroundStackPanel;
                    popup.Child = aroundBorder;

                    parent.IsEnabled = false;
                    popup.IsOpen = true;
                }));
        }
    }
}


相关文章
|
C# 编解码
WPF C# 多屏情况下,实现窗体显示到指定的屏幕内
原文:WPF C# 多屏情况下,实现窗体显示到指定的屏幕内 针对于一个程序,需要在两个显示屏上显示不同的窗体,(亦或N个显示屏N个窗体),可以使用如下的方式实现。
4913 0
WPF控件和窗体一起放大一起缩小
WPF控件和窗体一起放大一起缩小
214 0
|
C# 容器
WPF框架下,窗体的嵌套显示
WPF框架下,窗体的嵌套显示
212 0
|
C#
WPF登录跳转到指定页面
WPF登录跳转到指定页面
179 0
|
C# 索引
WPF实用指南二:移除窗体的图标
原文:WPF实用指南二:移除窗体的图标 WPF没有提供任何功能来移除窗体上的icon图标。一般的做法是设置一个空白的图标,如下图1: 这种做法在窗体边框与标题之间仍然会保留一片空白。
1229 0
|
C#
WPF透明窗体制作
原文:WPF透明窗体制作 窗体的样式: 其中Border用来实现透明效果,Grid用来呈现窗体内的控件,为了避免窗体内的控件变成透明的,所以Border,Grid必须处在同一级别上。
900 0
|
C# 容器 前端开发
WPF Viewport3D 解决透视模式时窗体模糊
原文:WPF Viewport3D 解决透视模式时窗体模糊 最近折腾Viewport3D玩,遇到了一些诡异的问题,研究一下略有心得,特此和大家分享~ 三维图形概述: https://msdn.microsoft.com/zh-cn/library/ms747437.aspx 概要 三维坐标系         二维图形的 WPF 坐标系将原点定位在呈现区域(通常是屏幕)的左上角。
1023 0
|
C#
wpf无边框窗体移动和大小调整
原文:wpf无边框窗体移动和大小调整   using System; using System.Windows; using System.
1635 0
|
C# Windows
WPF 窗体中获取键盘和鼠标无操作时的超时提示
原文:WPF 窗体中获取键盘和鼠标无操作时的超时提示 通过调用Windows API中的GetLastInputInfo来获取最后一次输入的时间 using System;using System.
892 0
|
C#
WPF窗体隐藏鼠标光标的方法
原文:WPF窗体隐藏鼠标光标的方法 要引用 System.Windows.Input;   Mouse.OverrideCursor = Cursors.
1764 0