I have CPropertySheet which contain multiple CPropertyPage as tabs. In one of the CPropertyPage I have a button and clicking on the button launches a CDialog. I am trying to get the control to CPropertyPage calling GetParent() method inside the CDialog class methods but somehow I don't get the right parent window and when I try to access the members of CPropertypage it throws an Access Violation Exception.
Following is the code I have used:
CDialog *parentDialog = (CDialog *)GetParent();
CPropertyPage *parentPage = (CPropertyPage *)parentDialog->GetParent();
DResourceStateMgr dSrcStateMgr(parentPage->m_psp.hInstance);
// throws Access Violation exception.
Same thing used to work in the Visual studio 2008 but when I moved to visual studio 2013 it throws the exception.
Related
I am using Visual Studio 2019 and building a C++ desktop application. I have a dialog class inherited from CDialogEx. In this class I have a few buttons, i.e. CButton as members.
When I use the EnableWindow method in the class constructor, it reports:
Debug Assertion Failed! Program C:\WINDOWS\SYSTEM32\mfc140ud.dll File:
d....\winocc.cpp line 345
If I commented it out the application worked fine. Any idea why I have this error? All I wanted to do is to disable the buttons as soon as the dialog shows up - that is why I use this method in the constructor.
There is a difference between a C++ class and the actual creation of the dialog window and its controls.
The constructor is for class related items, not window related items. For example, if you want to initialize member variables, then the constructor would be the place for it. But at construction, no dialog window has been created, thus there are no child controls that exist, thus the error you're seeing.
The place where you can assume that the window is created is in the dialog's OnInitDialog member function. This is where you should be able to call EnableWindow on the controls.
If you do not have an OnInitDialog, you can add that function using the class wizard.
I have a CView and I've been painting stuff on it just fine. Then I realized I needed to add some controls like text boxes and combo boxes to my CView. So I am trying to convert my CView into a CFormView which does not have a default constructor. But I need a default constructor for this line IMPLEMENT_DYNCREATE(CMyView, CFormView) so I have created a default constructor like this CMyView::CMyView():CFormView( ( UINT )666 ) { ... }. That 666 is because I don't know which argument I am supposed to pass there. I am guessing that I need to pass the ID of my CMyView class. I can't find the id of my CMyView class which was originally created by the Visual Studio project wizard automatically. Where should I look for it? When I run the program I get this error: First-chance exception at 0x75AEC41F in myapp.exe: Microsoft C++ exception: CInvalidArgException at memory location 0x003CF134.
Critical error detected c0000374
myapp.exe has triggered a breakpoint.
And it stops at line 51 in free.c
So my question is: how can I fix this? Also I want to keep the stuff that I previously painted in my former CView, now CFormView. Is the CFormView able to paint like the CView? If not, should I use a split pane in my CMainFrame and have a CView and a CFormView? I might have used terms specific to Java swing and I apologize for that. I am new to MFC and C++.
Thank you in advance,
Corneliu
The CFormView constructor needs the ID of the form's dialog template to be passed in. That is the template you create in the visual editor. You can see how this works by creating a little test project with a CFormView to make your declarations look like the MFC declarations in the test project.
The CFormView can be painted like a CView (in OnDraw), but you might have undesired effects on the controls if you do any scaling or scrolling of the view.
Other alternatives for mixing controls with painted output are (1) Using CControlBar to put controls on the edge of the view or (2) Put a CStatic on the CFormView and do your painting in the CStatic.
Check out Resources.rc.
You can try adding something like this:
IDD_DIALOG1 DIALOG 0, 0, 400, 400
STYLE DS_SETFONT | WS_CHILD
FONT 8, "MS Sans Serif"
BEGIN
END
I have created in VS 2012, in a visual resource editor, few components on the main form of the application (one of them is CStatic text).
I want now to access it, so I have wrote somewhere in my MainFrm.cpp (the code executes after clicking one of the buttons, so after everything was constructed):
CStatic * temp = (CStatic *) GetDlgItem(IDC_OPERATION_INFO);
temp->SetWindowText(text);
And while executing second line of the code, I get error:
Debug Assertion Failed!
Program: C:\Windows\system32\mfc110ud.dll
File: f:\\dd\vctools\vc7libs\ship\atlmfc\src\mfc\winocc.cpp
Line: 245
What I'm doing wrong?
The CStatic was created via visual editor, not in the code. Ofc I see it on the application.
The static control is probably on a CDialog or CFormView derived class, not CMainFrame. GetDlgItem only works for controls that are a child of the calling class.
A better way to access the control is to right click on it in the visual editor and select 'Add Variable'. It will let you add a CStatic variable (like m_opinfo) to the correct parent class. Then that class can call m_opinfo.SetWindowText(text).
You should also note that GetDlgItem can be unsafe to use since it returns a temporary pointer. The pointer is only valid for the scope of the code (method) that is executing. You should follow ScottMcP suggestion.
I've got a Dialog-based Application.
By default Visual Studio created an App and a Dlg class.
In the App's InitInstance, to show the dialog box, there is a declaration CMyDlg dlg, then a dlg.DoModal().
My question is, how do I access dlg members from my other classes? For instance, I created an edit box with a control variable associated with it. I want one of my other classes to set the text of the edit box and display it.
Help much appreciated.
I have an MFC application using satellites DLLs in order to support the multilingualism. I am using Visual Studio 2010.
I am able to change the language of the core part of the application without any problems. Things go wrong when I try to load a modeless dialog containing a "special" MFC control (CMFCColorButton, CVSListBox, etc).
The problem occurs at the following statement :
m_dlg->Create(SOME_IID, this); // returns false
How should I proceed to load a "special" MFC control from a satellite DLL?
You must register their classes before you reach OnCreate(). For custom controls, this is typically done in the constructor:
CMyClass::CMyClass()
{
// Pseudo code
m_mfcColorButton.RegisterWindowClass(AfxGetResourceHandle());
}
For MFC controls, I bet there is an initialization function that needs to be called.
I had the same problem: my CDialog - derived class failed in DoModal if I use localized resource dll. It contains CMFCColorButton on resource template.
My solution was to call in a resource dll AfxRegisterMFCCtrlClasses();
class CMyApp: public CWinApp
{
BOOL InitInstance()
{
AfxRegisterMFCCtrlClasses();
return CWinApp::InitInstance();
}
};