利用闲暇时间帮一个亲戚做一个库存管理系统,由于是一个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; })); } } }