使用MFC编写登录界面
时间: 2024-05-30 12:14:25 浏览: 98
可以直接回答该问题,以下是一个示例代码:
在 Visual Studio 中创建一个 MFC 应用程序项目,选择“单文档”类型。在“添加类向导”中,选择“MFC 类”模板,然后选择“对话框”类。在对话框类向导中,选择“预定义的对话框”,然后选择“登录对话框”。
在对话框类的 OnInitDialog 函数中,可以添加一些代码来初始化登录对话框,例如设置对话框标题、设置图标等。在对话框上添加两个文本框和一个“登录”按钮。在“登录”按钮的单击事件中,可以添加一些代码来验证用户名和密码,并决定是否允许用户登录。
示例代码如下:
BOOL CLoginDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
SetWindowText(_T("My Login Dialog"));
return TRUE; // return TRUE unless you set the focus to a control
}
void CLoginDlg::OnOK()
{
CString username, password;
GetDlgItemText(IDC_EDIT_USERNAME, username);
GetDlgItemText(IDC_EDIT_PASSWORD, password);
if (username == _T("admin") && password == _T("admin"))
{
MessageBox(_T("Login successfully!"));
CDialogEx::OnOK();
}
else
{
MessageBox(_T("Invalid username or password!"));
}
}
当用户单击“登录”按钮时,如果用户名和密码都是“admin”,则显示一个消息框,表示用户已成功登录。否则,显示一个消息框,指示用户名或密码无效。如果用户成功登录,对话框将关闭。
阅读全文
相关推荐


















