Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 15: Fundamental Controls


void CQuadrilateral::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect Recto;

// Create a light green brush
CBrush BrushLightGreen(RGB(212, 235, 235));
// Create a navy pen
CPen PenNavy(PS_SOLID, 1, RGB(0, 0, 128));
// Create a green brush
CBrush BrushGreen(RGB(100, 175, 180));
// Create a dark green pen
CPen PenGreen(PS_SOLID, 2, RGB(0, 115, 115));

// Get the location and dimensions of the client rectangle
GetClientRect(&Recto);

// Select the light green brush
CBrush *pOldBrush = dc.SelectObject(&BrushLightGreen);
// Select the navy pen
CPen *pOldPen = dc.SelectObject(&PenNavy);

// Draw a rectangular shape on the left side of the property page
dc.Rectangle(0, 0, 162, Recto.Height());

// Select the green brush
pOldBrush = dc.SelectObject(&BrushGreen);
// Select the dark green pen
pOldPen = dc.SelectObject(&PenGreen);

// Draw the square
dc.Rectangle(40, 40, 120, 100);
// Draw the rectangle
dc.Rectangle(20, 170, 140, 240);

// Set the back mode to transparent for the text
dc.SetBkMode(TRANSPARENT);
// Display indicative labels
dc.TextOut(60, 105, "Square");
dc.TextOut(45, 250, "Rectangle");

// Restore the old GDI tools
dc.SelectObject(pOldPen);
dc.SelectObject(pOldBrush);

if (IsIconic())
{

SendMessage(WM_ICONERASEBKGND,
reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
Free download pdf