[ Home | Syllabus | Course Notes | Assignments | Search]
Easiest way to generate a MFC application is to use VC++ Application Wizard
Now it is instructive to look at what you get for free.
Source Files:
Header Files: for the above
#if !defined(AFX_SKELETON_H__BA3C1EA4_4590_11D1_A985_00609752BFB9__INCLUDED_) #define AFX_SKELETON_H__BA3C1EA4_4590_11D1_A985_00609752BFB9__INCLUDED_ #include "resource.h" // main symbols class CSkeletonApp : public CWinApp { public: CSkeletonApp(); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CSkeletonApp) public: virtual BOOL InitInstance(); //}}AFX_VIRTUAL // Implementation //{{AFX_MSG(CSkeletonApp) 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 Developer Studio will insert additional declarations immediately before the previous line. #endif // !defined(AFX_SKELETON_H__BA3C1EA4_4590_11D1_A985_00609752BFB9__INCLUDED_)
FOOTNOTES
BEGIN_MESSAGE_MAP(CSkeletonApp, CWinApp) //{{AFX_MSG_MAP(CSkeletonApp) 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 END_MESSAGE_MAP() // The one and only CSkeletonApp object CSkeletonApp theApp; ///////////////////////////////////////////////////////////////////////////// // CSkeletonApp initialization BOOL CSkeletonApp::InitInstance() { // 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")); // To create the main window, this code creates a new frame window // object and then sets it as the application's main window object. CMainFrame* pFrame = new CMainFrame; m_pMainWnd = pFrame; // create and load the frame with its resources pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL); // The one and only window has been initialized, so show and update it. pFrame->ShowWindow(SW_SHOW); pFrame->UpdateWindow(); return TRUE; }
FOOTNOTES
Instead of FrameWnd directly to allow for status and toolbars
class CChildView : public CWnd { // Construction public: CChildView(); // Attributes public: // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CChildView) protected: virtual BOOL PreCreateWindow(CREATESTRUCT& cs); //}}AFX_VIRTUAL // Implementation public: virtual ~CChildView(); // Generated message map functions protected: //{{AFX_MSG(CChildView) afx_msg void OnPaint(); //}}AFX_MSG DECLARE_MESSAGE_MAP() };
BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) { if (!CWnd::PreCreateWindow(cs)) // let base set up window structure return FALSE; cs.dwExStyle |= WS_EX_CLIENTEDGE; // then we can modify cs.style &= ~WS_BORDER; cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, ::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL); return TRUE; } void CChildView::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here // Do not call CWnd::OnPaint() for painting messages }
// CMainFrame message handlers void CMainFrame::OnSetFocus(CWnd* pOldWnd) { // forward focus to the view window m_wndView.SetFocus(); // needed because it is the view that must handle user input } BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) { // let the view have first crack at the command if (m_wndView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo)) return TRUE; // otherwise, do default handling return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo); }
Select "File/Open"
In file dialog, select "Skeleton.rc", select "text" in the "open as" list on the bottom/select "Open" button (if says already open, continuing will close the resource editor/ select "OK")
Part of this file defines the default menus
///////////////////////////////////////////////////////////////////////////// // // Menu // IDR_MAINFRAME MENU PRELOAD DISCARDABLE BEGIN POPUP "&File" BEGIN MENUITEM "E&xit", ID_APP_EXIT END POPUP "&Edit" BEGIN MENUITEM "&Undo\tCtrl+Z", ID_EDIT_UNDO MENUITEM SEPARATOR MENUITEM "Cu&t\tCtrl+X", ID_EDIT_CUT MENUITEM "&Copy\tCtrl+C", ID_EDIT_COPY MENUITEM "&Paste\tCtrl+V", ID_EDIT_PASTE END POPUP "&Help" BEGIN MENUITEM "&About Skeleton...", ID_APP_ABOUT END END
Toplevel menu includes "File",
"Edit" and "Help"
Each top level item has a pop up menu with the indicated menu items
Other items of interest in this file are the shortcut key "accelerators" and the string table (which contains the "help" information associated with menu items.
You could edit this file to change the menu - or you could use the resource editor (since you opened this as text - close it, then select the "ResourceView" Tab.