I have a lot of CDialogEx derived classes that do something like this in OnInitDialog:
CMeetingScheduleAssistantApp::InitialiseResizeIcon(m_bmpResize, m_lblResize, this);
CMeetingScheduleAssistantApp::RestoreWindowPosition(_T("PublisherDB"), this, true);
Then, I have the following added to each derived dialog class:
int CPublishersDatabaseDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialogEx::OnCreate(lpCreateStruct) == -1)
return -1;
// Save Initial window size to m_rcInit
GetWindowRect(&m_rcInit);
return 0;
}
void CPublishersDatabaseDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
// Set the minimum window size to initial size.
lpMMI->ptMinTrackSize.x = m_rcInit.Width();
lpMMI->ptMinTrackSize.y = m_rcInit.Height();
CDialogEx::OnGetMinMaxInfo(lpMMI);
}
void CPublishersDatabaseDlg::OnClose()
{
CMeetingScheduleAssistantApp::SaveWindowPosition(_T("PublisherDB"), this);
CDialogEx::OnClose();
}
The only thing that is different for each dialog is the phrase that is used for saving the window position.
I want to have a based CDialogEx class that I can inherit from that will perform the above actions. I have looked on SO and seem some questions and creating a CDialog class and inheriting from another CDialog class. But this class I want to create is more generic. Effectively to be used as a base instead of CDialogEx.
Can this be done? Am I over-complicating this?
Problems
Why I try to create a new class, derived from CDialogEx:
I don't know if it is because it requires a dialog ID as stated here.
Classes such as CDialog, CFormView, or CPropertyPage, which require a dialog ID.
So I can't work out the correct way to create a base CDialogEx class for use in all my other dialog classes.
Update
I created this code and it tells me that CResizingDialog is not a class or a namespace:
#include "ResizingDialog.h"
#include "resource.h"
#include "stdafx.h"
IMPLEMENT_DYNAMIC(CResizingDialog, CDialogEx)
CResizingDialog::CResizingDialog(const CString& strWindowID, UINT nIDTemplate, CWnd* pParent = nullptr)
: m_strWindowID(strWindowID), CDialogEx(nIDTemplate, pParent)
{
}
CResizingDialog::~CResizingDialog()
{
}
void CResizingDialog::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CResizingDialog, CDialogEx)
ON_WM_CREATE()
ON_WM_GETMINMAXINFO()
ON_WM_CLOSE()
END_MESSAGE_MAP()
int CResizingDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialogEx::OnCreate(lpCreateStruct) == -1)
return -1;
// Save Initial window size to m_rcInit
GetWindowRect(&m_rcInit);
return 0;
}
void CResizingDialog::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
// Set the minimum window size to initial size.
lpMMI->ptMinTrackSize.x = m_rcInit.Width();
lpMMI->ptMinTrackSize.y = m_rcInit.Height();
CDialogEx::OnGetMinMaxInfo(lpMMI);
}
void CResizingDialog::OnClose()
{
SaveWindowPosition(m_strWindowID, this);
CDialogEx::OnClose();
}
Based on the comments encouraging me to try to create the class manually, I have it working:
#include "stdafx.h"
#include "resource.h"
#include "ResizingDialog.h"
IMPLEMENT_DYNAMIC(CResizingDialog, CDialogEx)
CResizingDialog::CResizingDialog(const CString& strWindowID, UINT nIDTemplate, CWnd* pParent /* nullptr */, bool bOnlyStorePosition /* false */)
: m_strWindowID(strWindowID),
m_bOnlyStorePosition(bOnlyStorePosition), CDialogEx(nIDTemplate, pParent)
{
}
CResizingDialog::~CResizingDialog()
{
}
void CResizingDialog::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CResizingDialog, CDialogEx)
ON_WM_CREATE()
ON_WM_GETMINMAXINFO()
ON_WM_CLOSE()
END_MESSAGE_MAP()
int CResizingDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialogEx::OnCreate(lpCreateStruct) == -1)
return -1;
// Save Initial window size to m_rcInit
GetWindowRect(&m_rcInit);
return 0;
}
void CResizingDialog::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
// Set the minimum window size to initial size.
lpMMI->ptMinTrackSize.x = m_rcInit.Width();
lpMMI->ptMinTrackSize.y = m_rcInit.Height();
CDialogEx::OnGetMinMaxInfo(lpMMI);
}
void CResizingDialog::OnClose()
{
SaveWindowPosition(m_strWindowID, this);
CDialogEx::OnClose();
}
void CResizingDialog::OnOK()
{
SaveWindowPosition();
CDialogEx::OnOK();
}
BOOL CResizingDialog::OnInitDialog()
{
CDialogEx::OnInitDialog();
if(!m_bOnlyStorePosition)
InitialiseResizeIcon(m_bmpResize, m_lblResize, this);
RestoreWindowPosition(m_strWindowID, this, true);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
I decided to duplicate the methods that were in the app class into this new dialog class instead. Eventually they can be removed from the app class. The only thing I also had to do was #include my resource file because the image needs to know the value of the resource ID.
This is the ResizingDialog.h header:
#pragma once
#include <afxwin.h>
class CResizingDialog : public CDialogEx
{
DECLARE_DYNAMIC(CResizingDialog)
public:
CResizingDialog(const CString& phrase, UINT nIDTemplate, CWnd* pParent = nullptr, bool bOnlyStorePosition = false); // Constructor
virtual ~CResizingDialog(); // Destructor
protected:
void OnOK() override;
virtual void DoDataExchange(CDataExchange* pDX) override; // DDX/DDV support
void SaveWindowPosition(void) { SaveWindowPosition(m_strWindowID, this); }
public:
BOOL OnInitDialog() override;
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnGetMinMaxInfo(MINMAXINFO* lpMMI);
afx_msg void OnClose();
DECLARE_MESSAGE_MAP()
private:
CBitmap m_bmpResize;
CStatic m_lblResize;
CRect m_rcInit;
CString m_strWindowID;
bool m_bOnlyStorePosition;
void RestoreWindowPosition(CString strWindow, CWnd* pWindow, bool bOverrideState = false);
void SaveWindowPosition(CString strWindow, CWnd* pWindow);
void InitialiseResizeIcon(CBitmap& rBmpResize, CStatic& rLblResize, CWnd* pDialog);
};
The actual functions SaveWindowPosition, RestoreWindowPosition and InitialiseResizeIcon are not shown here as they don't directly relate to the issue.
Related
I need to override event wm_lbuttonup of CMFCRibbonSlider class
class CMyRibbonSlider : public CMFCRibbonSlider
{
public:
virtual void OnLButtonUp(CPoint point); // need this event handler!
};
void CMyRibbonSlider::OnLButtonUp(CPoint point)
{
AfxMessageBox(_T("Works!"))
return;
}
Use CMyRibbonSlider object in MainFrame class
class CMainFrame : public CMDIFrameWndEx
{
....
CMyRibbonSlider* SliderLine;
}
When CMFCRibbonSlider control apears and I click left mousebutton, nothing happens. What do I do wrong?
EDIT:
initialization
CArray<CMFCRibbonBaseElement*, CMFCRibbonBaseElement*> ar;
m_wndRibbonBar.GetElementsByID(ID_SLIDER2, RibbonElementsArray);
m_wndRibbonBar.GetElementsByID(ID_START_BTN, ar);
RibbonElementsArray.Append(ar);
m_wndRibbonBar.GetElementsByID(ID_STOP_BTN, ar);
RibbonElementsArray.Append(ar);
m_wndRibbonBar.GetElementsByID(ID_SLIDER_LINE, ar); //HERE!
RibbonElementsArray.Append(ar);
m_wndRibbonBar.GetElementsByID(IDC_STATIC_TT, ar);
RibbonElementsArray.Append(ar);
Slider = DYNAMIC_DOWNCAST(CMFCRibbonSlider, RibbonElementsArray[0]);
btnStart = DYNAMIC_DOWNCAST(CMFCRibbonButton, RibbonElementsArray[1]);
btnStop = DYNAMIC_DOWNCAST(CMFCRibbonButton, RibbonElementsArray[2]);
SliderLine = (CMyRibbonSlider*)DYNAMIC_DOWNCAST(CMFCRibbonSlider, RibbonElementsArray[3]); //and HERE!
TmpLable = DYNAMIC_DOWNCAST(CMFCRibbonLabel, RibbonElementsArray[4]);
also I have these event handlers in Mainframe class and they work:
ON_UPDATE_COMMAND_UI(ID_SLIDER_LINE, &CMainFrame::OnUpdateSliderLine)
ON_COMMAND(ID_SLIDER_LINE, &CMainFrame::OnSliderLine)
SliderLine->GetPos() also returns right slider position, so I think initialization is right...
It is seem like the CMFCRibbonSlider control isn't added correctly to CMFCRibbonPanel and therefore CMainFrame class does not expose slider’s messages through message map.
Try to use a method described in the following article:
Walkthrough: Creating a New Ribbon Application By Using MFC
The code will be look like below.
MyRibbonSlider.h
#include "afxribbonslider.h"
#pragma once
class CMyRibbonSlider : public CMFCRibbonSlider
{
DECLARE_DYNCREATE(CMyRibbonSlider)
public:
CMyRibbonSlider();
CMyRibbonSlider(UINT nID, int nWidth = 100);
// Implementation
public:
virtual ~CMyRibbonSlider();
virtual void OnLButtonUp(CPoint point);
};
MyRibbonSlider.cpp
#include "stdafx.h"
#include "MyRibbonSlider.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
IMPLEMENT_DYNCREATE(CMyRibbonSlider, CMFCRibbonSlider)
CMyRibbonSlider::CMyRibbonSlider()
{
}
CMyRibbonSlider::CMyRibbonSlider(UINT nID, int nWidth)
: CMFCRibbonSlider(nID, nWidth)
{
}
CMyRibbonSlider::~CMyRibbonSlider()
{
}
void CMyRibbonSlider::OnLButtonUp(CPoint point)
{
TRACE("\nCMyRibbonSlider::OnLButtonUp()");
return;
}
Related declarations in the CMainFrame.h
afx_msg void OnSliderLine();
afx_msg void OnUpdateSliderLine(CCmdUI* pCmdUI);
CMainFrame.cpp
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWndEx)
...
ON_COMMAND(ID_SLIDER, &CMainFrame::OnSliderLine)
ON_UPDATE_COMMAND_UI(ID_SLIDER, &CMainFrame::OnUpdateSliderLine)
END_MESSAGE_MAP()
void CMainFrame::InitializeRibbon()
{
...
bNameValid = strTemp.LoadString(*your title*);
ASSERT(bNameValid);
CMFCRibbonPanel* pPanelAdvanced = pCategoryHome->AddPanel(strTemp, m_PanelImages.ExtractIcon (*your icon*));
strTemp = _T("Slider");
CMyRibbonSlider* pRibbonSlider = new CMyRibbonSlider(ID_SLIDER);
pPanelAdvanced->Add(pRibbonSlider);
...
}
void CMainFrame::OnSliderLine()
{
// TODO
}
void CMainFrame::OnUpdateSliderLine(CCmdUI* pCmdUI)
{
// TODO
}
I am updating some old software that does not work on Win7 or later. so, I am rebuilding some MFC libraries that are using latest win32 updates.
Now I have two issues:
MessageBox appears behind the CFrameWnd so it can't be accessed sending the application to halt.
Open dialog box (whether is based on CFileDialog or IFileDilog) does not get refreshed when changing the file type.
However, both problems are solved if the CFrameWnd is hidden. or, in case of MessageBox, you will not need to hide the window if you write: PostMessage(0x118); which in fact I don't know why.
There must be something I am missing Here.
I also Have another problem when using the OpenFileDialog class that inherits from the IFileDialog. is when closing this dialog without picking up a file, the application Crashes.
//--targetver.h
#pragma once
#include <sdkddkver.h>
//--stdafx.h:
#ifndef CS_EXTRALEAN
#define CS_EXTRALEAN
#endif
#pragma once
#include "targetver.h"
#include<afxwin.h>
#include<afxext.h>
#include<afxcmn.h>
//--stdafx.cpp
#include "stdafx.h"
//--CMainWnd.h
#pragma once
class CMainWnd : public CFrameWnd
{
public:
CMainWnd();
~CMainWnd();
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT, CPoint);
DECLARE_MESSAGE_MAP()
};
//--CMainWnd.cpp
#include "stdafx.h"
#include"CMainWnd.h"
BEGIN_MESSAGE_MAP(CMainWnd, CFrameWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
CMainWnd::CMainWnd()
: CFrameWnd()
{
CString class_name = AfxRegisterWndClass(
CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS,
AfxGetApp()->LoadStandardCursor(IDC_ARROW),
(HBRUSH)::GetStockObject(BLACK_BRUSH),
AfxGetApp()->LoadStandardIcon(IDI_ASTERISK));
HRESULT hResult = this->Create(
class_name,
L"This is CMainWnd",
WS_OVERLAPPEDWINDOW,
this->rectDefault,
NULL,
NULL,
0,
NULL);
}
CMainWnd::~CMainWnd() { }
void CMainWnd::OnPaint()
{ }
void CMainWnd::OnLButtonDown(UINT, CPoint)
{
MessageBox(L"HELLO MFC", L"MFC", MB_OK);
}
//--CAppWnd.h
#pragma once
class CAppWnd : public CWinApp
{
public:
CAppWnd();
~CAppWnd();
BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
//--CAppWnd.cpp
#include "stdafx.h"
#include "CAppWnd.h"
#include "CMainWnd.h"
BEGIN_MESSAGE_MAP(CAppWnd, CWinApp)
END_MESSAGE_MAP()
CAppWnd::CAppWnd()
:CWinApp()
{ }
CAppWnd::~CAppWnd()
{ }
BOOL CAppWnd::InitInstance()
{
this->m_pMainWnd = new CMainWnd;
this->m_pMainWnd->ShowWindow(m_nCmdShow);
return CWinApp::InitInstance();
}
CAppWnd The_App;
There was a simple problem. You override OnPaint but didn't call the default procedure. OnPaint handles to WM_PAINT message, it doesn't forgive this error.
void CMainWnd::OnPaint()
{
CFrameWnd::OnPaint(); //<= this was missing
//custom paint...
//CClientDC dc(this);
//dc.TextOut(0, 0, L"test");
//dc is automatically released...
}
Or you can use CPaintDC which is a wrapper for BeginPaint/EndPaint API
void CMainWnd::OnPaint()
{
CPaintDC dc(this);
//custom paint...
//dc.TextOut(0, 0, L"test");
//dc is automatically released...
}
If you don't do any painting in this frame window then remove the whole CMainWnd::OnPaint() function and the correspoonding ON_WM_PAINT message.
Above change should fix your error. I would rewrite the rest of the code so it calls the default override first. Example:
#include "stdafx.h"
#include "resource.h"
class CMainWnd : public CFrameWnd
{
public:
CMainWnd();
~CMainWnd();
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT, CPoint);
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CMainWnd, CFrameWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
CMainWnd::CMainWnd() : CFrameWnd() {}
CMainWnd::~CMainWnd() {}
void CMainWnd::OnPaint()
{
CFrameWnd::OnPaint();
}
void CMainWnd::OnLButtonDown(UINT f, CPoint pt)
{
CFrameWnd::OnLButtonDown(f, pt);
CFileDialog dlg(TRUE, 0, 0, 0,
L"All files|*.*|"
L"Text files|*.txt;*.txt||" , this);
if (dlg.DoModal() == IDOK)
MessageBox(dlg.GetPathName(), L"MFC", MB_OK);
}
class CAppWnd : public CWinApp
{
public:
BOOL InitInstance();
};
BOOL CAppWnd::InitInstance()
{
CWinApp::InitInstance();
CMainWnd *frame = new CMainWnd;
CString class_name = AfxRegisterWndClass(
CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS,
AfxGetApp()->LoadStandardCursor(IDC_ARROW),
(HBRUSH)::GetStockObject(BLACK_BRUSH),
AfxGetApp()->LoadStandardIcon(IDI_ASTERISK));
frame->Create(class_name, L"This is CMainWnd",
WS_OVERLAPPEDWINDOW, CFrameWnd::rectDefault, NULL, NULL, 0, NULL);
frame->ShowWindow(m_nCmdShow);
m_pMainWnd = frame;
return TRUE;
}
CAppWnd The_App;
Note that you can call up static members directly, for example CFrameWnd::rectDefault, it doesn't cause an error either way but it makes the code more clear.
I am trying to create CWnd derived class at runtime but CWnd::Create fails. I have no idea why. Here is minimal code that show the problem:
MFCTestApplicationDlg.h
#pragma once
class c_CustomButton : public CButton
{
protected:
DECLARE_MESSAGE_MAP()
virtual void PreSubclassWindow();
public:
c_CustomButton();
virtual ~c_CustomButton();
};
class TestWindow : public CWnd
{
public:
TestWindow();
virtual ~TestWindow();
protected:
DECLARE_MESSAGE_MAP()
DECLARE_DYNCREATE(TestWindow)
};
// CMFCTestApplicationDlg dialog
class CMFCTestApplicationDlg : public CDialogEx
{
...
}
MFCTestApplicationDlg.cpp
//
#include "stdafx.h"
#include "MFCTestApplication.h"
#include "MFCTestApplicationDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/*==========================================================================*/
c_CustomButton::c_CustomButton()
{
}
/*==========================================================================*/
c_CustomButton::~c_CustomButton()
{
}
BEGIN_MESSAGE_MAP(c_CustomButton, CButton)
END_MESSAGE_MAP()
/*==========================================================================*/
void c_CustomButton::PreSubclassWindow()
{
CButton::PreSubclassWindow();
ModifyStyle(0, BS_OWNERDRAW);
}
IMPLEMENT_DYNAMIC(TestWindow, CWnd)
TestWindow::TestWindow()
{
}
TestWindow::~TestWindow()
{
}
BEGIN_MESSAGE_MAP(TestWindow, CWnd)
END_MESSAGE_MAP()
void CMFCTestApplicationDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
c_CustomButton* pColoredButton = new c_CustomButton;
pColoredButton->Create((LPCTSTR)"", 0, CRect(), this, 0);// successeded
pColoredButton->SetWindowTextW((LPCTSTR)"test");
TestWindow* pTestWindow = new TestWindow;
pTestWindow->Create((LPCTSTR)"TestWindow", (LPCTSTR)"TestWindowName", 0, CRect(), this, 0);// failed
pTestWindow->SetWindowText((LPCTSTR)"test");
}
In void CMFCTestApplicationDlg::DoDataExchange(CDataExchange* pDX) I tried to create a CButton derived class object and CWnd derived class object. The first one created successfully but CWnd derived class object fails to create. Whats wrong with this code?
I need to place a CView derived class into a CDockablePane. Is there any code example somewhere, or can someone provide such code?
What I tried:
Apparently should be simple, online I found advice like "just create the view and set its parent to be the dialog or the dockable pane or what kind of window you want". But for some reason it doesn't work, maybe is because it needs a CFrameWnd, I don't know.
Anyway, I need to be able to do this without creating another document template class. Just to work with preexisting document and view classes.
Here's an example:
a class derived from CDockablePane:
//CRichEditPane .h
class CRichEditPane : public CDockablePane
{
DECLARE_DYNAMIC(CRichEditPane)
public:
CRichEditPane();
virtual ~CRichEditPane();
protected:
void AdjustLayout();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnSize(UINT nType, int cx, int cy);
};
//CRichEditPane .cpp
IMPLEMENT_DYNAMIC(CRichEditPane, CDockablePane)
CRichEditPane::CRichEditPane()
{
}
CRichEditPane::~CRichEditPane()
{
}
BEGIN_MESSAGE_MAP(CRichEditPane, CDockablePane)
ON_WM_CREATE()
ON_WM_SIZE()
END_MESSAGE_MAP()
// CRichEditPane message handlers
int CRichEditPane::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDockablePane::OnCreate(lpCreateStruct) == -1)
return -1;
CRuntimeClass *pClass = RUNTIME_CLASS(CRichEditViewInPane);
// calling constructor using IMPLEMENT_DYNCREATE macro
CRichEditViewInPane *pView = (CRichEditViewInPane*)pClass->CreateObject();
if (!pView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0,0,0,0), this, AFX_IDW_PANE_FIRST, NULL))
{
return -1;
}
CRichEditCtrl ctrl;
ctrl.Create(WS_CHILD, CRect(0, 0, 0, 0), this, 10991);
return 0;
}
void CRichEditPane::OnSize(UINT nType, int cx, int cy)
{
CDockablePane::OnSize(nType, cx, cy);
AdjustLayout();
}
a view class derived from CView:
//CRichEditViewInPane .h
class CRichEditViewInPane : public CRichEditView
{
DECLARE_DYNCREATE(CRichEditViewInPane)
protected:
CRichEditViewInPane(); // protected constructor used by dynamic creation
virtual ~CRichEditViewInPane();
public:
#ifdef _DEBUG
virtual void AssertValid() const;
#ifndef _WIN32_WCE
virtual void Dump(CDumpContext& dc) const;
#endif
#endif
protected:
DECLARE_MESSAGE_MAP()
};
//CRichEditViewInPane. cpp
IMPLEMENT_DYNCREATE(CRichEditViewInPane, CRichEditView)
CRichEditViewInPane::CRichEditViewInPane()
{
}
CRichEditViewInPane::~CRichEditViewInPane()
{
}
BEGIN_MESSAGE_MAP(CRichEditViewInPane, CRichEditView)
END_MESSAGE_MAP()
I have a problem about save Edit text to registy.
I did in main class save to registy text data and i did another registry class that call
CRegSettings so this class doing enum each time when i clicked button like
MyItem\0, MyItem\1.
my problem is i cant get that CString text into this CRegSettings class, this class doesnt have DoDataExchange so when i did it somehow its turn to error "CMySettingsItem::DoDataExchange' : recursive on all control paths" so it's doesnt work and i cant get CString from main class to this class, does anyone know how to do it ? Thanks.
Here is my codes.
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()
{
}
And Header
Main.h
#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 is only intended for user interface classes, such as your CDialog-based class. It is used to connect the user interface elements on screen with the member variables of that object. So your data is sitting in the m_csIp, m_csPort, m_csUsername, and m_csPassword members. You just need to assign it to the relevant member of your CMySettingsItem instance.
For example:
item.m_Password = m_csPassword;