MFC编程(二)
模拟登录窗口
- 使用类:
- Cwnd:组件基类
- CEdit:编辑框
- CString:字符串
- 使用函数:
- CWnd::SetWindowText(<String>):设置窗口标题文字
- CEdit::SetPasswordChar(char):设置密码框显示字符
- CEdit::SetFocus():设置编辑框为当前焦点
- 使用事件:
- EN_SETFOCUS:编辑框获取焦点事件
初始化一个CString变量用于保存用户名和密码
class CDemoDlg : public CDialog { // Construction public: CDemoDlg(CWnd* pParent = NULL); // standard constructor CString users[3][2]; ... } CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/) : CDialog(CDemoDlg::IDD, pParent) { //{{AFX_DATA_INIT(CDemoDlg) v_username = _T(""); v_password = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); this->users[0][0] = "test"; this->users[0][1] = "test"; this->users[1][0] = "admin"; this->users[1][1] = "123456"; this->users[2][0] = "calong"; this->users[2][1] = "calong"; }
当用户点击确定按钮判断用户名和密码是否输入正确
void CDemoDlg::OnOK() { // TODO: Add extra validation here UpdateData(true); if (v_username.IsEmpty()) { MessageBox("用户名不能为空"); c_username.SetFocus(); } else if (v_password.IsEmpty()) { MessageBox("密码不能为空"); c_password.SetFocus(); } else { for (int i = 0; i < 3; i ++) { for (int i = 0; i < 3; i ++) { if ((v_username == users[i][0]) && (v_password == users[i][1])) { MessageBox("欢迎您!"); CDialog::OnOK(); break; } if (i == 2) { MessageBox("对不起!您的用户名或密码有误!"); } } } }
本作品采用《CC 协议》,转载必须注明作者和本文链接