第三部分:登陆界面的设计及代码
一:效果图及界面设计
1. 效果图:
2. 界面设计:
(1)仿照QQ的登陆界面,右上角放了三个Label,用来做关闭、最小化和设置,使用了它们的Click事件当做按钮用。
(2)上面是一个pictureBox放一个图片。
(3)下面用tableLayoutPanel布局。
(4)tableLayoutPanel左侧图片和图片右下角的在线图放在一个Panel里,这个Panel占3行。
(5)用户名用ComboBox,密码框用TextBox,登陆用了个Button。
(6)用到的各个控件的Name:
关闭按钮:closewindow,最小化按钮:smallwindow,上面的大图片:pictureBox1,状态图标:zhuangtai;
用户名框:combuid,密码框:textpwd,登陆按钮:btnlogin,放左右两个图片imageList1;
右键菜单:contextMenuStrip1,右键菜单里用到的图片:imageList2。
二.:代码设计:
//记录更改状态,默认为1是在线 int zt = 1; //记录登录者的变量 private string uid; public string Uid { get { return uid; } set { uid = value; } } public Login() { InitializeComponent(); } //界面加载 private void Login_Load(object sender, EventArgs e) { //设置透明,需要先在设计中将要透明控件的backcolor设置为Transparent shezhi.Parent = this.pictureBox1; smallwindow.Parent = this.pictureBox1; closewindow.Parent = this.pictureBox1; zhuangtai.Parent = this.pictureBox2; //设置左右两个图片 pictureBox3.Image = imageList1.Images[0]; pictureBox4.Image = imageList1.Images[1]; }//关闭窗口按钮 private void closewindow_Click(object sender, EventArgs e) { this.Close(); } //关闭按钮鼠标放上去变色 private void closewindow_MouseEnter_1(object sender, EventArgs e) { closewindow.BackColor = Color.LightCoral; } //关闭按钮鼠标离开恢复颜色 private void closewindow_MouseLeave_1(object sender, EventArgs e) { closewindow.BackColor = Color.Transparent; } //最小化按钮最小化 private void smallwindow_Click_1(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; } //实现鼠标点在上面大图片上拖动窗体的代码 int x; int y; private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { x = e.X; y = e.Y; } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left) { this.Left = this.Left + (e.X - x); this.Top = this.Top + (e.Y - y); } } //密码框获得焦点时改为密码框,清空,字体颜色变化 private void textpwd_Enter(object sender, EventArgs e) { textpwd.Text = ""; textpwd.ForeColor = Color.Black; textpwd.PasswordChar = Convert.ToChar("●"); }
//用户名框鼠标点进去时,清空,字体颜色变化 private void combuid_MouseDown_1(object sender, MouseEventArgs e) { combuid.Text = ""; combuid.ForeColor = Color.Black; } //登录按钮按下时 private void btnlogin_Click_1(object sender, EventArgs e) { //取用户名密码,查询数据库 string uid = combuid.Text.ToString(); string pwd = textpwd.Text.ToString(); UsersTableAdapter adp = new UsersTableAdapter(); qqdata.UsersDataTable table = adp.LoginCheck(uid,pwd); //判断登录是否成功 if (table.Rows.Count > 0) { //成功之后this.DialogResult设为OK this.DialogResult = DialogResult.OK; Uid = table[0].UserName; //将用户名传到Uid adp.UpdateStateByCode(zt, uid); //将状态更改 adp.UpdateLasttimeByCode(DateTime.Now, uid); //将登录时间记录 } else { MessageBox.Show("账号密码错误!"); } } //头像右下角图标点击的时候弹出右键菜单
private void zhuangtai_Click_1(object sender, EventArgs e)
{
contextMenuStrip1.Show(zhuangtai, 0, 18);
}
//右键菜单选择在线的时候
private void 在线ToolStripMenuItem_Click(object sender, EventArgs e)
{
zhuangtai.Image = imageList2.Images[3];
zt = Convert.ToInt32(在线ToolStripMenuItem.Tag);
}
//右键菜单选择隐身的时候
private void 隐身ToolStripMenuItem_Click(object sender, EventArgs e)
{
zhuangtai.Image = imageList2.Images[2];
zt = Convert.ToInt32(隐身ToolStripMenuItem.Tag);
}
//右键菜单选择忙碌的时候
private void 忙碌ToolStripMenuItem_Click(object sender, EventArgs e)
{
zhuangtai.Image = imageList2.Images[1];
zt = Convert.ToInt32(忙碌ToolStripMenuItem.Tag);
}
//右键菜单选择离开的时候
private void 离开ToolStripMenuItem_Click(object sender, EventArgs e)
{
zhuangtai.Image = imageList2.Images[0];
zt = Convert.ToInt32(离开ToolStripMenuItem.Tag);
}