Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

MFC D3D Application: Direct3D Tutorial: Part I

, 9 Dec 2012 CPOL
Yet another Direct3D framework, this time for MFC apps, with a step by step tutorial
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "MFCD3D.h"

#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code !
	ON_WM_CREATE()
	//}}AFX_MSG_MAP

	ON_UPDATE_COMMAND_UI(ID_INDICATOR_DEVICE, OnUpdateDeviceStats)
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_FPS,    OnUpdateFrameStats)	
	
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_DEVICE,
	ID_INDICATOR_FPS
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// size the status bar panels
	m_wndStatusBar.SetPaneInfo(m_wndStatusBar.CommandToIndex(ID_INDICATOR_DEVICE),
							   ID_INDICATOR_DEVICE,
							   SBPS_NORMAL,
							   428);

	m_wndStatusBar.SetPaneInfo(m_wndStatusBar.CommandToIndex(ID_INDICATOR_FPS),
							   ID_INDICATOR_FPS,
							   SBPS_NORMAL,
							   50);

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;

	cs.style &= ~FWS_ADDTOTITLE;

	return TRUE;
}

// status bar pane updater
void CMainFrame::UpdateStatusBar(DWORD dwPaneID, LPCTSTR lpszText)
{
	CString* psText;

	// force UI updates...
	switch (dwPaneID)
	{
	case ID_INDICATOR_DEVICE:	psText = &m_sDeviceStats;	break;
	case ID_INDICATOR_FPS:		psText = &m_sFrameStats;	break;
	
	default:
		return;
	}
	
	// with any extra formatting here.
	psText->Format(" %s", lpszText);

	// uncomment the next section for dynamic (content-based) pane resizing; this
	// saves guessing the required pane sizes, but it can be annoying if the text
	// extent of psText varies frequently on broad ranges...

/*	CFont* pFont	= m_wndStatusBar.GetFont();
	CFont* pOldFont = NULL;
	
	// get a client DC for dynamic resizing
	CClientDC dc((CWnd*)&m_wndStatusBar);
	
	if (pFont != NULL)
		pOldFont = dc.SelectObject(pFont);

	// get the text extent
	CSize size = dc.GetTextExtent(*psText);

	// done with DC objects and the DC
	dc.SelectObject(pOldFont);
	ReleaseDC(&dc);

	// actual horizontal resize, with some extra trailing space
	m_wndStatusBar.SetPaneInfo(m_wndStatusBar.CommandToIndex(dwPaneID), dwPaneID, SBPS_NORMAL, size.cx + 3);
*/
}


/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


void CMainFrame::OnUpdateDeviceStats(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(); pCmdUI->SetText(m_sDeviceStats);
}

void CMainFrame::OnUpdateFrameStats(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(); pCmdUI->SetText(m_sFrameStats);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

hector [.j.] rivas
Software Developer (Senior) CompuCom, Inc.
United States United States
Hector J. Rivas has 25+ years of experience managing hardware and software development under many different operating systems, platforms and languages. He has developed microcontroller interfaces and PC games; authored computer based training lessons and delivered fully functional financial and administrative data-intensive applications, as well as image processing and other calculation-intensive applications. Mr. Rivas has also managed Y2K remediation, large scale platform migration and Web site projects, from R&D to actual deployment.
Follow on   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 9 Dec 2012
Article Copyright 2006 by hector [.j.] rivas
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid