Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 9 Strings Visual C++ and MFC Fundamentals


By the way, as you probably know already, you can change the dimensions of the
window with a code similar to the following:

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;

// The new width of the window's frame
cs.cx = 450;
// The new height of the window's frame
cs.cy = 370;
// Remove the Untitled thing
cs.style &= ~FWS_ADDTOTITLE;

return TRUE;
}

Now that we know how to control the origin, we will position it at a fixed point, 320 units
to the right and 220 units down. We can also easily draw the (Cartesian) axes now:

void CExoView::OnDraw(CDC* pDC)
{
CExoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

pDC->SetViewportOrg(220, 150);

// Use a red pen
CPen PenRed(PS_SOLID, 1, RGB(255, 0, 0));
CPen *pOld = pDC->SelectObject(&PenRed);

// A circle whose center is at the origin (0, 0)
pDC->Ellipse(-100, -100, 100, 100);

// Use a blue pen
CPen PenBlue(PS_SOLID, 1, RGB(0, 0, 255));
pOld = pDC->SelectObject(&PenBlue);

// Horizontal axis
pDC->MoveTo(-320, 0);
pDC->LineTo( 320, 0);
// Vertical axis
pDC->MoveTo( 0, -220);
pDC->LineTo( 0, 220);

pDC->SelectObject(pOld);
}
Free download pdf