【发布时间】:2021-01-24 03:19:05
【问题描述】:
我有一个基于对话框的 MFC 应用程序,它有两个对话框:
IDD_FITNESSAPP_LOGIN_DLGIDD_FITNESSAPP_MAIN_DLG
在IDD_FITNESSAPP_LOGIN_DLG 上,我有一个按钮btnLogin,它通过一些凭据运行(到目前为止工作,并且也报告失败)。
如何做到这一点,当我在IDD_FITNESSAPP_LOGIN_DLG 上单击btnLogin 时,它会导致IDD_FITNESSAPP_MAIN_DLG?
我在Dlg.DoModal() 上读了一点,但如果需要的话,我不明白。
这是“点击了btnLogin”按钮的代码:
void CFitnessAppMFC2Dlg::OnBnClickedbtnlogin()
{
// TODO: Add your control notification handler code here
UpdateData();
char UsernameFromFile[20], PasswordFromFile[20];
FILE* fleCredentials;
bool ValidLogin = false;
if (m_Username == "")
{
AfxMessageBox(_T("You must provide a username and a password or click Cancel"));
return;
}
if (m_Password == "")
{
AfxMessageBox(_T("Invalid Login"));
return;
}
try {
// Open the file for reading
fleCredentials = fopen("credentials.txt", "r");
// Scan the file from beginning to end
while (!feof(fleCredentials))
{
// Read a username
fscanf(fleCredentials, "%s", UsernameFromFile);
// Compare the typed username with the username from the file
CT2A ascii(m_Username);
if (strcmp(ascii, UsernameFromFile) == 0)
{
// With the current username, retrieve the corresponding password
fscanf(fleCredentials, "%s", PasswordFromFile);
// Compare the typed password with the one on file
CT2A ascii2(m_Password);
if (strcmp(ascii2, PasswordFromFile) == 0)
{
ValidLogin = true;
}
else
ValidLogin = false;
}
}
if (ValidLogin == true)
OnOK();
else
{
AfxMessageBox(_T("Invalid Credentials. Please try again"));
this->m_EditUsername.SetFocus();
}
fclose(fleCredentials);
}
catch (...)
{
AfxMessageBox(_T("Could not validate the credentials"));
}
UpdateData(FALSE);
}
【问题讨论】:
-
是的,
DoModal()就是您所需要的。
标签: c++ visual-studio winapi mfc