Visual C++ and MFC Fundamentals Chapter 9: Strings
This default origin is fine for most, if not all regular, operations performed on graphics
applications. Sometimes, you will need to control the position of the origin of the
coordinate system. For example, most CAD applications, including AutoCAD, allow the
user to set this origin.
The MFC library provides various functions to deal with the coordinates positions and
extents of the drawing area, including functions used to set the origin of the coordinate
system anywhere you want on the screen. Since you are drawing on a device context, all
you need to do is simply call the CDC::SetViewportOrg() method. It is overloaded with
two versions, which allow you to use either the X and the Y coordinates or a defined
point. The syntaxes of this method are:
SetViewportOrg(int X, int Y);
SetViewportOrg(CPoint Pt);
When calling this member function, simply specify where you want the new origin to be.
If using the second version, the argument can be a Win32 POINT structure or an MFC
CPoint class. To see the effect of this function, we will move the origin 200 units in the
positive direction of the X axis and 150 units in the positive direction of the vertical axis
without changing the circle and the line. Our OnDraw() method would look like this:
void CExoView::OnDraw(CDC* pDC)
{
CExoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDC->SetViewportOrg(200, 150);
// A circle whose center is at the origin (0, 0)
pDC->Ellipse(-50, -50, 50, 50);
// A line that starts at (0, 0) and ends at (100, 100)
pDC->MoveTo(0, 0);
pDC->LineTo(100, 100);
}