MFC Programming...
MFC Programming...
In MFC is there a way to use CView & its derivatives in standard controls like a controlbar?
Re:MFC Programming...
Yes, just use your CView-derived class like a normal window, and call Create with the appropriate parent window. Remember to set the relevant members of CCreateContext to NULL if the view has no document.
Re:MFC Programming...
hi,
I tried to do it but i got many errors ???. Are they because CView need a CFrame derived class to be shown.
I tried to do it but i got many errors ???. Are they because CView need a CFrame derived class to be shown.
Re:MFC Programming...
By the way i am writing a program under VC++ to develop a MDI based application in which instantaneous changes of user chosen variables are reflected in tree views and edit views in many control bars.
To complicate the matter :'( each such view also has an associated doc class also associated.
MFC dictates that all such doc-view pair have to be mentioned in the InitInstance of the CwinApp using the CDoctemplate. CControlBar are not derived from Frame class so CDoctemPlate also donot accept them causing many runtime errors if i try to instantiate a view class without mentioning them.
I cannot find a way round this :-[ and so the problem arises.
To complicate the matter :'( each such view also has an associated doc class also associated.
MFC dictates that all such doc-view pair have to be mentioned in the InitInstance of the CwinApp using the CDoctemplate. CControlBar are not derived from Frame class so CDoctemPlate also donot accept them causing many runtime errors if i try to instantiate a view class without mentioning them.
I cannot find a way round this :-[ and so the problem arises.
Re:MFC Programming...
You can have a view defined outside of InitInstance. This is how the MDI "New Window" command works.
I haven't programmed MFC in a long time, but from what I can remember, CView doesn't necessarily need to have a CFrameWnd as its parent. I've created CViews inside all kinds of things (including control bars).
Maybe it's worth your emailing me the code so I can have a look.
I haven't programmed MFC in a long time, but from what I can remember, CView doesn't necessarily need to have a CFrameWnd as its parent. I've created CViews inside all kinds of things (including control bars).
Maybe it's worth your emailing me the code so I can have a look.
Re:MFC Programming...
hi everybody,
I got over the problem of displaying a CView derived class in a Control Bar. But now the problem that has arised is i am not able to guess how to connect that view class with a Document class.
By the problem was i was illegally passing NULL to the Create Function of CView, which really should have been a registered windows class rather than Null. ;D I did pass on that by passing AfxRegisterWinclass(Null,0,0,0) instead of just NULL.
Another problem is debug configuration gives error of a bad pointer. any guesses for that?? ???Ok By the way the code is Here. A readme.txt file has some information abt the project.
I got over the problem of displaying a CView derived class in a Control Bar. But now the problem that has arised is i am not able to guess how to connect that view class with a Document class.
By the problem was i was illegally passing NULL to the Create Function of CView, which really should have been a registered windows class rather than Null. ;D I did pass on that by passing AfxRegisterWinclass(Null,0,0,0) instead of just NULL.
Another problem is debug configuration gives error of a bad pointer. any guesses for that?? ???Ok By the way the code is Here. A readme.txt file has some information abt the project.
Re:MFC Programming...
I can't look at the code yet, but you should be able to pass a CCreateContext* to CView::Create, through which you can pass a CDocument* (and more).
Re:MFC Programming...
Hello everbody,
I will try the thing that Tim has suggested of passing a CreateContext parameter with CView::Create. Thanks Tim.
More later if i succeed. ;D
I will try the thing that Tim has suggested of passing a CreateContext parameter with CView::Create. Thanks Tim.
More later if i succeed. ;D
Re:MFC Programming...
OK, I had a look at your code.
A couple of things:
-- Allocate your CMyView object using new. Declare m_MyView as CMyView*. The reason for this is that CView::PostNCDestroy calls "delete this", which breaks bady unless you used new to allocate the view.
-- Pass a pointer to a CCreateContext as the last parameter to CView::Create (it comes just after the ID). You'll have to communicate the CDocument* to the dialog bar somehow.
A couple of things:
-- Allocate your CMyView object using new. Declare m_MyView as CMyView*. The reason for this is that CView::PostNCDestroy calls "delete this", which breaks bady unless you used new to allocate the view.
-- Pass a pointer to a CCreateContext as the last parameter to CView::Create (it comes just after the ID). You'll have to communicate the CDocument* to the dialog bar somehow.
Re:MFC Programming...
Hi everybody,
I got over the problem of adding the Document to the View by adding the following code to the Controlbar
Now in the CMyView class i access the CDocument as
Where GetDocument() is written as
Now when the program is run in DEBUG configuration it gives an error in Objcore.cpp(line 43). Looking up the code of Objcore i found that it was in IsKindOf() function mentioned in the above code.
I have inherited CWorkspcDoc from the CDocument.
On running it in RELEASE configuration it gives error if the pDC->DrawText is called. I figure it out that it is a problem in the GetDocument. Is not deriving from CDocument class enough to make it a RUNTIME_CLASS?
MSDN says for IsKindOf
This function works only for classes declared with the DECLARE_DYNAMIC or DECLARE_SERIAL macro.
Though CWorkspcDoc(My document class derived from CDocument) doesnot have either of them but it does have DECLARE_DYNCREATE. Aren't they the same? Any hints??
--ADDENDUM--
By the way, I was trying making a shortcut by declaring CView m_Myview rather than as CView *m_MyView(cos i am always terrified of doing deallocation manually) ;D. So i didnot declare it as a pointer. Thanks Tim for making me notice it.
I got over the problem of adding the Document to the View by adding the following code to the Controlbar
Code: Select all
int CMyDialogBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (MyBaseDialogClass::OnCreate(lpCreateStruct) == -1)
return -1;
CCreateContext m_MyContext;
m_MyContext.m_pCurrentDoc = &m_MyDocument;
m_MyContext.m_pNewViewClass = RUNTIME_CLASS(CMyView);
m_MyView.Create(AfxRegisterWndClass(NULL,0,0,0),"Mywindow",WS_CHILD|WS_VISIBLE,CRect(0,0,100,100),this,123);
return TRUE;
}
Code: Select all
void CMyView::OnDraw(CDC* pDC)
{
CWorkspcDoc* pDoc = GetDocument();
pDC->Rectangle(CRect(0,0,130,130));
// pDC->DrawText(pDoc->m_strValue,CRect(0,0,100,100),DT_LEFT);
//if above line is uncommented it gives error
}
Code: Select all
CWorkspcDoc* CMyView::GetDocument()
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CWorkspcDoc)));
return (CWorkspcDoc*)m_pDocument;
}
I have inherited CWorkspcDoc from the CDocument.
On running it in RELEASE configuration it gives error if the pDC->DrawText is called. I figure it out that it is a problem in the GetDocument. Is not deriving from CDocument class enough to make it a RUNTIME_CLASS?
MSDN says for IsKindOf
This function works only for classes declared with the DECLARE_DYNAMIC or DECLARE_SERIAL macro.
Though CWorkspcDoc(My document class derived from CDocument) doesnot have either of them but it does have DECLARE_DYNCREATE. Aren't they the same? Any hints??
--ADDENDUM--
By the way, I was trying making a shortcut by declaring CView m_Myview rather than as CView *m_MyView(cos i am always terrified of doing deallocation manually) ;D. So i didnot declare it as a pointer. Thanks Tim for making me notice it.
Re:MFC Programming...
Got the document and the view to work together at last..... ;D
Thanx to Tim & all watching the thread.
-anubis
Thanx to Tim & all watching the thread.
-anubis