Download presentation
Presentation is loading. Please wait.
1
Building an MFC Application
Using the Wizard
2
Terms Solution A container for related projects An executable or DLL
May be constructed from multiple projects Each project or solution is a directory and its contents May have different versions May generate multiple applications Solutions & projects manageable in W. Explorer Logical relationship of solution:components does not necessarily mirror the way they are stored on disk.
3
Solutions, Projects & directories
Solution1 (in VS explorer view) App1 (Project 1) X.cpp X.h App2 (Project 2) App3 (Project 3) Visual Studio Projects (WinExplorer view) Solution 1 (folder) App1 (folder) App2 (folder) App3 (folder) Solution1.sln (shareable metadata) Solution1.suo (user-specific metadata) App1 might depend on App2 & App3 Managed with project dependencies
4
Project A container for items e.g.; code, .rc files May be used in other projects Types specify language,etc.
5
Select FilenewProject Select Visual C++
Using VS 2013 Premiere - 1 Open VS Select FilenewProject Select Visual C++ In the center area, select MFC Application At the bottom of the screen, enter a name for your project (like "Smith-J-MFC-app") (use your own name!!) At the bottom right corner Check the box for "create directory" Click OK
6
Using VS 2013 Premiere - 2 In the navigation panel, right-click your project name (NOT the solution name) Click on Properties Ensure these values are present: Under General: Configuration type: Application (.exe) Use of MFC: Use MFC in a Shared DLL Assumes user's PC has MFC installed Faster than "Use MFC in a static library" Common language runtime (CLR): no CLR Under C/C++, Code Generation, Runtime Library "Multithreaded Debug" Under Pre-compiled headers: "Not using pre-compiled headers"
7
Using VS 2013 Premiere - 3 Two .h files Three .cpp files
You should now have Two .h files MainFrame.h – function prototypes & global variables MyWinApp.h – name of your program & project #include <afxwin.h> class CMyWinApp : public CWinApp {public: virtual BOOL InitInstance(); }; Three .cpp files Main.cpp #include "MyWinApp.h" CMyWinApp MyApplication; MyWinApp.cpp – #include MyWinApp.h, InitInstance code MainFrame.cpp – #include both .h files, everything else
8
Classes & Global variables
Declare global vars in MainFrm.h Define, initialize in PreCreateWindow or in OnCreate following is in MainFrm.h class CMainFrame : public CFrameWnd { // no initializations here!!! private: // declare global variables here public: // declare constructors and prototypes for // other functions in MainFrame.cpp void OnPaint(); // for example }
9
Flow of execution (hidden) WinMain runs InitInstance begins
CMainFrame runs (does nothing in our case) Main window object (CREATESTRUCT) created PreCreateWindow Allows "filling-in" of struct values Window exists but is not displayed yet Sends WM_CREATE on ending OnCreate Creates a view to occupy the window Creates a status bar (if requested) Initialize your own variables here InitInstance ends – sends WM_PAINT OnPaint writes/draws text & graphics, shows screen
10
PreCreateWindow Optional settings "cs" is the name of the CREATESTRUCT passed to the PreCreateWindow member cs.lpszName = title; (a CString init'd here) cs.x = 0; //X pos of window cs.y = 0; //Y pos of window cs.cx = 700; //window width cs.cy = 300; //window height
11
OnCreate Most actions inserted by the Wizard Can now SetFont, use it's size Compute row positions for TextOut/DrawText
12
OnPaint Must do: MAY do this: CPaintDC dc(this); Invalidate();
// Invalidate window so entire client area // is redrawn when UpdateWindow is called. Invalidate(); // Update Window to cause View to redraw. UpdateWindow(); MAY do this: SetFont(&myfont, true); dc.TextOut(formatted string);
13
Setting Font VERIFY(myfont.CreateFont( SetFont(&myfont, true);
24, // nHeight in points 0, // nWidth 0, // nEscapement 0, // nOrientation FW_NORMAL, // nWeight TRUE, // bItalic FALSE, // bUnderline 0, // cStrikeOut ANSI_CHARSET, // nCharSet OUT_DEFAULT_PRECIS, // nOutPrecision CLIP_DEFAULT_PRECIS, // nClipPrecision DEFAULT_QUALITY, // nQuality DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily _T("Arial"))); // lpszFacename SetFont(&myfont, true);
14
Debugging tips AfxMessageBox (_T("your message"));
Be CAREFUL where you put AfxMessageBox. If it's in an OnMove, it will pop up for EVERY pixel of the movement!! Same for OnPaint (pops up for each redraw) May need to test for !m_wndView in OnSetFocus Use a test to prevent multiple popups Win32API attribute "afx_msg" is empty: E.g.; afx_msg int OnCreate(…) "afx_msg" does not apply to MFC programs Can be safely omitted when coding Wizard keeps it for commonality
15
Window Creation Create new frame window object
CMainFrame* pFrame = new CMainFrame; CMainFrame is the frame object pFrame points to the object Set it as main window object m_pMainWnd = pFrame; Load frame's resources pFrame->LoadFrame(……parameters….) Now show & update the window Notes: "stdafx.h" includes "afxwin.h" m_pMainWnd is defined in afxwin.h Starting with version 3.0, all MFC classes are "thread safe" and require the multi-threaded run-time libraries to link successfully. Do NOT add: #include <windows.h> to your code
16
MFC-Hello sample MainFrm.cpp MFC-hello.cpp
1 message map (for CMainFrame class) handlers - on_wm_create() , on_wm_setfocus() MFC-hello.cpp 2 message maps (for CMFChelloApp class, for CAboutDlg class) there are two message maps, because there are 2 classes, though the message map for the CAboutDlg class does not have any handlers. rule is 1 map per class Childview.cpp - 1 message map for the cChildview class.
17
Basic Structure of an MFC program
afxwin.h MyWinApp.h MainFrame.h Main.cpp MyWinApp.cpp MainFrame.cpp
18
Using fonts CreateFont – allows specifying EVERYTHING
CreatePointFont – specify only size & typeface Cfont myfont; myfont.CreateFont (…); or myfont.CreatePointFont (…); Activate it: Method 1 (using a pointer to the context object) CDC* dc; dc = GetDC(); CFont * fontptr = dc->SelectObject(&myfont); Method 2 (using the context object directly) CPaintDC dc(this);// "this" is handle for current window CFont * fontptr = dc.SelectObject(&myfont);
19
Collection of UI elements
Resource (.rc) files Collection of UI elements Provide info to user Icons Bitmaps Toolbars Cursors Dialogs Accelerators
20
Resource files & their content
Projname.rc toolbars, dialog boxes, etc. Accelerator and string tables. Default "About" dialog box. Resource.h definitions for the resources Projname.ico The icon file for the project or a control
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.