Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hi All, I am new to this concept of Document View Architecture so please can any one help me to solve this problem and please give some programming example.Thanks for help...
[Update]
Any one please help me ...
Posted
Updated 19-Nov-13 21:41pm
v2

I suppose Google won't help you: "mfc document view architecture tutorial"[^].
 
Share this answer
 
When I am starting a new project I always choose between SDI or Dialog based depending on the interface layout I will need. Do not get sidetracked by "Document/View", I have created hundreds of projects but never once used the "Document" capabilities. One of the first things I do after generating a SDI project is remove the generated CDocument code. Think of it more as "Frame Window / View" vs "Dialog" based.

If your interface needs to be a complex, with frame windows you have built in control bars and you still have the CFormView class (which I use allot). If your project is a simple utility you would probably choose the Dialog based project.

The AppWizard generates a skeleton project for you but as a minimalist I try and remove all unnecessary code. I will post an example of the way I do things with SDI projects, I hope it helps a little.

Here is an example of an alternative way to create your main window:

//=================================================
// Global variables
//=================================================
CMainFrame*		g_pMainFrame = NULL;
CString			g_cstrWinClass("YourWindowClassName");


BOOL CApp::InitInstance()
{
	// InitCommonControls() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	INITCOMMONCONTROLSEX iccx = {0};
	iccx.dwSize = sizeof(iccx);
	iccx.dwICC = ICC_STANDARD_CLASSES;
	InitCommonControlsEx(&iccx);

	CWinApp::InitInstance();

	// Create a window class
	WNDCLASS wc = {0};
	wc.style = CS_DBLCLKS|CS_GLOBALCLASS;
	wc.lpfnWndProc = ::DefWindowProc;
	wc.hInstance = AfxGetInstanceHandle();
	wc.hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	wc.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszClassName = g_cstrWinClass;

	// Register the window class
	if(!::GetClassInfo(wc.hInstance, wc.lpszClassName, &wc))
	{
		if(!::RegisterClass(&wc))
		{
			TRACE(_T("Window class could not be registered\n"));
			return FALSE;
		}
	}


	// Create main window
	g_pMainFrame = new CMainFrame;
	m_pMainWnd = g_pMainFrame;
	// Call CreateWindow()
	DWORD dwStyle = WS_OVERLAPPEDWINDOW;
	if(!g_pMainFrame->Create(
		g_cstrWinClass, g_cstrWinName,
		dwStyle, CRect(0,0,1024,768), NULL,
		MAKEINTRESOURCE(IDR_MAINFRAME), NULL))
		return FALSE;
	g_pMainFrame->CenterWindow();
	g_pMainFrame->ShowWindow(SW_SHOW);
	g_pMainFrame->UpdateWindow();

	return TRUE;
}


Here is an alternative way to create your view(s) inside your frame window init function:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if(CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	... 

	// Create the main view
	CCreateContext context;
	memset(&context, 0, sizeof(CCreateContext));
	context.m_pNewViewClass = RUNTIME_CLASS(CMainView);
	VERIFY(g_pMainView = STATIC_DOWNCAST(CMainView,
		CreateView(&context, AFX_IDW_PANE_FIRST)));
	g_pMainView->OnInitialUpdate();
	SetActiveView(g_pMainView);

	return 0;
}


Here is how to switch out your active view:

void CCSMainFrame::SwitchView(CView* pNewView)
{
	ASSERT(pNewView);
	CView* pOldView = GetActiveView();
	if(pOldView == pNewView)
		return;

	if(pOldView)
	{
		// Hide the old view
		pOldView->ShowWindow(SW_HIDE);
		::SetWindowLong(pOldView->m_hWnd, GWL_ID, AFX_IDW_PANE_FIRST+1); 
	}
	if(pNewView)
	{
		// Show the new view
		pNewView->BringWindowToTop();
		pNewView->ShowWindow(SW_SHOWNORMAL);  
		pNewView->UpdateWindow();
		::SetWindowLong(pNewView->m_hWnd, GWL_ID, AFX_IDW_PANE_FIRST);  
		SetActiveView(pNewView);	
		RecalcLayout();	
	}
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900