// PrintXDlg.cpp : 实现文件 // #include "stdafx.h" #include "PrintTest.h" #include "PrintXDlg.h" #include ".\printxdlg.h" #include #include "PrintFrameWnd.h" #include "PrintView.h" #include "MyPreviewView.h" #ifdef _DEBUG #define new DEBUG_NEW #endif CPrintXDlg::CPrintXDlg(UINT nIDD, CWnd* pParent /*=NULL*/) : CDialog(nIDD, pParent) { } void CPrintXDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CPrintXDlg, CDialog) END_MESSAGE_MAP() // CPrintXDlg 消息处理程序 BOOL CPrintXDlg::OnInitDialog() { CDialog::OnInitDialog(); m_pTemplate = new CSingleDocTemplate( IDR_MENU1, NULL, RUNTIME_CLASS(CPrintFrameWnd), RUNTIME_CLASS(CPrintView)); return TRUE; // 除非设置了控件的焦点,否则返回 TRUE } void CPrintXDlg::OnBnClickedPreview() { // TODO: 在此添加控件通知处理程序代码 CSingleDocTemplate* pDocTemplate = m_pTemplate; CFrameWnd *pFrameWnd = (CFrameWnd*)pDocTemplate->CreateNewFrame(NULL, NULL); m_pPrintFrameWnd = pFrameWnd; if(pFrameWnd != NULL) { pDocTemplate->InitialUpdateFrame(m_pPrintFrameWnd, NULL, FALSE); m_pPrintFrameWnd->SetWindowText(_T("打印预览")); CPrintView *m_pView = (CPrintView*)m_pPrintFrameWnd->GetActiveView(); pFrameWnd->ShowWindow(SW_SHOWMAXIMIZED); m_pView->SetPrintWnd(this); m_pView->DoPrintPreviewX(); } } void CPrintXDlg::OnBnClickedPrint() { // TODO: 在此添加控件通知处理程序代码 CPrintView view; view.SetPrintWnd(this); view.DoPrintFileX(); /*CSingleDocTemplate* pDocTemplate = m_pTemplate; CFrameWnd *pFrameWnd = (CFrameWnd*)pDocTemplate->CreateNewFrame(NULL, NULL); m_pPrintFrameWnd = pFrameWnd; if(pFrameWnd != NULL) { pDocTemplate->InitialUpdateFrame(m_pPrintFrameWnd, NULL, FALSE); m_pPrintFrameWnd->SetWindowText(_T("打印预览")); CPrintView *m_pView = (CPrintView*)m_pPrintFrameWnd->GetActiveView(); m_pView->SetPrintWnd(this); m_pView->DoPrintFileX(); m_pPrintFrameWnd->ShowWindow(SW_SHOW); }*/ } void CPrintXDlg::SetPageMargin(CDC *pDC, CPrintInfo *pInfo, int nLeft, int nTop, int nRight, int nBottom) { int nOldMode = pDC->GetMapMode(); pDC->SetMapMode(MM_LOMETRIC); // 计算一个设备单位等于多少0.1mm double scaleX = 254.0 / (double)GetDeviceCaps(pDC->m_hAttribDC,LOGPIXELSX); double scaleY = 254.0 / (double)GetDeviceCaps(pDC->m_hAttribDC, LOGPIXELSY); //每英寸有多少个像素 int nx = GetDeviceCaps(pDC->m_hAttribDC, PHYSICALOFFSETX); int ny = GetDeviceCaps(pDC->m_hAttribDC, PHYSICALOFFSETY); int nwidth = GetDeviceCaps(pDC->m_hAttribDC, PHYSICALWIDTH); int nheight = GetDeviceCaps(pDC->m_hAttribDC, PHYSICALHEIGHT); // 物理边距(单位:0.1mm) int nPageWidth = (int)((double)nwidth*scaleX + 0.5); int nPageHeight = (int)((double)nheight*scaleY + 0.5); int m_nPhyLeft = (int)((double)nx*scaleX + 0.5); int m_nPhyTop = (int)((double)ny*scaleY + 0.5); pDC->DPtoLP(&pInfo->m_rectDraw); CRect rcTemp = pInfo->m_rectDraw; rcTemp.NormalizeRect(); int m_nPhyRight = nPageWidth - rcTemp.Width() - m_nPhyLeft; // 物理右边距,单位0.1mm int m_nPhyBottom = nPageHeight - rcTemp.Height() - m_nPhyTop; // 物理下边距,单位0.1mm // 若边距小于物理边距,则调整它们 if (nLeft < m_nPhyLeft) nLeft = m_nPhyLeft; if (nTop < m_nPhyTop) nTop = m_nPhyTop; if (nRight < m_nPhyRight) nRight = m_nPhyRight; if (nBottom < m_nPhyBottom) nBottom = m_nPhyBottom; // 计算并调整pInfo->m_rectDraw的大小 pInfo->m_rectDraw.left = nLeft - m_nPhyLeft; pInfo->m_rectDraw.top = -nTop + m_nPhyTop; pInfo->m_rectDraw.right -= nRight - m_nPhyRight; pInfo->m_rectDraw.bottom += nBottom - m_nPhyBottom; pDC->LPtoDP(&pInfo->m_rectDraw); pDC->SetMapMode(nOldMode); }