Visual MFC C++ Example:

 

// HelloMFCView.cpp : implementation of the CHelloMFCView class

//

 

#include "stdafx.h"

#include "HelloMFC.h"

 

#include "HelloMFCDoc.h"

#include "HelloMFCView.h"

#include "Dialog1.h"

#include "Dialog2.h"

 

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

 

/////////////////////////////////////////////////////////////////////////////

// CHelloMFCView

 

IMPLEMENT_DYNCREATE(CHelloMFCView, CView)

 

BEGIN_MESSAGE_MAP(CHelloMFCView, CView)

//{{AFX_MSG_MAP(CHelloMFCView)

ON_COMMAND(ID_COLORS_BORDER_BLUE, OnColorsBorderBlue)

ON_COMMAND(ID_COLORS_BORDER_GREEN, OnColorsBorderGreen)

ON_COMMAND(ID_COLORS_BORDER_RED, OnColorsBorderRed)

ON_COMMAND(ID_COLORS_TEXT_BLUE, OnColorsTextBlue)

ON_COMMAND(ID_COLORS_TEXT_GREEN, OnColorsTextGreen)

ON_COMMAND(ID_COLORS_TEXT_RED, OnColorsTextRed)

ON_COMMAND(ID_COLORS_FILL_BLUE, OnColorsFillBlue)

ON_COMMAND(ID_COLORS_FILL_GREEN, OnColorsFillGreen)

ON_COMMAND(ID_COLORS_FILL_RED, OnColorsFillRed)

ON_COMMAND(ID_SETTEXT, OnSettext)

ON_COMMAND(ID_SLIDER, OnSlider)

//}}AFX_MSG_MAP

// Standard printing commands

ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)

ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)

ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)

END_MESSAGE_MAP()

 

/////////////////////////////////////////////////////////////////////////////

// CHelloMFCView construction/destruction

 

CHelloMFCView::CHelloMFCView()

{

 

m_TextColor = RGB(255, 0, 0);

m_BorderColor = RGB(0, 255, 0);

m_FillColor = RGB(0, 0, 255);

 

// additions of handout 12

m_HelloString = "Hello, world!";

 

 

}

 

CHelloMFCView::~CHelloMFCView()

{

}

 

BOOL CHelloMFCView::PreCreateWindow(CREATESTRUCT& cs)

{

// TODO: Modify the Window class or styles here by modifying

// the CREATESTRUCT cs

 

return CView::PreCreateWindow(cs);

}

 

/////////////////////////////////////////////////////////////////////////////

// CHelloMFCView drawing

 

void CHelloMFCView::OnDraw(CDC* pDC)

{

// Set the text color to red.

COLORREF oldTextColor =

pDC->SetTextColor(m_TextColor);

 

// Print "Hello, world!" 10 times.

for (int i=0; i < 200; i+=20)

pDC->TextOut(i+20, i, m_HelloString);

 

// Create and select a green pen.

CPen greenPen(PS_SOLID, 3, m_BorderColor);

CPen *oldPen = pDC->SelectObject(&greenPen);

 

// Create and select a blue paintbrush.

CBrush blueBrush(m_FillColor);

CBrush *oldBrush = pDC->SelectObject(&blueBrush);

 

// An array containing points of a triangle.

CPoint triangle[3];

triangle[0] = CPoint(20, 20);

triangle[1] = CPoint(20, 200);

triangle[2] = CPoint(200, 200);

 

// Print a triangle under the text.

pDC->Polygon(triangle, 3);

 

// Reset the original brush and

// text colors.

pDC->SelectObject(oldBrush);

pDC->SelectObject(oldPen);

pDC->SetTextColor(oldTextColor);

 

}

 

/////////////////////////////////////////////////////////////////////////////

// CHelloMFCView printing

 

BOOL CHelloMFCView::OnPreparePrinting(CPrintInfo* pInfo)

{

// default preparation

return DoPreparePrinting(pInfo);

}

 

void CHelloMFCView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

{

// TODO: add extra initialization before printing

}

 

void CHelloMFCView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

{

// TODO: add cleanup after printing

}

 

/////////////////////////////////////////////////////////////////////////////

// CHelloMFCView diagnostics

 

#ifdef _DEBUG

void CHelloMFCView::AssertValid() const

{

CView::AssertValid();

}

 

void CHelloMFCView::Dump(CDumpContext& dc) const

{

CView::Dump(dc);

}

 

CHelloMFCDoc* CHelloMFCView::GetDocument() // non-debug version is inline

{

ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHelloMFCDoc)));

return (CHelloMFCDoc*)m_pDocument;

}

#endif //_DEBUG

 

/////////////////////////////////////////////////////////////////////////////

// CHelloMFCView message handlers

 

void CHelloMFCView::OnColorsBorderBlue()

{

m_BorderColor = RGB(0, 0, 255);

Invalidate();

}

 

void CHelloMFCView::OnColorsBorderGreen()

{

m_BorderColor = RGB(0, 255, 0);

Invalidate();

}

 

void CHelloMFCView::OnColorsBorderRed()

{

m_BorderColor = RGB(255, 0, 0);

Invalidate();

}

 

void CHelloMFCView::OnColorsTextBlue()

{

m_TextColor = RGB(0, 0, 255);

Invalidate();

}

 

void CHelloMFCView::OnColorsTextGreen()

{

m_TextColor = RGB(0, 255, 0);

Invalidate();

}

 

void CHelloMFCView::OnColorsTextRed()

{

m_TextColor = RGB(255, 0, 0);

Invalidate();

}

 

void CHelloMFCView::OnColorsFillBlue()

{

m_FillColor = RGB(0, 0, 255);

Invalidate();

}

 

void CHelloMFCView::OnColorsFillGreen()

{

m_FillColor = RGB(0, 255, 0);

Invalidate();

}

 

void CHelloMFCView::OnColorsFillRed()

{

m_FillColor = RGB(255, 0, 0);

Invalidate();

}

 

void CHelloMFCView::OnSettext()

{

CDialog1 Dialog;

Dialog.m_Edit1 = m_HelloString;

if (Dialog.DoModal() == IDOK) {

m_HelloString = Dialog.m_Edit1;

Invalidate();

}

}

 

void CHelloMFCView::OnSlider()

{

CDialog2 Dialog;

if (Dialog.DoModal() == IDOK) {

Invalidate();

}

}