Page 1 of 1

MFC Programming...

Posted: Wed Jul 02, 2003 4:17 pm
by anubis
In MFC is there a way to use CView & its derivatives in standard controls like a controlbar?

Re:MFC Programming...

Posted: Wed Jul 02, 2003 5:08 pm
by Tim
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...

Posted: Thu Jul 03, 2003 11:32 am
by anubis
hi,
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...

Posted: Thu Jul 03, 2003 11:55 am
by anubis
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.

Re:MFC Programming...

Posted: Thu Jul 03, 2003 12:55 pm
by Tim
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.

Re:MFC Programming...

Posted: Sat Jul 05, 2003 5:44 am
by anubis
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.

Re:MFC Programming...

Posted: Sat Jul 05, 2003 6:22 am
by Tim
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...

Posted: Sat Jul 05, 2003 6:30 am
by anubis
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 :D ;)

Re:MFC Programming...

Posted: Sun Jul 06, 2003 4:33 pm
by Tim
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.

Re:MFC Programming...

Posted: Mon Jul 07, 2003 4:24 am
by anubis
Hi everybody,
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;
}
Now in the CMyView class i access the CDocument as

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 
}
Where GetDocument() is written as

Code: Select all

CWorkspcDoc* CMyView::GetDocument()
{
   ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CWorkspcDoc)));
   return (CWorkspcDoc*)m_pDocument;
}
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.

Re:MFC Programming...

Posted: Fri Jul 11, 2003 1:14 am
by anubis
Got the document and the view to work together at last..... ;D

Thanx to Tim & all watching the thread.

-anubis