1 / 22

Microsoft Foundation Classes

Microsoft Foundation Classes. What is MFC?. Set of C++ classes written by MS Simplifies writing complex programs Covers many areas: GUI I/O O/S interfaces ActiveX Framework for IE extensions Component of Windows. Why Use MFC?. Faster development More code is reusable

lee-lewis
Download Presentation

Microsoft Foundation Classes

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. Microsoft Foundation Classes

  2. What is MFC? • Set of C++ classes written by MS • Simplifies writing complex programs • Covers many areas: • GUI • I/O • O/S interfaces • ActiveX • Framework for IE extensions • Component of Windows

  3. Why Use MFC? • Faster development • More code is reusable • Many common tasks are "built-in" • Winmain, WndProc • Smaller executable • Uses all the C++ paradigms • Inheritance, polymorphism, etc.

  4. MFC class categories • CObject – the root class • Application architecture classes • Window, dialog, control • Drawing, printing • Datatypes • Array, list, map • File & DB • Internet & networking • OLE • Debugging Used in this course

  5. Cobject • Think of it as the "master" class • Serialization • Stream data to/from devices (disks) • Runtime class info • Debugging • Some derived classes • Cmenu (menus & menu mgmt.) • CDC (device context & drawing) • CGdiObject (drawing devices – brushes, etc)

  6. Related info • MFC class hierarchy chart • Macros & global variables • WndProc (Win32API) buried in MFC library • Message Maps • Names of events • Handler functions for those events • E.g.; ON_WM_MOVE for CMainFrame::OnMove • ON_WM_MOVE pre-defined constant • OnMove pre-defined fn in the CMainFrame class

  7. WIn32API vs MFC Message handling Window gets moved Window gets moved WndProc() case WM_MOVE: movefn(); CMainFrame::OnMove Win32API MFC

  8. Parts of a basic MFC Program - 1MyWinApp.h #include <afxwin.h> class CMyWinApp : public CWinApp { public: virtual BOOL InitInstance(); };

  9. Parts of a basic MFC Program – 2MainFrame.h #include <afxwin.h> class CMainFrame : public CFrameWnd { private: // variables known to all CMainFrame members public: // define the prototypes for functions to handle window events CMainFrame(); // class constructor // prototypesfor members named in the MESSAGE MAP // The MESSAGE MAP is implemented in MainFrame.cpp //afx_msgint OnCreate(LPCREATESTRUCT lpCreateStruct); //afx_msg void OnShowWindow(BOOL bShow, UINT nStatus); afx_msgvoid OnMove(int x, int y); afx_msg void OnPaint(); // msghndlr for WM_PAINT event void common(CWnd* wptr); DECLARE_MESSAGE_MAP(); }; // note: the "afx_msg" prefix is pre-VS 2005 and is #defined as empty

  10. Parts of a basic MFC Program – 3 // Main.cpp #include "MyWinApp.h" CMyWinAppMyApplication;

  11. Parts of a basic MFC Program – 4// MyWinApp.cpp // create the window frame & display it #include "MyWinApp.h" #include "MainFrame.h" BOOL CMyWinApp::InitInstance() { CMainFrame *pFrame; pFrame= new CMainFrame; m_pMainWnd = pFrame; pFrame->ShowWindow(SW_SHOW); pFrame->UpdateWindow(); return TRUE; }

  12. Parts of a basic MFC Program – 5aMainFrame.cpp • This is where all the real work gets done #include "MainFrame.h" // Define the messages we will respond to and create the window BEGIN_MESSAGE_MAP (CMainFrame, CFrameWnd) // classname, base class //ON_WM_CREATE() //ON_WM_SHOWWINDOW() //ON_WM_ACTIVATE() ON_WM_MOVE() ON_WM_PAINT() END_MESSAGE_MAP()

  13. Parts of a basic MFC Program – 5bMainFrame.cpp – part 2 CMainFrame::CMainFrame()// Explicit constructor { CString title = "DJ's WIndow"; Create (NULL /* classname*/, title, WS_OVERLAPPEDWINDOW, CRect (0, 0, 670, 300), NULL, /* parent window ptr*/ 0, /* ID */ NULL /* create context ptr - onlyif overriding the context */ ); }

  14. Parts of a basic MFC Program – 5c void CMainFrame::OnPaint() {CPaintDC dc(this);// "this" is a handle for current window // dc is the "device context" for the window dc.TextOut(x, y, "hello"); // works for printers or displays common (this); // call common code and pass the context } void CMainFrame::OnMove() { } void CMainFrame::common (CWnd * wptr) { // have the window ptr (wptr), but not always using it }

  15. Window, Dialog & Control classes • Frame Window • CFrameWnd, CMenu • View – • Represent client area of a frame • Show data, accept input • Cview, CScrollView, form & record, control • Dialog box (modal, modeless) • Cdialog, CDataExchange • Control – • Attached to CFrameWnd or CMiniFrameWnd • Static, text, number, button, list, toolbar, misc • Control bar

  16. CFrameWnd • Framework for a single-document interface • Usage • Derive a class using CFrameWnd::Create • Add member variables for your data • Implement message handlers • Define a message map • Specifies actions when the window gets a msg

  17. Dialog boxes • Modal • Requires user interaction • Blocks program operation until action completed • Mode errors can be caused by: • Caps-lock or Insert key • Focus stealing (user types but nothing happens) • Modeless • No interaction required (e.g.; toolbar) • Note: this happens a lot in Unix/Linux editor, "vi", because mode indicator can be "off".

  18. Views • Represent the client area of a frame window • Associated with document class & CFrameWnd • 2 types of document views • Cview- base class for app-specific views • CScrollView- base class for scrollable data • CFormView, CRecordView, CHtmlEditView • Control views • CCrtlView, CEditView, CCListView, CTreeView

  19. GetWindowRect • Win32API version BOOL WINAPI GetWindowRect ( HWND hWnd,  LPRECT lpRect ); note: TWO parameters, lpRect is a struct • MFC version: voidGetWindowRect ( LPRECT lpRect  ) const; note: one parameter, lpRect is a class (a Crect object)

  20. Document/View Architecture • Display of data should be independent of the data • May need to have different ways to look at the data • CDocument class holds the data • CView manages display and UI • 2 kinds of apps: • Single Document Interface • Multiple Document Interface

  21. View • Display of the data & a way to display it • Frame window: contains a view of the data • Multiple views possible • But only ONE doc per View

  22. tips • SDI application • One frame window derived from class CFrameWnd. • This window is both: • main frame window • and document frame window. • MDI application, the main frame window is derived from class CMDIFrameWnd, and the document frame windows, which are MDI child windows, are derived from class CMDIChildWnd.

More Related