Skip to main content
gamedev.net
🔒 Locked

MFC issue

Started by CProgrammer 8 replies 250+ views
Original Post
CProgrammer
CProgrammer
Ok, the following code just wont work. OnInitialUpdate is never called, eventhough I have overridden it as a virtual function: header:
#ifndef _CMain_H_
#define _CMain_H_

#include "StdAfx.h"

class CMain : public CView
{
public:
	CMain(CFrameWnd *FrameWnd);

private:

public:
	public:
	virtual void OnInitialUpdate();

};

#endif
 
cpp file
#include "CMain.h"

CMain::CMain(CFrameWnd *FrameWnd)
{
	//Erstellen des Fensters in unserem Rahmenfenster
	//''FrameWnd''.
	Create(0, 0, WS_CHILD | WS_VISIBLE, CRect(), FrameWnd, AFX_IDW_PANE_FIRST);
}

void CMain::OnInitialUpdate()
{
	CView::OnInitialUpdate();
	//Never called
}
 
-CProgrammer
Solo
Solo
How are you attaching the view to the document?
// Ryan
CProgrammer
CProgrammer
I have a class derived from CFrameWnd with the following constructor:

CFrame::CFrame()
{
Create(0, APP_NAME, WS_OVERLAPPEDWINDOW, rectDefault);

main = new CMain(this);
}

Another derived class from CWinApp has the following function to create the CFrameWnd:

BOOL CWinAppd::InitInstance()
{
CFrame *pMainWnd = new CFrame();

m_pMainWnd = pMainWnd;

m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();

return TRUE;
}


[edited by - CProgrammer on December 27, 2003 12:38:08 AM]
Solo
Solo
Typically, documents are created using either CSingleDocTemplate or CMultiDocTemplate. The constructors for these classes take as parameters the object, view, and frame window, link them together, and in the process fires off events like OnInitialUpdate. If you're not doing this or manually calling the AddView() method from your document the View object isn't attached to a document so it's OnInitialUpdate handler never gets called.

[edited by - Solo on December 27, 2003 12:56:28 AM]
// Ryan
CProgrammer
CProgrammer
I see, thanks a bunch.
-CProgrammer
CProgrammer
CProgrammer
So all I need now is a class derived from CDocument. How would I attached that to my sampke?
-CProgrammer
CProgrammer
CProgrammer
No wait I think I got it.

-CProgrammer
CProgrammer
CProgrammer
Hmm i added this to InitInstance but it still dont work:

CSingleDocTemplate* pDoc;
pDoc = new CSingleDocTemplate(
NULL,
RUNTIME_CLASS(CDoc),
RUNTIME_CLASS(CFrame),
RUNTIME_CLASS(CMain));
AddDocTemplate(pDoc);

Solo
Solo
I suggest creating a new MFC project with all the single document model code in place. Then add your CMain implementation files to the project and replace all occurences of CView with CMain.

Also make sure your view window isn''t still trying to create the frame window.

And then of course, don''t forget that the document, view, and frame window (if MDI) will not be created until a new document is created via CWinApp::OnFileNew() or CWinApp::OnFileOpen().
// Ryan
CProgrammer
CProgrammer
Solo: Thanks for the tip but I found the problem:
It seems I didnt create a usable set of resources IDR_MAINFRAME and didnt ad these lines.

CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
if (!ProcessShellCommand(cmdInfo))
return FALSE;


Thanks a bunch though, your tip about attaching the view to a document worked fine!
-CProgrammer

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.