Microsoft Foundation Classes

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

MFC

Microsoft Foundation Classes


Windows API
• Comes as part of every copy of windows
• A large Set of Functions (over a thousand)
• Provide all necessary services and communications
MFC
• Set of predefined Classes
• An OO approach to Windows programming
• Encapsulates the Windows API
• The Process of Windows Programming Involves
– Creating and using MFC Objects
– Objects of classes derived from MFC
Document/View Concept

What is Document?
Collection of Data in your Application

Your Document class is derived from MFC class CDocument

You’ll add data members here to store items and member functions
to support processing

Why?

Standard Mechanism provided within MFC

You’ll get a broad range of built in functionality

Speed

Ease of Implementation

Etc
What is View?
• An object which provides a mechanism to display some or all of the
data stored in
document
• A view always relates to a particular document object

• How the data is to be displayed in a window

• How the user interact with it

• A document can have multiple view objects associated with it

• MFC incorporates a mechanism for integrating a Document with its


View.
private:
CArray <CPoint , CPoint&> myArray;
int index;
// Operations
public:

// Overrides
// ClassWizard generated virtual function
overrides
//{{AFX_VIRTUAL(CDrawingDoc)
public:
virtual BOOL OnNewDocument();
virtual void Serialize(CArchive& ar);
//}}AFX_VIRTUAL

void SetValue(CPoint);
CPoint GetValue(int);
CDrawingDoc::CDrawingDoc()
{
// TODO: add one-time construction code here
myArray.SetSize(10);
index = 0;

CDrawingDoc::SetValue(CPoint pt)
{
myArray[index++] = pt;
}

CPoint CDrawingDoc::GetValue(int i)
{
return myArray[i];
}
void CDrawingView::OnDraw(CDC* pDC)
{
CDrawingDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CPoint pt;
CRect rectangle;
pt.x = 50;
pt.y = 50;
pDoc->SetValue(pt);
pt.x = 200;
pt.y = 200;
pDoc->SetValue(pt);
CPen mypen;
if(!mypen.CreatePen(PS_SOLID , 4 , RGB(200 ,0,0)))
{
AfxMessageBox("Pen Creation Error");
AfxAbort();
}
CPen* oldPen = pDC->SelectObject(&mypen);
CBrush myBrush;
myBrush.CreateHatchBrush(HS_DIAGCROSS , RGB(5,5,215));
CBrush* oldBrush = static_cast<CBrush*>(pDC->SelectObject(&myBrush));
//CBrush* oldBrush = static_cast<CBrush*>(pDC->SelectStockObject(NULL_BRUSH));
rectangle = CRect(pDoc->GetValue(0) , pDoc->GetValue(1));
rectangle.NormalizeRect();
pDC->Rectangle(rectangle);
pDC->SelectObject(oldPen);
pDC->SelectObject(oldBrush);
Good Luck

You might also like