1 / 20

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

Download Presentation

Building an MFC Application

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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:componentsdoes 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. Using VS 2013 Premiere - 1 • Open VS • Select FilenewProject • Select Visual C++ • In the center area, select MFC Application • At the bottom of the screen, enter a name for your project (like "JSmith-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 • 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" CMyWinAppMyApplication; • MyWinApp.cpp – #include MyWinApp.h, InitInstancecode • 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 CStringinit'd here) cs.x = 0; //Y pos of window cs.y = 0; //X 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: • 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);

  13. 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);

  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_msgint 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; CMainFrameis 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_pMainWndis 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 • 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 Cfontmyfont; 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. Resource (.rc) files • Collection of UI elements http://msdn.microsoft.com/en-us/library/y3sk7e6b.aspx • 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

More Related