Site Contents

Home
Programming
OpenGL Demos
MFC OpenGL
Tutorial 1
Graphics Demos
Jonathan
Travel
Garden

Main Guestbook
Help!
Contact Us

Forum

ISP Referral

Page Validation

Valid XHTML 1.0!
Valid CSS!

tutorial 1 - simple project

Creating a Window

The first thing that we have to do with the code is turn our dialog into a window. The code snippet below is from the file "MFC_Tutorial_01.h". The three lines in blue are the ones that you need to change or add to your code. What we are doing here is including the header file which contains the dialog class, adding a reference to our dialog window as a member of the application class (CApp) and making the instance of our application class global so that we can use it in our dialog.

#include "Dlg.h"

/////////////////////////////////////////////////////////////////////////////
// CApp:
// See MFC_Tutorial_01.cpp for the implementation of this class
//

class CApp : public CWinApp
{
public:
	CApp();

	CDlg *m_pTheDialog;

 // Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CApp)
	public:
	virtual BOOL InitInstance();
	//}}AFX_VIRTUAL

 // Implementation

	//{{AFX_MSG(CApp)
		// NOTE - the ClassWizard will add and remove member functions here.
		//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

extern CApp theApp;

Next, we will work on our application class in the file "MFC_Tutorial_01.cpp". Find in the file the implementation of BOOL CApp::InitInstance(). Inside you will find a section of code that looks like this:


CDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
	// TODO: Place code here to handle when the dialog is
	//  dismissed with OK
}
else if (nResponse == IDCANCEL)
{
	// TODO: Place code here to handle when the dialog is
	//  dismissed with Cancel
}

// Since the dialog has been closed, return FALSE so that we exit the
//  application, rather than start the application's message pump.
return FALSE;

Delete that section of code because we are going to rewrite it. Here is the code to replace it with:


// tell MFC which window will be the main one
m_pMainWnd = new CDlg;

// store a pointer to our dialog window
m_pTheDialog = (CDlg*)m_pMainWnd;

// open the dialog window
// IDD_MFC_TUTORIAL_01_DIALOG is the resource id for the dialog in the resource editor
// you can still open a window the Win32 way, but I'm using the resource editor to
// make this tutorial easier to understand
m_pTheDialog->Create(IDD_MFC_TUTORIAL_01_DIALOG, NULL);

return TRUE;

At this point, you will need to use the MFC ClassWizard by clicking View|ClassWizard to add an ExitInstance function override to our CApp class. The screenshot below shows you what my ClassWizard window looked like when I added the new function.

AppWizard Generated dialog (click to enlarge)

This method is called every time your application closes down, so this is the place to insert some code to close our window, or else we won't be able to quit our application. Here is what you need to put in the method:

int CApp::ExitInstance() 
{
	if (m_pMainWnd)
	{
		delete m_pMainWnd;
	}

	return CWinApp::ExitInstance();
}

By going into your Workspace Resource Viewer, select the Dialog Resource and remove the 3 controls that the AppWizard generated for us. Your Dialog Editor window should look something like this now:

AppWizard Generated dialog (click to enlarge)

At the moment, our window is really a system modal dialog which means that it is a window that expects user input (OK or Cancel). The window class that our dialog is derived from sets this up by default, so we have to override those features. To do this, you have to add a couple of declarations to the CDlg class (written in blue):

class CDlg : public CDialog
{
// ... code snipped
public:
	CDlg(CWnd* pParent = NULL);	// standard constructor


	// for overriding the default modal dialog functions 
	virtual void OnOK();
	virtual void OnCancel();

// ... more code snipped from here onwards
};

When you have done that, you need to implement them in "Dlg.cpp" like this:

void CDlg::OnOK()
{
	// this overrided function must be left blank
}

void CDlg::OnCancel()
{
	// this must be called otherwise clicking the X in the window bar will not work
	PostQuitMessage(0);
}

That's the first stage of the tutorial completed! You have a simple window that opens when you run the programme and automatically closes when you quit. In the next part we will get the dialog ready to accept OpenGL into it!

 

Part 1: Introduction Part 2: Getting Started

Part 3: Creating a Window Part 4: Using OpenGL

Go to Top

Page Stats

Page last updated: 19:21 GMT on Saturday 1 January 2005
This page has had 2798 hits since 27 March 2004. Last viewed on 6 September 2010.
It took the server 0.0202601 seconds to generate this page.

Site designed and maintained by Jonathan Copelin
Go to homepage