11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 13. Adding Special Features to Application<br />

CMainFrame::CMainFrame()<br />

{<br />

CString szWinName;<br />

szWinName.LoadString(IDR_MAINFRAME);<br />

}<br />

Create<br />

(<br />

NULL, szWinName, WS_OVERLAPPEDWINDOW, rectDefault,<br />

NULL, MAKEINTRESOURCE(IDR_MAINFRAME)<br />

);<br />

We must provide a window name, which will be displayed in the caption bar of the window. In<br />

standard SDI or MDI applications, this string can be obtained from string resource IDR_MAINFRAME. To<br />

make the sample similar to a standard application, we can load this string and use it as the window name.<br />

Since we will not support any file type, in the sample string resource IDR_MAINFRAME contains only one<br />

simple string (This means it does not contain several sub-strings that are separated by character ‘\n’ as in<br />

standard SDI or MDI applications).<br />

For a simple application, there is no need to implement status bar and tool bar any more, so function<br />

CMainFrame::OnCreate(…) is removed, and variables CMainFrame::m_wndStatusBar, CMainFrame::<br />

m_wndToolBar along with another global variable indicators are also deleted.<br />

For any application implemented by <strong>MFC</strong>, it is originated from class CWinApp. This class has a CWnd<br />

type member pointer m_pMainWnd. When the mainframe window is created, its address is stored by this<br />

pointer. If we want to create window by ourselves, we must do the same thing in order to let the rest part of<br />

our application have <strong>MFC</strong> features.<br />

With the above implementation, function CGenApp::InitInstance(…) can be greatly simplified, what<br />

we need to do here is implementing a CMainFrame type object, assigning its address to CGenApp::<br />

m_pMainWnd, then calling functions CWnd::ShowWindow(…) and CWnd::UpdateWindow(…) to display the<br />

window. This last step is necessary, if we omit it, the window will not be displayed. The following is the<br />

modified function in the sample:<br />

BOOL CGenApp::InitInstance()<br />

{<br />

#ifdef _AFXDLL<br />

Enable3dControls();<br />

#else<br />

Enable3dControlsStatic();<br />

#endif<br />

m_pMainWnd=new CMainFrame();<br />

m_pMainWnd->ShowWindow(m_nCmdShow);<br />

m_pMainWnd->UpdateWindow();<br />

}<br />

return TRUE;<br />

Here variable m_nCmdShow indicates how the window should be displayed (minimized, maximized,<br />

etc). It must be passed to function CWnd::ShowWindow(…) in order to initialize the window to a specified<br />

state.<br />

Excluding Classes from Build<br />

Although we do not need to use view and document classes anymore (CGenView and CGenDoc in the<br />

sample), it is difficult to remove them from the project once they are generated automatically. However, we<br />

can change the project settings so that the two classes will not be complied when the project is being built.<br />

This can be achieved through following steps: 1) Executing command Project | Settings.... 2) From the<br />

popped up dialog box, expand “Source Files” node in “Settings For:” window. 3) Select “GenView.cpp”<br />

and click on “General” tab on the right part of the dialog box. 4) Check “exclude file from build” check box<br />

(Figure 13-1). We can do the same thing for file “GenDoc.cpp” to exclude class CGenDoc from build.<br />

396

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!