
Beginning Visual C++ 2005 (2006) [eng]-1
.pdf






|
|
Working with Dialogs and Controls |
|
|
|
|
void CSketcherView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) |
|
|
{ |
|
|
CScrollView::OnPrepareDC(pDC, pInfo); |
|
|
CSketcherDoc* pDoc = GetDocument(); |
|
|
pDC->SetMapMode(MM_ANISOTROPIC); |
// Set the map mode |
|
CSize DocSize = pDoc->GetDocSize(); |
// Get the document size |
|
// y extent must be negative because we want MM_LOENGLISH |
|
|
DocSize.cy = -DocSize.cy; |
// Change sign of y |
|
pDC->SetWindowExt(DocSize); |
// Now set the window extent |
|
// Get the number of pixels per inch in x and y |
|
|
int xLogPixels = pDC->GetDeviceCaps(LOGPIXELSX); |
|
|
int yLogPixels = pDC->GetDeviceCaps(LOGPIXELSY); |
|
|
// Calculate the viewport extent in x and y |
|
|
long xExtent = static_cast<long>(DocSize.cx)*m_Scale*xLogPixels/100L; |
|
|
long yExtent = static_cast <long>(DocSize.cy)*m_Scale*yLogPixels/100L; |
|
|
pDC->SetViewportExt(static_cast<int>(xExtent), |
|
|
static_cast<int>(-yExtent)); // Set viewport extent |
|
|
} |
|
The override of the base class function is unusual here in that you have left the call to CScrollView:: OnPrepareDC() in and added the modifications after it rather than where the comment in the default code suggests. If the class was derived from CView, you would replace the call to the base class version because it does nothing, but in the case of CScrollView this isn’t the case. You need the base class function to set some attributes before you set the mapping mode. Don’t make the mistake of calling the base class function at the end of the override version though — if you do, scaling won’t work.
After setting the mapping mode and obtaining the document extent, you set the window extent with the y extent negative. This is just to be consistent with the MM_LOENGLISH mode that you were using previously — remember that the origin is at the top, so y values in the client area are negative with this mapping mode.
The CDC member function GetDeviceCaps() supplies information about the device that the device context is associated with. You can get various kinds of information about the device, depending on the argument you pass to the function. In this case, the arguments LOGPIXELSX and LOGPIXELSY return the number of pixels per logical inch in the x and y directions. These values are equivalent to 100 units in your logical coordinates.
You use these values to calculate the x and y values for the viewport extent, which you store in the local variables xExtent and yExtent. The document extent along an axis in logical units divided by 100 gives the document extent in inches. If this is multiplied by the number of logical pixels per inch for the device, you get the equivalent number of pixels for the extent. If you then use this value as the viewport extent, you get the elements displayed at a scale of 1 to 1. If you simplify the equations for converting between device and logical coordinates by assuming the window origin and the viewport origin are both (0,0), they become:
xDevice = xLogical * xViewportExt xWindowExt
yDevice = yLogical * yViewportExt |
|
yWindowExt |
845 |
|


