1. 程式人生 > >VC++對話方塊程式列印及列印預覽的實現(二)

VC++對話方塊程式列印及列印預覽的實現(二)

這是接上面的具體實現能夠成功的打印出資料

CPrintView.h的詳細程式碼

pragma once
#include "StatisticsCoverDlg.h"
// CPrintView 檢視

#define PRINTMARGIN 2
BOOL CALLBACK _AfxMyPreviewCloseProc(CFrameWnd* pFrameWnd);
class CPrintView : public CScrollView
{
 DECLARE_DYNCREATE(CPrintView)
public:
 CPrintView();           // 動態建立所使用的受保護的建構函式
 virtual ~CPrintView();
 CCoverageStatisticsDlg*       m_pOldWnd;
public:
#ifdef _DEBUG
 virtual void AssertValid() const;
 virtual void Dump(CDumpContext& dc) const;
#endif

protected:
 virtual void OnDraw(CDC* pDC);      // 重寫以繪製此檢視
 virtual void OnInitialUpdate();     // 構造後的第一次
    virtual void OnPrint(CDC* pDC, CPrintInfo* pInfo);
 virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
    virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
 void PrintPageHeader(CDC* pDC, UINT nPageNumber, CPrintInfo* pInfo);;
 UINT m_cxWidth;
 UINT m_cxOffset;
 UINT m_nLinesPerPage;
 UINT m_cyPrinter;
 CFont m_ListFont;
 CFont m_fontPrinter;
 BOOL m_bPrintData;
 BOOL m_bPrintImage;
 BOOL m_bPrintHead;
 DECLARE_MESSAGE_MAP()
public:
 BOOL OnPreparePrinting(CPrintInfo* pInfo);
 void OnFilePrintPreview( );
};

//

CPrintView.cpp的詳細程式碼

#include "stdafx.h"
#include "RadioPlaning.h"
#include "PrintView.h"
#include "MyPreviewView.h"

// CPrintView

IMPLEMENT_DYNCREATE(CPrintView, CScrollView)
//修改建構函式;將座標射模式置換成預設模式。
CPrintView::CPrintView()
{
 m_nMapMode = MM_TEXT;
}

CPrintView::~CPrintView()
{
}

BEGIN_MESSAGE_MAP(CPrintView, CScrollView)
 //增加訊息對映實現列印
 ON_COMMAND(ID_FILE_PRINT,CView::OnFilePrint)
END_MESSAGE_MAP()


// CPrintView 繪圖

void CPrintView::OnInitialUpdate()
{
 CScrollView::OnInitialUpdate();

 CSize sizeTotal;
 // TODO: 計算此檢視的合計大小
 sizeTotal.cx = sizeTotal.cy = 100;
 SetScrollSizes(MM_TEXT, sizeTotal);
}

void CPrintView::OnDraw(CDC* pDC)
{
 CDocument* pDoc = GetDocument();
 // TODO: 在此新增繪製程式碼
}


// CPrintView 診斷

#ifdef _DEBUG
void CPrintView::AssertValid() const
{
 CScrollView::AssertValid();
}

void CPrintView::Dump(CDumpContext& dc) const
{
 CScrollView::Dump(dc);
}
#endif //_DEBUG


