I am using two classes in one project. First class is CClientSocketDlg and other one is CUserSpecificationDlg.
Here is the some part of the code.
class CClientSocketDlg : public CDialog
{
public:
bool StartClient();
CClientSocketDlg(CWnd* pParent = NULL);
void AppendMessage(LPCTSTR strText );
enum { IDD = IDD_CLIENTSOCKET_DIALOG };
CEdit m_ctlMsgList;
CIPAddressCtrl m_ctlIPAddress;
};
class CuserspecificationDlg : public CDialogEx
{
public:
CuserspecificationDlg(CWnd* pParent = NULL);
// Dialog Data
enum { IDD = IDD_USERSPECIFICATION_DIALOG };
CListCtrl m_List;
CString out; // For Edit control in list box having IDC_EDIT2
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
};
Can I declare them in one project ? Is it allowed? Can I use m_ctlIPAddress
in CUserspecification class ?
Related
If I try in Visual Studio 2019 run class wizard for some dialog, the error "Failed retrieve controls of dialog 'IDD.....' is shown. But after it is class wizard opened and work properly.
But I am interesting, why it throw this error.
Dialog is inherited from my own class, not from CDialogEx. But My own class is inherited from CDialogEx.
My dialog class code:
#pragma once
#include "resource.h"
#include "CMjAcDialog.h"
// CMjcDlgVyberTrasu dialog
class CMjcDlgVyberTrasu : public CMjAcDialog
{
DECLARE_DYNAMIC(CMjcDlgVyberTrasu)
public:
CMjcDlgVyberTrasu(CWnd* pParent = nullptr); // standard constructor
virtual ~CMjcDlgVyberTrasu();
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_VYBER_TRASU };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
vector<CMajTrasa *> *m_pVecOfTrasy;
CListCtrl m_cList;
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedBtUkaz();
virtual BOOL OnInitDialog();
};
And part of my my Dialog Class CMjAcDialog code:
class CMjAcDialog : public CDialogEx
{
DECLARE_DYNAMIC(CMjAcDialog)
protected:
UINT m_IDD;
CMajCtrlMap m_majCtrlMap;
CFont m_Font_mid, m_Font_big;
CString m_csDlgTitle;
HICON m_hIcon;
private:
int m_idMainIcon;
float m_dCurDpiScale;
// konstruktor
public:
CMjAcDialog(UINT idd, CWnd* pParent = NULL, CString acsWinCaption = L"", int idMainIcon = 0);
~CMjAcDialog();
protected:
virtual BOOL OnInitDialog();
.....
....
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 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;
I have made a DLL with a function that opens a non-modal window.
class CMainFrame : public CFrameWnd
{
protected:
CMainFrame() {}
DECLARE_DYNCREATE(CMainFrame)
public:
virtual void PostNcDestroy()
{ AfxPostQuitMessage(0); }
public:
virtual ~CMainFrame() {}
};
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
class CDllApp : public CWinApp
{
public:
CDllApp() :
m_pDocTemplate(NULL)
{};
public:
virtual BOOL InitInstance()
{
CWinApp::InitInstance();
m_pDocTemplate = new CMultiDocTemplate(IDR_MAINFRAME, RUNTIME_CLASS(C3DGraphDoc),
RUNTIME_CLASS(CMainFrame), RUNTIME_CLASS(C3DGraphView));
return TRUE;
}
public:
CMultiDocTemplate* m_pDocTemplate;
};
CDllApp theApp;
class C3DThread : public CWinThread
{
DECLARE_DYNCREATE(C3DThread)
public:
virtual BOOL InitInstance()
{
theApp.m_pDocTemplate->OpenDocumentFile(NULL);
}
};
IMPLEMENT_DYNCREATE(C3DThread, CWinThread)
void __stdcall ExportedFunction()
{
AfxBeginThread(RUNTIME_CLASS(C3DThread));
}
If I close all windows before exiting main application, then all looks fine, otherwise my threads hang up. So, I think I need to clean up by closing all open documents and I wonder what would be the simplest way to do it gracefully. m_pDocTemplate->CloseAllDocuments() from theApp doesn't work, since frame windows for documents belong to different threads.
I have an MFC view, and I have another project which implements and MFC dialog.
I want to host the dialog in my view.
My view is of class CFormView.
I did it that way in my view code:
m_myDialog->Create(myDialog::IDD, this);
Now, I see my dialog, but I can't set focus on it and can't use it.
What do I have to change in order to host my dialog in my view, and be able to use it and set focus to it, just as part of the view?
Thanks
I know this is a few weeks old, but you need to provide more code or a better context of what is taking place.
I had a similar problem to yourself & found this hard to find info on when I first tried this. Following is an abstract of something I've used. I'm sure there's probably a better way to do this, but I find it works the way I want;
//MyApp.h
class MyDialogClass;
class MyApp : public CWinAppEx
{
public:
MyApp();
virtual BOOL InitInstance();
//code etc
MyDialogClass *p_myDlg;
};
//MyApp.cpp
#include "MyApp.h"
#include "CMyView.h"
#include "mydialogclass.h"
BOOL MyApp::InitInstance()
{
//code etc
p_myDlg = CMyView::GetView()->p_myDlg;
//can be used here or elsewhere. I have mine linked with a button
p_myDlg->ShowWindow(true);
};
//CMyView.h
class MyDialogClass;
class CMyView : public CFormView
{
protected: // create from serialization only
CMyView();
DECLARE_DYNCREATE(CMyView)
public:
enum{ IDD = IDD_CMyView_VIEW };
static CMyView* GetView();
MyDialogClass *p_myDlg;
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
};
//CMyView.cpp
#include "MyApp.h"
#include "CMyView.h"
#include "mydialogclass.h"
int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFormView::OnCreate(lpCreateStruct) == -1)
return -1;
p_myDlg = new MyDialogClass(this);
return 0;
}
void CMyView::DisplayDialogFoo()
{
//can be used here or elsewhere. I have mine linked with a button
p_myDlg->ShowWindow(true);
}
//mydialogclass.h
class MyDialogClass : public CDialog
{
DECLARE_DYNAMIC(MyDialogClass)
public:
MyDialogClass(CWnd* pParent /*= NULL*/);
enum { IDD = IDD_MyDialog_DLG };
};
//mydialogclass.cpp
#include "mydialogclass.h"
MyDialogClass::MyDialogClass(CWnd* pParent /*=NULL*/)
: CDialog(MyDialogClass::IDD, pParent)
{
Create(IDD, pParent);
}
There's also an article here I just found:
http://www.codeproject.com/Articles/1651/Tutorial-Modeless-Dialogs-with-MFC