I have a Dialog-based MFC app, and it has two Dialog Boxes:
IDD_FITNESSAPP_LOGIN_DLG
IDD_FITNESSAPP_MAIN_DLG
On IDD_FITNESSAPP_LOGIN_DLG
, I have a button, btnLogin
, that runs through some credentials (working so far, and reports failures too).
How do I make it so that when I click btnLogin
on IDD_FITNESSAPP_LOGIN_DLG
, it leads to IDD_FITNESSAPP_MAIN_DLG
?
I read a little on Dlg.DoModal()
but I don't understand that if its needed.
Here's the code for the "on button clicked 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);
}
1. Resourse View-> Dialog->Right click->Add Resource->Dlg
, Then add class for the new Dlg.
2.There are two types of Dlgs.
When the user wants to operate on applications other than the dialog box, the dialog box must first be responded to.
#include "CXXXDlg.h "
void CXXXDlg::OnBnClickedOk()
{
CMyNewDlg Dlg;
Dlg.DoModal();
}
When the user opens a non-modal dialog box, other windows can still be operated.
#include "CXXXDlg.h"
void CXXXDlg::OnBnClickedOk()
{
CMyNewDlg *pDlg=new CMyNewDlg ;
pDlg->Create(IDD_DIALOG2,this);
pDlg->ShowWindow(SW_SHOW);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.