PrintView.cpp
上传用户:dztljd
上传日期:2007-01-04
资源大小:55k
文件大小:4k
源码类别:

打印编程

开发平台:

Visual C++

  1. // PrintView.cpp : implementation of the CPrintView class
  2. //
  3. #include "stdafx.h"
  4. #include "Print.h"
  5. #include "PrintDoc.h"
  6. #include "PrintView.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CPrintView
  14. IMPLEMENT_DYNCREATE(CPrintView, CFormView)
  15. BEGIN_MESSAGE_MAP(CPrintView, CFormView)
  16. //{{AFX_MSG_MAP(CPrintView)
  17. // NOTE - the ClassWizard will add and remove mapping macros here.
  18. //    DO NOT EDIT what you see in these blocks of generated code!
  19. //}}AFX_MSG_MAP
  20. // Standard printing commands
  21. ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
  22. ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
  23. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
  24. END_MESSAGE_MAP()
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CPrintView construction/destruction
  27. CPrintView::CPrintView()
  28. : CFormView(CPrintView::IDD)
  29. {
  30. m_pMemDC = new CDC ;
  31. m_pBm = new CBitmap;
  32. //{{AFX_DATA_INIT(CPrintView)
  33. // NOTE: the ClassWizard will add member initialization here
  34. //}}AFX_DATA_INIT
  35. // TODO: add construction code here
  36. }
  37. CPrintView::~CPrintView()
  38. {
  39. delete m_pMemDC; //CLEAN UP OUR VARIABLES
  40. delete m_pBm;
  41. }
  42. void CPrintView::DoDataExchange(CDataExchange* pDX)
  43. {
  44. CFormView::DoDataExchange(pDX);
  45. //{{AFX_DATA_MAP(CPrintView)
  46. // NOTE: the ClassWizard will add DDX and DDV calls here
  47. //}}AFX_DATA_MAP
  48. }
  49. BOOL CPrintView::PreCreateWindow(CREATESTRUCT& cs)
  50. {
  51. // TODO: Modify the Window class or styles here by modifying
  52. //  the CREATESTRUCT cs
  53. return CFormView::PreCreateWindow(cs);
  54. }
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CPrintView printing
  57. BOOL CPrintView::OnPreparePrinting(CPrintInfo* pInfo)
  58. {
  59. return DoPreparePrinting(pInfo);
  60. }
  61. void CPrintView::OnBeginPrinting(CDC* pDC, CPrintInfo* /*pInfo*/)
  62. {
  63. // TODO: add extra initialization before printing
  64. if (m_pMemDC->GetSafeHdc()) m_pMemDC->DeleteDC();
  65. m_pMemDC->CreateCompatibleDC(pDC);
  66. CClientDC dc(this);
  67. CRect rect;
  68. GetClientRect(rect);
  69. m_pMemDC->SetMapMode(MM_ANISOTROPIC);
  70. m_pMemDC->SetWindowExt(dc.GetDeviceCaps(LOGPIXELSX),dc.GetDeviceCaps(LOGPIXELSY));
  71. m_pMemDC->SetViewportExt(m_pMemDC->GetDeviceCaps(LOGPIXELSX),m_pMemDC->GetDeviceCaps(LOGPIXELSY));
  72. if (m_pBm->GetSafeHandle()) m_pBm->DeleteObject();
  73. m_pBm->CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
  74. m_pMemDC->SelectObject(m_pBm);
  75. dc.DPtoLP(rect); //Convert to Logical Coordinates
  76. m_rect = rect; //Save Logical Coordinates
  77. m_pMemDC->BitBlt(0,0,rect.Width(),rect.Height(),&dc,0,0,SRCCOPY);
  78. //This will prevent creation of the Memory device context 
  79. //and our Bitmap object a second time.
  80. }
  81. void CPrintView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  82. {
  83. // TODO: add cleanup after printing
  84. }
  85. void CPrintView::OnPrint(CDC* pDC, CPrintInfo*)
  86. {
  87. // TODO: add code to print the controls
  88. //The Following code scales the image based on printer resolution.
  89. pDC->SetMapMode(MM_ANISOTROPIC);
  90. pDC->SetWindowExt(m_pMemDC->GetDeviceCaps(LOGPIXELSX),m_pMemDC->GetDeviceCaps(LOGPIXELSY));
  91. pDC->SetViewportExt(pDC->GetDeviceCaps(LOGPIXELSX),pDC->GetDeviceCaps(LOGPIXELSY));
  92. pDC->StretchBlt(0,0,m_rect.Width(),m_rect.Height(),m_pMemDC,0,0,m_rect.Width(),m_rect.Height(),SRCCOPY);
  93. }
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CPrintView diagnostics
  96. #ifdef _DEBUG
  97. void CPrintView::AssertValid() const
  98. {
  99. CFormView::AssertValid();
  100. }
  101. void CPrintView::Dump(CDumpContext& dc) const
  102. {
  103. CFormView::Dump(dc);
  104. }
  105. CPrintDoc* CPrintView::GetDocument() // non-debug version is inline
  106. {
  107. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPrintDoc)));
  108. return (CPrintDoc*)m_pDocument;
  109. }
  110. #endif //_DEBUG
  111. /////////////////////////////////////////////////////////////////////////////
  112. // CPrintView message handlers