【问题标题】:Visual Studio 2019 MFC C++ Make button open another dialog box?Visual Studio 2019 MFC C++ Make 按钮打开另一个对话框?
【发布时间】:2021-01-24 03:19:05
【问题描述】:

我有一个基于对话框的 MFC 应用程序,它有两个对话框:

  1. IDD_FITNESSAPP_LOGIN_DLG
  2. IDD_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


【解决方案1】:

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    相关资源
    最近更新 更多