![]() |
![]() |
|||||||
|
Login Change Info Logout
DOWNLOADS |
by Alan Oursland
Introduction MFC was created to make programming in Windows easier. As an object-oriented wrapper for Win32, it automates many routine programming tasks (mostly passing references around). Paradigms like the document/view architecture were added to automate even more tasks for the programmer, but in the process, some control was taken away. This tutorial will show you how to create a very simple MFC program and how to customize it for your application. This program will be a base for other tutorials that I write for OpenGL and DirectX. I will be going through this tutorial in Visual C++ 6.0. We will begin by creating a standard MFC Doc/View application using AppWizard. Select New from the File menu, click on the Projects tab, and select MFC AppWizard (exe). Enter SimpleMFC as the project name and press OK.
We now have a standard Doc/View application with the following classes: CAboutDlgCMainFrame CSimpleMFCApp CSimpleMFCDoc CSimpleMFCView Go to the file view and remove the following files from the project and delete them from the directory: SimpleMFCView.hSimpleMFCView.cpp SimpleMFCDoc.h SimpleMFCDoc.cpp You can also remove IDR_SIMPLETYPE from the icon resource tab and delete the associated file SimpleMFCDoc.ico (be sure to do it in that order). We now have a project that doesn't compile. Edit SimpleMFC.cpp:
CSimpleMFCApp::CSimpleMFCApp()
{
m_pMainWnd = NULL;
// Place all significant
// initialization in InitInstance
}
BOOL CSimpleMFCApp::InitInstance()
{
// Standard initialization
// If you are not using these features
// and wish to reduce the size
// of your final executable, you should
// remove from the following
// the specific initialization routines
// you do not need.
#ifdef _AFXDLL
Enable3dControls();
// Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic();
// Call this when linking to MFC statically
#endif
// The one and only window has been initialized,
// so show and update it.
m_pMainWnd = new CMainFrame();
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CSimpleMFCApp, CWinApp)
//{{AFX_MSG_MAP(CSimpleMFCApp)
// NOTE - the ClassWizard will add
// and remove mapping macros here.
// DO NOT EDIT what you see in these
// blocks of generated code!
//}}AFX_MSG_MAP
// Standard file based document commands
END_MESSAGE_MAP()
Remove the definition for OnAppAbout from SimpleMFC.h. We still have a program that doesn't compile. CMainFrame is expecting to be created dynamically within the Doc/View architecture, thus its constructor is protected. We will fix that. Edit MainFrm.h:
Edit MainFrm.cpp:
CMainFrame::CMainFrame()
{
RECT r;
r.left = 100;
r.top = 100;
r.right = 200;
r.bottom = 200;
Create(NULL, "Simple MFC App",
WS_POPUP|WS_THICKFRAME,
r, NULL, NULL, 0, NULL );
}
If you run the program, a window with a border will appear. It will not have a title or menu bar. This is about the simplest Windows application you can make. Press Alt-F4 and Windows will send a WM_CLOSE message to the application to close it. Let's take a look at what is going on here. CSimpleMFCApp is your application derived from CWinApp. CWinApp contains a pointer to the main application window. We have a global instance of CSimpleMFCApp. When it is created it instantiates an instance of our CMainFrame and assigns it as the CWinApp main window. It then displays the window. This is all that the application has to do. We will not be modifying it further. CMainFrame creates the actual internal system window. Remember that MFC is just a wrapper for Win32. CMainframe, which is derived from CFrameWnd, is not actually an MS-Windows window. It just manages the handle to a window that MS-Windows creates. (The fact that the product is named Windows and the objects themselves are called "windows" may make this confusing.) In the Doc/View architecture, the window handle is created "dynamically" (which basically means the base class creates it in a predefined way). Since we are not using the Doc/View architecture, and we want more control over the window, we create the window handle ourselves by calling CFrameWnd::Create in the CFrameWnd constructor. CFrameWnd::Create eventually calls the Win32 create, but it manages the window handle for us. Create takes several parameters which are the key to customizing this window.
BOOL Create( LPCTSTR lpszClassName,
LPCTSTR lpszWindowName,
DWORD dwStyle = WS_OVERLAPPEDWINDOW,
const RECT& rect = rectDefault,
CWnd* pParentWnd = NULL,
LPCTSTR lpszMenuName = NULL,
DWORD dwExStyle = 0,
CCreateContext* pContext = NULL );
That's about it. Once the window is created, it uses a function inside MFC for the message handling, so you don't need to do anything there. In the next section, we'll play around with this window, so you can customize it to suit your needs.
|
|
|
|||||||||||||||||||||||||
|
Questions or Comments? devcentral AT iticentral DOT com PRIVACY POLICY |