Building an MFC Application

Building an MFC Application

Building an MFC Application Using the Wizard 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. 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 Project A container for items e.g.; code, .rc files May be used in other projects Types specify language,etc.

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 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" Using VS 2013 Premiere - 3 You should now have Two .h files MainFrame.h function prototypes & global variables MyWinApp.h name of your program & project #include 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 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 } 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 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 = 700; //window width = 300; //window height

OnCreate Most actions inserted by the Wizard Can now SetFont, use it's size Compute row positions for TextOut/DrawText OnPaint Must do: CPaintDC dc(this); // 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); Setting Font VERIFY(myfont.CreateFont( 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); 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 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 to your code

MFC-Hello sample MainFrm.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.

Basic Structure of an MFC program afxwin.h MyWinApp.h MainFrame.h MainFrame.cpp Main.cpp MyWinApp.cpp 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); Resource (.rc) files Collection of UI elements Provide info to user Icons Bitmaps Toolbars Cursors Dialogs Accelerators

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

Recently Viewed Presentations

  • SET-BC Synergy Project

    SET-BC Synergy Project

    SET-BC Synergy Project. École Oceanside Elementary School. Gaynor Charnock - Karen Lawrence - Amber Tanner. ... "can be tricky to find the right picture" "It's really fun because you can draw on the pictures and incorporate your voice"
  • Elasticity - Homepages

    Elasticity - Homepages

    Size Scale Continuum Mechanics Assumption Material Behavior is Size Dependent Dislocation Core Void Stress Field Crack-path Dependence on Residual Stress 3/22/2002, Friday Multiscale Concept Size Scale Continuum Mechanics Assumption Material Behavior is Size Dependent Dislocation Core Void Stress Field Crack-path...
  • DNA Extraction - Mrs. Duddles Science

    DNA Extraction - Mrs. Duddles Science

    Brightest Star - Polaris. Best season to view - all year. Remember. Zeus loved Callisto and changed her into a bear to protect her from his jealous wife. Arcas liked to hunt and almost killed his mother, Callisto, Big Bear....
  • Tomorrows Results Today - Ankersmid

    Tomorrows Results Today - Ankersmid

    D- Count® Applications for the Food and Cosmetic Industry Elke Kohler, Chemunex SA, Paris D-Count ® : Non-Filterable Product Testing D-Count ® Technology FluorassureÒ Viability Markers Cell Labelling Technology D-Count ® : Optical Bench Data Processing D-Count ® Main Menu...
  • Memory Chapter 7A - Hopkins Helpful Hints

    Memory Chapter 7A - Hopkins Helpful Hints

    The Atkinson-Schiffrin (1968) three-stage model of memory includes a) sensory memory,b) short-term memory, and c) long-term memory. Preview Question 1: How do psychologists describe the human memory system? This is the OLD way to describe our memory system - there...
  • Matter and Change

    Matter and Change

    VAPOR The gaseous state of a substance that is generally a liquid or solid at room temperature. Steam (the gaseous state of matter) is referred to as a vapor because water is a liquid at room temperature. Moist air contains...

    Conclusion. NoC is an efficient on-chip communication architecture for SoC architectures. Topologies is a key factor in MP-NoC design. Some architectures can sustain very high data rates at the expense of high-energy dissipation and considerable silicon area overhead, while others...
  • The Reign of Louis XIV

    The Reign of Louis XIV

    It resulted from those who were horrified by groups that claimed to be the only ones with a correct set of doctrines. Montaigne and Descartes Montaigne explores ideas about life's meaning in essays. Descartes uses observation and reason to create...