简单的Windows登陆界面设计

时间:2023-03-09 02:47:43
简单的Windows登陆界面设计
要求:
1.用户名必须为字母。
 //限定用户名必须为字母
private void txtName_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z'))
{
e.Handled = false;
}
else {
MessageBox.Show("用户名只能为字母!");
e.Handled = true;
}
}
2.光标进入文本框时背景蓝色,文字白色;光标离开文本框时,背景白色,文字黑色。
界面:
   //光标进入文本框时,背景为蓝色,字体为白色;
//光标离开文本框时,背景为白色,字体为黑色。
private void txtName_Enter(object sender, EventArgs e)
{
txtName.ForeColor = Color.White;
txtName.BackColor = Color.Blue;
} private void txtName_Leave(object sender, EventArgs e)
{
txtName.BackColor = Color.White;
txtName.ForeColor = Color.Black;
}

3.当输入用户名“admin”和密码“123”之后,单击”确定“按钮,系统将弹出消息框以显示输入正确,否则显示用户名或密码错误的提示信息。

 private void btnLogin_Click(object sender, EventArgs e)
{
string userName = txtName.Text;
string password = txtPwd.Text;
if (userName == "admin" && password == "")
{
MessageBox.Show("欢迎进入个人理帐系统!", "登陆成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("您输入的用户名或密码错误!", "登录失败!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}

4.单击”取消“按钮,清除输入信息,并将光标定位在txtName文本框中。

   private void btnCancel_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtPwd.Text = "";
txtName.Focus();
}

5.最终界面:

简单的Windows登陆界面设计简单的Windows登陆界面设计
小技巧:为label设置Image属性,为了让图片完整显示出来,需要把label的AutoSize属性设置为false,然后适当拉大label大小。还要注意,ImageAlign属性设置为MiddleLeft,TextAlign属性设置为MiddleRight。

Notice:

(1)ico:是Windows图标文件格式的一种,可以存储单个图案、多尺寸、多色板的图标文件。
(2)MessageBox:消息框,显示一个模态对话框,其中包含一个系统图标、 一组按钮和一个简短的特定于应用程序消息,如状态或错误的信息。
(3)Button的快捷键通过设置Text属性为”取消(&C)“实现。
(4)此练习使用的软件为Visual Studio 2012,图形资源由VS提供,据说在VS的安装文件夹Common7\ImageLibrary中能找到,没有的话,可以到官网下载。