// CPrintView 訊息處理程式
//過載虛擬函式OnPreatePrinting,呼叫DoPreparePrinting生成
//用於列印或列印預覽的裝置描述表
BOOL CPrintView::OnPreparePrinting(CPrintInfo* pInfo)
{
 return DoPreparePrinting(pInfo);
}
void CPrintView::OnFilePrintPreview( )
{

 CPrintPreviewState* pState = new CPrintPreviewState;

 //pState->lpfnCloseProc =_AfxMyPreviewCloseProc;//設定列印預覽視窗關閉時的呼叫函式;

 if(!DoPrintPreview(AFX_IDD_PREVIEW_TOOLBAR, this,RUNTIME_CLASS(CMyPreviewView), pState))
 {
  TRACE0("Error: DoPrintPreview failed.\n");

  AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
  delete pState;
 }

}
//當單機列印預覽視窗關閉按鈕時呼叫
BOOL CALLBACK _AfxMyPreviewCloseProc(CFrameWnd* pFrameWnd)
{
 ASSERT_VALID(pFrameWnd);

 CMyPreviewView* pView = (CMyPreviewView*) pFrameWnd->GetDlgItem(AFX_IDW_PANE_FIRST);

 ASSERT_KINDOF(CPreviewView, pView);

 pView->OnPreviewClose();
 return FALSE;

}
//列印頁首頁尾
void CPrintView::PrintPageHeader(CDC* pDC, UINT nPageNumber, CPrintInfo* pInfo)
{
 CFont *pOldFont;
 CFont fontHeader;
 fontHeader.CreatePointFont(100,"FixedSys",pDC);
 pOldFont=(CFont *)(pDC->SelectObject(&fontHeader));
 int nPosY,nPosX;
 nPosY= 3 * m_cyPrinter;
 nPosX= m_cxOffset;
 pDC->TextOut(nPosX,nPosY,CString(_T("北京聯基動力科技有限公司")));
 CString str;
 str.Format(_T("第 %d 頁 / 共 %d 頁"),nPageNumber,pInfo->GetMaxPage());
 CSize size = pDC->GetTextExtent(str);     //獲取特定的字串在螢幕上所佔的寬度和高度
 nPosX = m_cxOffset+m_cxWidth-size.cx;
 pDC->TextOut(nPosX,nPosY,str);
 pDC->MoveTo(m_cxOffset,4*m_cyPrinter);
 pDC->LineTo(m_cxOffset+m_cxWidth,4*m_cyPrinter);
 pDC->SelectObject(pOldFont);
 fontHeader.DeleteObject();
}
//開始列印設定字型等
void CPrintView::OnBeginPrinting(CDC* pDC,CPrintInfo* pInfo)
{
 CScrollView::OnBeginPrinting(pDC,pInfo);
 //CDC* pDC   = (CDC*)wParam;
 //CPrintInfo* pInfo = (CPrintInfo *)lParam;
 if(m_fontPrinter.m_hObject==NULL)
  m_fontPrinter.CreatePointFont(120,"FixedSys",pDC);
 TEXTMETRIC tm;
 CFont* pOldFont = pDC->SelectObject (&m_fontPrinter);
 pDC->GetTextMetrics (&tm);                                           //獲取當前字型的資訊
 m_cyPrinter = tm.tmHeight + tm.tmExternalLeading;
 CSize size = pDC->GetTextExtent (_T ("---------1---------2---------" \
  "3---------4---------5---------6---------7---------8------"), 81);
 pDC->SelectObject (pOldFont);
 m_cxWidth = size.cx;
 //////////////////////////////////////////////////////////////////////////
 m_nLinesPerPage = (pDC->GetDeviceCaps (VERTRES) -
  (m_cyPrinter * (3 + (2 * PRINTMARGIN)))) / m_cyPrinter;
 //pInfo->SetMaxPage (1);
 pInfo->SetMaxPage(pInfo->m_nCurPage);
 m_cxOffset = (pDC->GetDeviceCaps (HORZRES) - size.cx) / 2;
 return ;
}
//結束列印,釋放字型等
void CPrintView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
 CScrollView::OnEndPrinting(pDC,pInfo);
 if(m_fontPrinter.m_hObject!=NULL)
  m_fontPrinter.DeleteObject();
 return;
}
//列印資料
void CPrintView::OnPrint(CDC* pDC,CPrintInfo* pInfo)
{//Andy
 CScrollView::OnPrint(pDC,pInfo);
 UINT gl_uNumOfPoints = 52;
 //CDC* pDC   = (CDC*)wParam;
 //CPrintInfo* pInfo = (CPrintInfo *)lParam;
 int nPageNumber = pInfo->m_nCurPage;
 int i,j;
 CFont *pOldFont;
 CFont DataFont;
 DataFont.CreatePointFont(120,_T("宋體"),pDC);
 TCHAR *pszTitle[4]={"臺站名稱","門限","面積","人口"};
 pOldFont = pDC->SelectObject(&DataFont);
 pDC->Rectangle(m_cxOffset,(int)((2.5 + PRINTMARGIN)*m_cyPrinter),m_cxOffset+m_cxWidth,(m_nLinesPerPage+3 + PRINTMARGIN)*m_cyPrinter+20);
 for(i=1;i<4;i++)
 {
  pDC->MoveTo(m_cxOffset+i*m_cxWidth/4,(int)((2.5 + PRINTMARGIN)*m_cyPrinter));
  pDC->LineTo(m_cxOffset+i*m_cxWidth/4,(3 +m_nLinesPerPage+PRINTMARGIN)*m_cyPrinter+20);
 }
 pDC->MoveTo(m_cxOffset,(4 + PRINTMARGIN)*m_cyPrinter);
 pDC->LineTo(m_cxOffset+m_cxWidth,(4 + PRINTMARGIN)*m_cyPrinter);
 for(i=0;i<4;i++)
 {
  pDC->TextOut(m_cxOffset+50+i*m_cxWidth/4,(int)((2.8 + PRINTMARGIN)*m_cyPrinter),CString(pszTitle[i]));
 }
 //////////////////////////////////////Andy//////////////////////////////預覽資料
 for(int i = 0; i < g_p2DView->m_StationManage.m_StationEntityArray.GetSize(); i++)
 {
  CStationEntity* pEntity = g_p2DView->m_StationManage.m_StationEntityArray.GetAt(i);
  double nCoverArea = g_p2DView->m_StationManage.GetCoverageArea(pEntity->m_nObjID,g_pMain->m_sysParameters.fThreshold);
  Station station = (g_p2DView->m_StationManage.m_StationEntityArray.GetAt(i))->m_Station;
  CString str[4];
  int nIndex = (nPageNumber-1)*(m_nLinesPerPage-1)+i;
  if(nIndex<(int)gl_uNumOfPoints)
  {
   str[0].Format("%s",station.szName);
   str[1].Format("%.3f",g_pMain->m_sysParameters.fThreshold);
   str[2].Format("%.3f",nCoverArea);
   str[3].Format("%.f",0);
   for(j=0;j<4;j++)
   {
    pDC->TextOut(m_cxOffset+50+j*m_cxWidth/4,(4 + PRINTMARGIN+i)*m_cyPrinter+5,str[j]);
   }
  }
 }
 pDC->SelectObject (pOldFont);
 PrintPageHeader(pDC,pInfo->m_nCurPage,  pInfo);
 return ;
}