17.6.5 准备设备上下文
考虑到缩放比例,这时Sketcher程序将调用视图对象的OnPrepareDC()函数,把映射模式设置为MM_ANISOTROPIC。就打印而言,要正确地准备设备上下文,必须进行其他一些修改:
- 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
- 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
- int scale(pDC->IsPrinting() 1 : m_Scale); // If we are printing, use scale 1
- int xExtent = (DocSize.cx*scale*xLogPixels)/100;
- int yExtent = (DocSize.cy*scale*yLogPixels)/100;
- pDC->SetViewportExt(xExtent, yExtent); // Set viewport extent
- }
对于到打印机以及屏幕的输出,框架将调用这个函数。在进行打印时,应当确保使用比例1来设置从逻辑坐标到设备坐标的映射。如果使一切保持原样,那么输出将采用当前视图比例,但是在计算需要多少页以及如何设置每一页的原点时,必须考虑比例的问题。
通过调用当前CDC对象的IsPrinting()成员函数,可以确定是否具有打印机设备上下文,如果正在打印,该函数将返回TRUE。在具有打印机设备上下文时,只需要把比例设置为1。当然,必须修改计算视区大小的语句,使它们使用视图的局部变量scale,而不是成员变量m_Scale。