#include "StdAfx.h" #include "PrintTool.h" #include #include #include "SigmaDoc.h" #include "SigmaView.h" static CItemView * GetItemView(CSigmaView * pView) { if (pView == 0) return 0; if (pView->m_pDoc == 0) return 0; return pView->m_pDoc->GetItemView(); } CPrintTool::CPrintTool(CSigmaDoc * pDoc) :m_pDoc(pDoc) { } void CPrintTool::ViewAll(CDC* pDC) { CItemView * item = m_pDoc->GetItemView(); if (item == 0) return; CRect rt = item->GetClientRect(); //CRect rt = GetPrintArea(pDC); //rt.OffsetRect(-rt.left, -rt.top); CRect8 rect(1e100, -1e100, -1e100, 1e100); if (m_pDoc->GetDraw()->GetCount() > 0) m_pDoc->GetDrawRange(rect); //else // *((CRect8*)&rect) = rt; m_pDoc->GetDC().Extend(rect, rt, EXTEND_MODE_CENTER); //居中显示 //pDoc->GetDC().Extend(rect, rt, EXTEND_MODE_STRECH); //拉伸显示 } CRect CPrintTool::GetPrintArea(CDC* pDC) { CRect rt(0, 0, pDC->GetDeviceCaps(HORZRES), pDC->GetDeviceCaps(VERTRES)); return rt; } bool CPrintTool::GetPartitionNum(int & rowOut, int & colOut, int superpositionWidth) { rowOut = colOut = 0; if (m_pDoc->GetDraw()->GetCount() == 0) { rowOut = colOut = 1; return false; } CItemView* item = GetItemView((CSigmaView *)m_pDoc->GetView()); if (item == 0) { rowOut = colOut = 1; return false; } CRect clientRect = item->GetClientRect(); //打印区域的大小.以打印机点数为单位。 //获得一页可打印区的打印长度(单位:毫米) double sx = clientRect.Width() * m_pDoc->GetDC().UnitX; double sy = clientRect.Height() * m_pDoc->GetDC().UnitY; //获得总图的毫米长度 CRect8 rect(1e100, -1e100, -1e100, 1e100); m_pDoc->GetDrawRange(rect); // CPoint2D pt; pt.x0 = 0; pt.y0 = 0; CPoint2D ptSW = m_pDoc->m_xyDC.GetReal(pt); double dx = ptSW.x0 - rect.left; double dy = ptSW.y0 - rect.bottom; // double mx = m_pDoc->GetDC().GetScreenWidth8(rect.Width()) * m_pDoc->GetDC().UnitX; double my = m_pDoc->GetDC().GetScreenHeight8(rect.Height()) * m_pDoc->GetDC().UnitY; my = fabs(my); if (mx <= ZERO) rowOut = 1; else { while (mx > ZERO) { rowOut++; mx -= sx; if (mx <= ZERO) break; mx += superpositionWidth; } } if (my <= ZERO) colOut = 1; else while (my > ZERO) { colOut++; my -= sy; if (my <= ZERO) break; my += superpositionWidth; } return true; } bool CPrintTool::Part(PrintPartition * partition) { CSigmaView * pView = (CSigmaView *)m_pDoc->GetView(); CItemView* item = GetItemView(pView); if (item == 0) return false; CDC * pDC = pView->GetDc(); if (pDC == 0) return false; CRect clientRect = item->GetClientRect(); //打印区域的大小.以打印机点数为单位。 if (partition->currentPage == 1) { pDC->SetViewportOrg(clientRect.left, clientRect.top); } else if (partition->currentPage > 1) { int pagew = clientRect.Width(); int pageh = clientRect.Height(); int x, y; switch (partition->kind) { case 1://全部 { //由当前页计算出行号和列号 //行号、列号、页号,起始索引都是1 //求整行数r= partition->currentPage / partition->row int row = partition->currentPage / partition->row; row += (partition->currentPage % partition->row > 0); //不是整行 总是少一行.则加一行。是整行则不用加. //下面句求列号的代码,分成两种情况看。就看懂了。 //1.当currentPage是整行。2.当currentPage不是整行时。写的很好。 int col = (partition->currentPage - 1) % partition->row + 1; //TRACE("OnPrepareDC( row=%d, col=%d)\n", row,col); x = -(pagew - partition->supperPosition)*(col - 1); y = -(pageh - partition->supperPosition)*(row - 1); } break; case 2://纵向 x = -(pagew - partition->supperPosition)*(partition->currentPage - 1); y = 0; break; case 3://横向 x = 0; y = -(pageh - partition->supperPosition)*(partition->currentPage - 1); break; } x += clientRect.left; y += clientRect.top; //pDC->SetViewportOrg(x/2, y); //pView->SetOriginPointForPrint(1000, y); } return 0; }