如何解决Visual Studio 2019 MFC C ++的``使''按钮打开另一个对话框?
我有一个基于对话框的MFC应用程序,它有两个对话框:
-
IDD_FITNESSAPP_LOGIN_DLG
-
IDD_FITNESSAPP_MAIN_DLG
在IDD_FITNESSAPP_LOGIN_DLG
上,我有一个按钮btnLogin
,该按钮运行某些凭据(到目前为止有效,并且也报告故障)。
如何做到这一点,以便当我在btnLogin
上单击IDD_FITNESSAPP_LOGIN_DLG
时,它指向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,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);
}
解决方法
1。Resourse View-> Dialog->Right click->Add Resource->Dlg
,然后为新Dlg添加类。
2。Dlg有两种类型。
模态对话盒
当用户要在对话框以外的应用程序上进行操作时,必须首先响应该对话框。
#include "CXXXDlg.h "
void CXXXDlg::OnBnClickedOk()
{
CMyNewDlg Dlg;
Dlg.DoModal();
}
无模式对话框
当用户打开非模式对话框时,其他窗口仍然可以操作。
#include "CXXXDlg.h"
void CXXXDlg::OnBnClickedOk()
{
CMyNewDlg *pDlg=new CMyNewDlg ;
pDlg->Create(IDD_DIALOG2,this);
pDlg->ShowWindow(SW_SHOW);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。