我有关于将“编辑文本”保存到注册的问题。
我在主类中做了保存到注册文本数据的操作,并且在另一个注册表类中进行了调用
CRegSettings,所以每次我单击按钮时此类都进行枚举
MyItem \ 0,MyItem \ 1。

我的问题是我无法将CString文本输入此CRegSettings类,此类没有DoDataExchange,所以当我以某种方式将其转到错误“ CMySettingsItem :: DoDataExchange':在所有控制路径上递归”时,它就无法工作,我无法得到从主类到此类的CString,有人知道怎么做吗?谢谢。

这是我的代码。

Main.cpp

// CNewConnectionDlg dialog

IMPLEMENT_DYNAMIC(CNewConnectionDlg, CDialog)



CNewConnectionDlg::CNewConnectionDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNewConnectionDlg::IDD, pParent)
    , m_csIp(_T("localhost"))
    , m_csPort(_T("22"))
    , m_csUsername(_T("Username"))
    , m_csPassword(_T("Password"))

{

}

class CMySettingsItem : public CRegSettings
{
public:
    DWORD Id;

    CString m_Password;
    CString Password;


    BEGIN_REG_MAP(CMySettingsItem)
        REG_ITEM(Id, 1)
        REG_ITEM(Password, m_Password)
    END_REG_MAP()
protected:
    virtual void DoDataExchange(CDataExchange* pDX);

};

// Sample application configuration
class CMySettings : public CRegSettings
{
public:

    DWORD RootId; // DWORD option
    CString RootName; // String option
    std::string FullName;

    // list of options (CMySettingsItem)
    CSimpleArray<CMySettingsItem> Profile;  // ATL CSimpleArray
    //std::list<CMySettingsItem> ProfileList;   // STL list

    CMySettingsItem SubItem; // Subitem test

    BEGIN_REG_MAP(CMySettings)
        REG_ITEM(RootId, 1)
        REG_ITEM(RootName, "Profile")
        REG_ITEM_STL(FullName, "Profile")
        REG_ITEM_SIMPLE_ARRAY(Profile)
        //REG_ITEM_LIST(ProfileList)
    END_REG_MAP()
};

void CMySettingsItem::DoDataExchange(CDataExchange* pDX)
{
    CMySettingsItem::DoDataExchange(pDX);

    DDX_Text(pDX, IDC_PASSWORD, m_Password);


}

CNewConnectionDlg::~CNewConnectionDlg()
{
    AfxGetApp()->WriteProfileString("Settings", "UserName", m_csUsername);
    AfxGetApp()->WriteProfileString("Settings", "IP",       m_csIp);
    AfxGetApp()->WriteProfileString("Settings", "Port",     m_csPort);
    AfxGetApp()->WriteProfileString("Settings", "Password", m_csPassword);




    CMySettings configuration(HKEY_CURRENT_USER, "Software\\MyItem\\Item");

    // Load configuration
    if(configuration.Load() != ERROR_SUCCESS)
        printf("failed to load\n");

    // Use loaded configuration
    configuration.RootId++;

    CMySettingsItem item;
    item.Id = configuration.RootId;

    configuration.Profile.Add(item); // CSimpleArray
    //configuration.ProfileList.push_back(item); // list

    // Save new configuration
    if(configuration.Save() != ERROR_SUCCESS)
        printf("failed to save\n");



}


void CNewConnectionDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);

    DDX_Text(pDX, IDC_IP, m_csIp);
    DDV_MaxChars(pDX, m_csIp, 255);

    DDX_Text(pDX, IDC_PORT, m_csPort);
    DDV_MaxChars(pDX, m_csPort, 6);

    DDX_Text(pDX, IDC_USERNAME, m_csUsername);
    DDV_MaxChars(pDX, m_csUsername, 20);

    DDX_Text(pDX, IDC_PASSWORD, m_csPassword);
    DDV_MaxChars(pDX, m_csPassword, 255);

}


BEGIN_MESSAGE_MAP(CNewConnectionDlg, CDialog)
    ON_EN_CHANGE(IDC_PASSWORD, &CNewConnectionDlg::OnEnChangePassword)
    ON_EN_CHANGE(IDC_USERNAME, &CNewConnectionDlg::OnEnChangeUsername)
END_MESSAGE_MAP()

void CNewConnectionDlg::OnEnChangePassword()
{

}


void CNewConnectionDlg::OnEnChangeUsername()
{

}


和标题

主站

#pragma once
#include "InnerDlg.h"
#include <afxtempl.h>

class CNewConnectionDlg : public CDialog
{
    DECLARE_DYNAMIC(CNewConnectionDlg)

public:
    CNewConnectionDlg(CWnd* pParent = NULL);   // standard constructor
    virtual ~CNewConnectionDlg();

     CNewConnectionDlg(const CString sSection, const CString sArrayName);

    // Dialog Data
    enum { IDD = IDD_NEWCONNECTION };


public:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

protected:
    DECLARE_MESSAGE_MAP()
public:

    // The server ip or name
    CString m_csIp;

    // The negotiating port on the server
    CString m_csPort;

    // Username for the server
    CString m_csUsername;

    // Password for the server
    CString m_csPassword;


    afx_msg void OnEnChangePassword();
    afx_msg void OnEnChangeUsername();

};

最佳答案

DoDataExchange仅适用于用户界面类,例如基于CDialog的类。它用于将屏幕上的用户界面元素与该对象的成员变量连接。因此,您的数据位于m_csIp,m_csPort,m_csUsername和m_csPassword成员中。您只需要将其分配给您的CMySettingsItem实例的相关成员。

例如:

item.m_Password = m_csPassword;

09-07 08:08