(一)简单的wpf登陆页面

本学期末的大作业是实现一个简单的地铁售票系统。自己带着几个组员便根据老师的要求开始做,有组员负责设计简单的页面,然后由我对页面进行简单的美化,并实现前后台逻辑。此系列博客将根据情况逐步更新项目进度。

先看页面效果:

wpf_login

页面的背景是一张图片,顶端的Login 是一个lable,下方分别是2个textbox,2个button,1个textBlock中嵌套一个hyperlink.

1.当双击双击输入框时,会自动清除框内文字

2.点击Cancel,自动退出项目

3.点击OK,校验并登陆

4.元素居中显示,并自动适应当前屏幕

页面xaml文件代码:

<Window x:Class=”subway.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:subway”
mc:Ignorable=”d”
Title=”MainWindow” Margin=”0” Height=”1478.562” Width=”3010.497” WindowState=”Maximized”>
<Window.Background>
<ImageBrush ImageSource=”Image/login_bg.jpg”/>
</Window.Background>
<Viewbox>
<Grid Height=”591” Width=”1022”>
<!–<Grid.Background>
<ImageBrush ImageSource=”/Image/login_bg.jpg” />
</Grid.Background>–>

    &lt;Label x:Name="label" Content="Login" Foreground="White" Margin="122,43,136,0" VerticalAlignment="Top" FontFamily="微软雅黑" FontSize="45px"  HorizontalContentAlignment="Center" /&gt;
    &lt;TextBox x:Name="textBox_username" Margin="374,163,382,380" TextWrapping="Wrap" Text="username"  Opacity="0.5"  FontFamily="微软雅黑" FontSize="25px" /&gt;
    &lt;PasswordBox x:Name="passwordBox_password" Password="********" Margin="374,232,382,314"    Opacity="0.5" FontFamily="微软雅黑" FontSize="25px" ToolTip="please  input your password"/&gt;
    &lt;Button x:Name="button" IsDefault="True" Content="OK" HorizontalAlignment="Left" Margin="370,335,0,0" VerticalAlignment="Top" Width="94" Height="41" Opacity="0.7" FontFamily="微软雅黑" FontSize="20px" Click="button_Click" &gt;
        &lt;Button.Template&gt;
            &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
                &lt;Border BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="0" CornerRadius="8,8,8,8" Name="PART_Background"&gt;
                    &lt;Border.Background&gt;
                        &lt;LinearGradientBrush EndPoint="0,1" StartPoint="0,0"&gt;
                            &lt;GradientStop Color="White" Offset="0.0" /&gt;
                            &lt;GradientStop Color="Silver" Offset="0.5" /&gt;
                            &lt;GradientStop Color="White" Offset="0.0" /&gt;
                        &lt;/LinearGradientBrush&gt;
                    &lt;/Border.Background&gt;
                    &lt;ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" /&gt;
                &lt;/Border&gt;
                &lt;ControlTemplate.Triggers&gt;
                    &lt;Trigger Property="UIElement.IsMouseOver" Value="True"&gt;
                        &lt;Setter Property="Border.Background" TargetName="PART_Background"&gt;
                            &lt;Setter.Value&gt;
                                &lt;LinearGradientBrush EndPoint="0,1" StartPoint="0,0"&gt;
                                    &lt;GradientStop Color="Silver" Offset="0.0" /&gt;
                                    &lt;GradientStop Color="White" Offset="0.5" /&gt;
                                    &lt;GradientStop Color="Silver" Offset="0.0" /&gt;
                                &lt;/LinearGradientBrush&gt;
                            &lt;/Setter.Value&gt;
                        &lt;/Setter&gt;
                    &lt;/Trigger&gt;
                    &lt;Trigger Property="ButtonBase.IsPressed" Value="True"&gt;
                        &lt;Setter Property="UIElement.Effect"&gt;
                            &lt;Setter.Value&gt;
                                &lt;DropShadowEffect BlurRadius="10" Color="Black" Direction="0" Opacity="0.6" RenderingBias="Performance" ShadowDepth="0" /&gt;
                            &lt;/Setter.Value&gt;
                        &lt;/Setter&gt;
                    &lt;/Trigger&gt;
                &lt;/ControlTemplate.Triggers&gt;
            &lt;/ControlTemplate&gt;
        &lt;/Button.Template&gt;
    &lt;/Button&gt;
    &lt;Button x:Name="button1" IsCancel="True" Content="Cancel" HorizontalAlignment="Left" Margin="546,335,0,0" VerticalAlignment="Top" Width="94" Height="41" Opacity="0.7" FontFamily="微软雅黑" FontSize="20px" Click="button1_Click" RenderTransformOrigin="0.553,1.355" &gt;
        &lt;Button.Template&gt;
            &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
                &lt;Border BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="0" CornerRadius="8,8,8,8" Name="PART_Background"&gt;
                    &lt;Border.Background&gt;
                        &lt;LinearGradientBrush EndPoint="0,1" StartPoint="0,0"&gt;
                            &lt;GradientStop Color="White" Offset="0.0" /&gt;
                            &lt;GradientStop Color="Silver" Offset="0.5" /&gt;
                            &lt;GradientStop Color="White" Offset="0.0" /&gt;
                        &lt;/LinearGradientBrush&gt;
                    &lt;/Border.Background&gt;
                    &lt;ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" /&gt;
                &lt;/Border&gt;
                &lt;ControlTemplate.Triggers&gt;
                    &lt;Trigger Property="UIElement.IsMouseOver" Value="True"&gt;
                        &lt;Setter Property="Border.Background" TargetName="PART_Background"&gt;
                            &lt;Setter.Value&gt;
                                &lt;LinearGradientBrush EndPoint="0,1" StartPoint="0,0"&gt;
                                    &lt;GradientStop Color="Silver" Offset="0.0" /&gt;
                                    &lt;GradientStop Color="White" Offset="0.5" /&gt;
                                    &lt;GradientStop Color="Silver" Offset="0.0" /&gt;
                                &lt;/LinearGradientBrush&gt;
                            &lt;/Setter.Value&gt;
                        &lt;/Setter&gt;
                    &lt;/Trigger&gt;
                    &lt;Trigger Property="ButtonBase.IsPressed" Value="True"&gt;
                        &lt;Setter Property="UIElement.Effect"&gt;
                            &lt;Setter.Value&gt;
                                &lt;DropShadowEffect BlurRadius="10" Color="Black" Direction="0" Opacity="0.6" RenderingBias="Performance" ShadowDepth="0" /&gt;
                            &lt;/Setter.Value&gt;
                        &lt;/Setter&gt;
                    &lt;/Trigger&gt;
                &lt;/ControlTemplate.Triggers&gt;
            &lt;/ControlTemplate&gt;
        &lt;/Button.Template&gt;
    &lt;/Button&gt;
    &lt;TextBlock  HorizontalAlignment="Left" Margin="576,282,0,0" VerticalAlignment="Top" Foreground="#216380"&gt;
    &lt;Hyperlink NavigateUri="../Admin/Admin_getPassword" Click="Hyperlink_getPWD_Click" &gt;找回密码. . . &lt;/Hyperlink&gt;
    &lt;/TextBlock&gt;
