microsoft foundation classes

22
Microsoft Foundation Classes

Upload: lee-lewis

Post on 02-Jan-2016

35 views

Category:

Documents


2 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Microsoft  Foundation Classes

Microsoft Foundation Classes

Page 2: 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

Page 3: Microsoft  Foundation Classes

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.

Page 4: Microsoft  Foundation Classes

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

Page 5: Microsoft  Foundation Classes

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)

Page 6: Microsoft  Foundation Classes

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

Page 7: Microsoft  Foundation Classes

WIn32API vs MFC Message handling

Window gets moved

WndProc() case WM_MOVE:

movefn();

Win32API

Window gets moved

CMainFrame::OnMove

MFC

Page 8: Microsoft  Foundation Classes

Parts of a basic MFC Program - 1MyWinApp.h

#include <afxwin.h>class CMyWinApp : public CWinApp{public:virtual BOOL InitInstance();};

Page 9: Microsoft  Foundation Classes

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 eventsCMainFrame(); // class constructor// prototypes for members named in the MESSAGE MAP// The MESSAGE MAP is implemented in MainFrame.cpp//afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);//afx_msg void OnShowWindow(BOOL bShow, UINT nStatus);afx_msg void OnMove(int x, int y);afx_msg void OnPaint(); // msg hndlr for WM_PAINT eventvoid common(CWnd* wptr);DECLARE_MESSAGE_MAP();

}; // note: the "afx_msg" prefix is pre-VS 2005 and is #defined as empty

Page 10: Microsoft  Foundation Classes

Parts of a basic MFC Program – 3

// Main.cpp

#include "MyWinApp.h"CMyWinApp MyApplication;

Page 11: Microsoft  Foundation Classes

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;}

Page 12: Microsoft  Foundation Classes

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 windowBEGIN_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()

Page 13: Microsoft  Foundation Classes

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 - only if overriding the context */);

}

Page 14: Microsoft  Foundation Classes

Parts of a basic MFC Program – 5cvoid 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 displayscommon (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}

Page 15: Microsoft  Foundation Classes

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

Page 16: Microsoft  Foundation Classes

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

Page 17: Microsoft  Foundation Classes

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".

Page 18: Microsoft  Foundation Classes

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

Page 19: Microsoft  Foundation Classes

GetWindowRect

• Win32API versionBOOL WINAPI GetWindowRect ( HWND hWnd,

LPRECT lpRect ); note: TWO parameters, lpRect is a struct

• MFC version:void GetWindowRect ( LPRECT lpRect ) const; note: one parameter, lpRect is a class (a Crect object)

Page 20: Microsoft  Foundation Classes

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

Page 21: Microsoft  Foundation Classes

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

Page 22: Microsoft  Foundation Classes

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.