This is a continuation from the previous module... Program examples compiled using Visual C++ 6.0 (MFC 6.0) compiler on Windows XP Pro machine with Service Pack 2. Topics and sub topics for this Tutorial are listed below. You can compare the standard C file I/O, standard C++ file I/O and Win32 directory, file and access controls with the MFC serialization. So many things lor! Similar but not same. Those links also given at the end of this tutorial.
|
CMymfc17App Class
The application class files, shown in Listing 4, contain only code generated by AppWizard. The application was generated with a default file extension and with the Microsoft Windows Explorer launch and drag-and-drop capabilities. These features are described later in this module. To generate additional code, you must do the following when you first run AppWizard (already shown in the previous steps): in the AppWizard Step 4 page, click the Advanced button. When the Advanced Options dialog appears, you must enter the Filename extension in the upper-left control, as shown here.
Figure 21: Setting the File extension for MYMFC17 project, as done in step 4 of 6 AppWizard.
This ensures that the document template resource string contains the correct default extension and that the correct Explorer-related code is inserted into your application class InitInstance() member function. You can change some of the other resource substrings if you want. The generated calls to Enable3dControls() and Enable3dControlsStatic() in CMymfc17App::InitInstance are not necessary with Microsoft Windows 95, Microsoft Windows 98, or Microsoft Windows NT 4.0. These two functions support an older DLL that is shipped with Microsoft Windows 3.51. |
MYMFC17.H // mymfc17.h : main header file for the MYMFC17 application //
#if !defined(AFX_MYMFC17_H__1A036EA3_821A_11D0_8FE2_00C04FC2A0C2__INCLUDED_) #define AFX_MYMFC17_H__1A036EA3_821A_11D0_8FE2_00C04FC2A0C2__INCLUDED_
#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif
#include "resource.h" // main symbols
/////////////////////////////////////////////////////////////////////// // CMymfc17App: // See mymfc17.cpp for the implementation of this class //
class CMymfc17App : public CWinApp { public: CMymfc17App();
// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CMymfc17App) public: virtual BOOL InitInstance(); //}}AFX_VIRTUAL
// Implementation
//{{AFX_MSG(CMymfc17App) afx_msg void OnAppAbout(); // 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() };
///////////////////////////////////////////////////////////////////// //
//{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations // immediately before the previous line.
#endif // !defined(AFX_MYMFC17_H__1A036EA3_821A_11D0_8FE2_00C04FC2A0C2__INCLUDED_)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MYMFC17.CPP // mymfc17.cpp : Defines the class behaviors for the application. //
#include "stdafx.h" #include "mymfc17.h"
#include "MainFrm.h" #include "mymfc17Doc.h" #include "mymfc17View.h"
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif
/////////////////////////////////////////////////////////////////////// // CMymfc17App
BEGIN_MESSAGE_MAP(CMymfc17App, CWinApp) //{{AFX_MSG_MAP(CMymfc17App)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout) // 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 ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////// // CMymfc17App construction
CMymfc17App::CMymfc17App() { // TODO: add construction code here, // Place all significant initialization in InitInstance }
/////////////////////////////////////////////////////////////////////// // The one and only CMymfc17App object
CMymfc17App theApp; /////////////////////////////////////////////////////////////////////// // CMymfc17App initialization
BOOL CMymfc17App::InitInstance() { AfxEnableControlContainer();
// 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
// Change the registry key under which our settings are stored. // You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. // Document templates serve as the connection between // documents, frame windows and views.
CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CMymfc17Doc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CMymfc17View)); AddDocTemplate(pDocTemplate);
// Enable DDE Execute open EnableShellOpen(); RegisterShellFileTypes(TRUE); // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE;
// The one and only window has been initialized, // so show and update it. m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow();
// Enable drag/drop open m_pMainWnd->DragAcceptFiles();
return TRUE; }
/////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About
class CAboutDlg : public CDialog { public: CAboutDlg();
// Dialog Data //{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA
// ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL
// Implementation protected: //{{AFX_MSG(CAboutDlg) // No message handlers //}}AFX_MSG DECLARE_MESSAGE_MAP() };
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT }
void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP }
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP()
// App command to run the dialog void CMymfc17App::OnAppAbout() { CAboutDlg aboutDlg; aboutDlg.DoModal(); }
/////////////////////////////////////////////////////////////////////// // CMymfc17App commands
|
Listing 4: The CMymfc17App class listing.
CMainFrame Class
The main frame window class code, shown in Listing 5, is almost unchanged from the code that AppWizard generated. The overridden ActivateFrame() function and the WM_DROPFILES handler exist solely for trace purposes.
MAINFRM.H // MainFrm.h : interface of the CMainFrame class // ///////////////////////////////////////////////////////////////////////
#if !defined(AFX_MAINFRM_H__1A036EA7_821A_11D0_8FE2_00C04FC2A0C2__INCLUDED_) #define AFX_MAINFRM_H__1A036EA7_821A_11D0_8FE2_00C04FC2A0C2__INCLUDED_ #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000
class CMainFrame : public CFrameWnd { protected: // create from serialization only CMainFrame(); DECLARE_DYNCREATE(CMainFrame)
// Attributes public:
// Operations public:
// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CMainFrame) public: virtual BOOL PreCreateWindow(CREATESTRUCT& cs); virtual void ActivateFrame(int nCmdShow = -1); //}}AFX_VIRTUAL
// Implementation public: virtual ~CMainFrame(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif
protected: // control bar embedded members CStatusBar m_wndStatusBar; CToolBar m_wndToolBar;
// Generated message map functions protected: //{{AFX_MSG(CMainFrame) afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnDropFiles(HDROP hDropInfo); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations // immediately before the previous line.
#endif // !defined(AFX_MAINFRM_H__1A036EA7_821A_11D0_8FE2_00C04FC2A0C2__INCLUDED_)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MAINFRM.CPP // MainFrm.cpp : implementation of the CMainFrame class //
#include "stdafx.h" #include "mymfc17.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) ON_WM_CREATE() ON_WM_DROPFILES() //}}AFX_MSG_MAP END_MESSAGE_MAP()
static UINT indicators[ ] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL, }; /////////////////////////////////////////////////////////////////////// // 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.Create(this) || !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 }
// TODO: Remove this if you don't want tool tips or a resizable toolbar m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
// 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) { // TODO: Modify the Window class or styles here by modifying the CREATESTRUCT cs return CFrameWnd::PreCreateWindow(cs); }
/////////////////////////////////////////////////////////////////////// // 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::ActivateFrame(int nCmdShow) { TRACE("Entering CMainFrame::ActivateFrame\n"); CFrameWnd::ActivateFrame(nCmdShow); }
void CMainFrame::OnDropFiles(HDROP hDropInfo) { TRACE("Entering CMainFrame::OnDropFiles\n"); CFrameWnd::OnDropFiles(hDropInfo); }
|
Listing 5: The CMainFrame class listing.
Continue on next module...part 4.
Further reading and digging:
MSDN MFC 9.0 class library online documentation - latest version.
DCOM at MSDN.
COM+ at MSDN.
COM at MSDN.
Unicode and Multi-byte character set: Story and program examples.