&lt;/Grid&gt;

</Viewbox>



后台逻辑代码:

在构造函数中设置自动适应当前屏幕

       double x = SystemParameters.WorkArea.Width;//得到屏幕工作区域宽度
double y = SystemParameters.WorkArea.Height;//得到屏幕工作区域高度
double x1 = SystemParameters.PrimaryScreenWidth;//得到屏幕整体宽度
double y1 = SystemParameters.PrimaryScreenHeight;//得到屏幕整体高度
this.Width = x1;//设置窗体宽度
this.Height = y1;//设置窗体高度
this.Title = “登陆”;

 

双击输入框

    textBox_username.MouseDoubleClick += BoxMouseDoubleClick;
passwordBox_password.MouseDoubleClick += BoxMouseDoubleClick;
private void BoxMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
passwordBox_password.Password = “”;
textBox_username.Text = “”;
}


 

点击Cancel

this.Close();


点击OK

   /// <summary>
/// 点击OK 实现登陆
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
private void button_Click(object sender, RoutedEventArgs e)
{
BaseFunc baseFc = new BaseFunc();
username = textBox_username.Text.Trim();

    string password = passwordBox_password.Password.Trim();
    if(username==""||password=="")
    {
        MessageBox.Show("请输入正确信息");
        return;
    }
    else
    {
        bool isLogin = baseFc.GetLoginCheck(username, password);
        if(isLogin)
        {
            //this.Close();
            // MessageBox.Show("登陆成功");
            chooseSystem chose = new chooseSystem();
            chose.ShowDialog();
        }
        else
        {
            MessageBox.Show("登陆失败,无此用户或信息错误");
        }
    }
    MessageBox.Show(username);

}</code></pre>

