WPF登入表單

來源:互聯網
上載者:User

          利用閑暇時間幫一個親戚做一個庫存管理系統,由於是一個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;                }));        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.