找回密码的页面与的登陆相同,后台逻辑在之前的博客中已经写过,邮件不会当作垃圾邮件处理,目前不支持域邮箱。再次贴出代码(包括用户检测,邮件发送)

    #region 密码丢失

/// &lt;summary&gt;
/// 发送密码
/// &lt;/summary&gt;
/// &lt;param name="username"&gt;用户名&lt;/param&gt;
/// &lt;param name="password"&gt;新密码&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public string GetSendpasswrord(string username, string email)
{
    User member = new User();
    if (baseF.GetCheckUser(username))//检查是否已被注册
    {
        try
        {
            SqlConnection conn = connectdb.ConnectDataBase();
            //打开数据库
            conn.Open();
            //创建查询语句
            SqlCommand querySingleInfo = conn.CreateCommand();
                querySingleInfo.CommandText = "SELECT email FROM coustom where UserName=" + "'" + username + "'";
            SqlDataReader singleInfoReader = querySingleInfo.ExecuteReader();
            //有多行数据,用while循环
            while (singleInfoReader.Read())
            {
                member.email = singleInfoReader["email"].ToString().Trim();
            }
            if (member.email.Equals(email))
            {
                string pwd = "你的密码是: ";
                //获取密码
                pwd += Getpassrord(username);
                pwd += " , 请妥善保存";
                //发送邮件
                checkInfo = SendEmail(email, pwd);
            }
            else
            {
                checkInfo = "邮箱错误";
            }
        }
        catch (SqlException e)
        {
            Console.WriteLine("查询错误");
            checkInfo = "查询错误";
        }
    }
    else
        checkInfo = "用户名不存在";
    return checkInfo;
}

/// &lt;summary&gt;
/// 获取用户密码
/// &lt;/summary&gt;
/// &lt;param name="username"&gt;用户名&lt;/param&gt;
/// &lt;returns&gt;返回查询结果&lt;/returns&gt;
private string Getpassrord(string username)
{
    string password = "";
    try
    {
        SqlConnection conn = connectdb.ConnectDataBase();
        //打开数据库
        conn.Open();
        //创建查询语句
        SqlCommand querySingleInfo = conn.CreateCommand();
            querySingleInfo.CommandText = "SELECT Password FROM coustom where UserName=" + "'" + username + "'";
        SqlDataReader singleInfoReader = querySingleInfo.ExecuteReader();
        //有多行数据,用while循环
        while (singleInfoReader.Read())
        {
            password = singleInfoReader["Password"].ToString().Trim();
        }
        //关闭查询
        singleInfoReader.Close();
        //关闭数据库连接
        conn.Close();
    }
    catch (SqlException e)
    {
        password = "查询失败";
    }
    return password;
}

/// &lt;summary&gt;
/// 发送邮件
/// &lt;/summary&gt;
/// &lt;param name="email"&gt;邮件&lt;/param&gt;
/// &lt;param name="password"&gt;授权码&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
private string SendEmail(string email, string message)
{
    try
    {
        SmtpClient smtpClient = new SmtpClient();
        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式        
        smtpClient.Host = "smtp.qq.com";//指定SMTP服务器  (126)    
        System.Net.NetworkCredential nc = new System.Net.NetworkCredential("a@qq.com", "授权码");
        smtpClient.Credentials = nc;//用户名和授权码
        // 发送邮件设置        
        MailMessage mailMessage = new MailMessage("a@qq.com", email); // 发送人和收件人        
        mailMessage.Subject = "密码找回";//主题        
        mailMessage.Body = message;
        mailMessage.BodyEncoding = Encoding.UTF8;//正文编码        
        mailMessage.IsBodyHtml = true;//设置为HTML格式        
        mailMessage.Priority = MailPriority.High;//优先级
        smtpClient.Send(mailMessage);
        sendInfo = "发送成功";
    }
    catch (Exception e)
    {
        sendInfo = e.ToString();
    }
    return sendInfo;
}

#endregion</code></pre>


 

 

文章目录
|