📄 feeviewprint.cpp
字号:
#include "stdafx.h"
#include "fee.h"
#include "feeDoc.h"
#include "feeView.h"
#include "printsetfontDlg.h"
#include "printsetmaginDlg.h"
BOOL CFeeView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
//
CString strSql;
CString strupmagin,strdownmagin,strleftmagin,strrightmagin;
CString strheadfont,strheadtext,strmidfont,strfootfont,strfoottext;
CString strheadsize,strmidsize,strfootsize;
char flag;
HRESULT result;
flag=0;
strSql.Format("select * from print");
CFeeApp* pApp=(CFeeApp *)AfxGetApp();
result=pApp->m_pjifeiRecordset->Open(strSql.AllocSysString(),
pApp->m_pjifeiConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText);
if (!SUCCEEDED(result))
{
AfxMessageBox("can't open telephone table");
}
if (!pApp->m_pjifeiRecordset->adoEOF)
{
strheadfont=(char*)(_bstr_t)pApp->m_pjifeiRecordset->GetCollect(_variant_t("headfont"));
strheadsize=(char*)(_bstr_t)pApp->m_pjifeiRecordset->GetCollect(_variant_t("headsize"));
strheadtext=(char*)(_bstr_t)pApp->m_pjifeiRecordset->GetCollect(_variant_t("headtext"));
strmidfont=(char*)(_bstr_t)pApp->m_pjifeiRecordset->GetCollect(_variant_t("midfont"));
strmidsize=(char*)(_bstr_t)pApp->m_pjifeiRecordset->GetCollect(_variant_t("midsize"));
strfootfont=(char*)(_bstr_t)pApp->m_pjifeiRecordset->GetCollect(_variant_t("footfont"));
strfootsize=(char*)(_bstr_t)pApp->m_pjifeiRecordset->GetCollect(_variant_t("footsize"));
strfoottext=(char*)(_bstr_t)pApp->m_pjifeiRecordset->GetCollect(_variant_t("foottext"));
strupmagin=(char*)(_bstr_t)pApp->m_pjifeiRecordset->GetCollect(_variant_t("upmagin"));
strdownmagin=(char*)(_bstr_t)pApp->m_pjifeiRecordset->GetCollect(_variant_t("downmagin"));
strleftmagin=(char*)(_bstr_t)pApp->m_pjifeiRecordset->GetCollect(_variant_t("leftmagin"));
strrightmagin=(char*)(_bstr_t)pApp->m_pjifeiRecordset->GetCollect(_variant_t("rightmagin"));
flag=1;
}
pApp->m_pjifeiRecordset->Close();
if (flag==1)
{
rcMarginMM=CRect(atol(strleftmagin),atol(strupmagin),\
atol(strupmagin),atol(strrightmagin));
csFtHead=strheadfont;
csFtFoot=strfootfont;
csFtPrint=strmidfont;
iFtHead =atol(strheadsize);
iFtFoot =atol(strfootsize);
iFtPrint=atol(strmidsize);
csPageHead=strheadtext;
csPageFooter=strfoottext;
}
else
{
//
rcMarginMM = CRect(10,10,10,10);
csFtHead = "Arial";
csFtFoot = "Times New Roman";
csFtPrint = "Arial";
iFtHead = 12;
iFtFoot = 6;
iFtPrint = 8;
}
pDx = NULL;
iItemXPage = 1;
iNumCol = 0;
pColTitle = NULL;
iTotItem = 0;
pFormat = NULL;
szFtPrint = szFtHead = szFtFoot = CSize(0,0);
szFoot = CSize(0,0);
CListCtrl *pList;
pList=&GetListCtrl();
pInfo->SetMinPage(1);
if (!pList) return false;
iTotItem = pList->GetItemCount();
if (iTotItem <= 0) return false;
//ASSERT(pDx == NULL);
CHeaderCtrl* pHeader = (CHeaderCtrl*)(pList->GetDlgItem(0));
ASSERT(pHeader);
iNumCol = pHeader->GetItemCount();
if (iNumCol <= 0) return false;
pDx = new int[iNumCol+1];
pFormat = new int[iNumCol];
pColTitle = new char *[iNumCol];
ZeroMemory(pColTitle, sizeof(char *) * iNumCol);
ZeroMemory(pDx, sizeof(int) * (iNumCol + 1));
ZeroMemory(pFormat, sizeof(int) * iNumCol);
char txt[128];
for (int t = 0; t < iNumCol; t++)
{
HDITEM hi;
hi.cchTextMax = 128;
hi.pszText = txt;
hi.mask = HDI_TEXT|HDI_WIDTH|HDI_FORMAT;
if (pHeader->GetItem(t, &hi))
{
pDx[t+1] = hi.cxy;
pFormat[t] = hi.fmt;
if (hi.cchTextMax > 0 && hi.pszText)
{
pColTitle[t] = new char[hi.cchTextMax];
ASSERT(pColTitle[t]);
lstrcpy(pColTitle[t], hi.pszText);
}
}
}
return DoPreparePrinting(pInfo);
}
bool CFeeView::DoPreparePrinting(CPrintInfo * pInfo)
{
ASSERT(pInfo != NULL);
ASSERT(pInfo->m_pPD != NULL);
if (pInfo->m_pPD->m_pd.nMinPage > pInfo->m_pPD->m_pd.nMaxPage)
pInfo->m_pPD->m_pd.nMaxPage = pInfo->m_pPD->m_pd.nMinPage;
pInfo->m_pPD->m_pd.nFromPage = (WORD)pInfo->GetMinPage();
pInfo->m_pPD->m_pd.nToPage = (WORD)pInfo->GetMaxPage();
if (AfxGetApp()->DoPrintDialog(pInfo->m_pPD) != IDOK) return FALSE;
ASSERT(pInfo->m_pPD != NULL);
ASSERT(pInfo->m_pPD->m_pd.hDC != NULL);
if (pInfo->m_pPD->m_pd.hDC == NULL) return FALSE;
pInfo->m_nNumPreviewPages = AfxGetApp()->m_nNumPreviewPages;
VERIFY(pInfo->m_strPageDesc.LoadString(AFX_IDS_PREVIEWPAGEDESC));
return TRUE;
}
void CFeeView::OnBeginPrinting(CDC * pDC, CPrintInfo * pInfo)
{
// TODO: add extra initialization before printing
ftPrint.CreatePointFont(iFtPrint*10, csFtPrint, pDC);
ftHead.CreatePointFont(iFtHead*10, csFtHead, pDC);
ftFoot.CreatePointFont(iFtFoot*10, csFtFoot, pDC);;
CString cs = "X";
CFont * of = pDC->SelectObject(&ftPrint);
szFtPrint = pDC->GetTextExtent(cs);
pDC->SelectObject(&ftHead);
szFtHead = pDC->GetTextExtent(cs);
pDC->SelectObject(&ftFoot);
szFtFoot = pDC->GetTextExtent(cs);
pDC->SelectObject(of);
szFtPrint.cy += 4;
szFtHead.cy += 4;
for (int t = 1; t <= iNumCol; t++) pDx[t] += pDx[t-1];
CRect rcPrint(pInfo->m_rectDraw);
SubtractMargin(pDC, rcPrint);
double db = rcPrint.Width();
db /= (double) pDx[iNumCol];
for (t = 0; t <= iNumCol; t++) pDx[t] = (int)double(pDx[t] * db);
//ASSERT(pList);
PrintListHead(pDC, rcPrint, true);
PrintPageHead(pDC, rcPrint, 0, 0, true);
CSize sz1 = PrintListFoot(pDC, rcPrint, true);
CSize sz2 = PrintPageFoot(pDC, rcPrint, true);
szFoot.cy = sz1.cy+sz2.cy + 2;
iItemXPage = rcPrint.Height() / (szFtPrint.cy + 1) -1;
int nPages = iTotItem / iItemXPage + 1;
pInfo->SetMaxPage(nPages);
}
void CFeeView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: add cleanup after printing
}
void
CFeeView::SubtractMargin(CDC * pDC, CRect &rc)
{
CSize upsz(rcMarginMM.left*100, rcMarginMM.top*100);
CSize dwsz(rcMarginMM.right*100, rcMarginMM.bottom*100);
pDC->HIMETRICtoDP(&upsz);
pDC->HIMETRICtoDP(&dwsz);
rc.top += upsz.cy;
rc.left += upsz.cx;
rc.right -= dwsz.cx;
rc.bottom -= dwsz.cy;
}
CSize
CFeeView::PrintPageHead(CDC * pDC, CRect & rcPage, const int nPage, const int iMaxPage, bool bCalc)
{
CFont * oft = pDC->SelectObject(&ftHead);
int y = rcPage.top;
int x = rcPage.left;
CSize szPg(0,0);
/*
if (csPage != "")
{
CString cs;
cs.Format(csPage, nPage, iMaxPage);
CRect rc1(x,y,rcPage.right,y);
pDC->DrawText(cs, rc1, DT_WORDBREAK|DT_CALCRECT|DT_RIGHT);
rc1.left = rcPage.right - rc1.Width();
rc1.right = rcPage.right;
if (!bCalc) pDC->DrawText(cs, rc1, DT_WORDBREAK|DT_RIGHT);
szPg = rc1.Size();
}
*/
if (csPageHead != "")
{
CRect rc1(x,y,rcPage.right - szPg.cy,y);
pDC->DrawText(csPageHead, rc1, DT_WORDBREAK|DT_CALCRECT|DT_LEFT);
if (!bCalc) pDC->DrawText(csPageHead, rc1, DT_WORDBREAK|DT_LEFT);
if (rc1.Height() > szPg.cy) szPg.cy = rc1.Height();
}
rcPage.top += szPg.cy;
pDC->SelectObject(oft);
return szPg;
}
CSize
CFeeView::PrintPageFoot(CDC * pDC, CRect & rcPage, bool bCalc)
{
CFont * oft = pDC->SelectObject(&ftFoot);
int y = rcPage.top;
int x = rcPage.left;
CSize szPg(0,0);
if (csPageFooter != "")
{
CRect rc1(x,y,rcPage.right - szPg.cy,y);
pDC->DrawText(csPageFooter, rc1, DT_WORDBREAK|DT_CALCRECT|DT_LEFT);
if (!bCalc) pDC->DrawText(csPageFooter, rc1, DT_WORDBREAK|DT_LEFT);
szPg.cy = rc1.Height();
}
rcPage.bottom -= szPg.cy;
pDC->SelectObject(oft);
return szPg;
}
CSize
CFeeView::PrintListHead(CDC * pDC, CRect & rcPage, bool bCalc)
{
if (!bCalc)
{
CFont * oft = pDC->SelectObject(&ftPrint);
CRect rcc(rcPage.left, rcPage.top, rcPage.right, rcPage.top + szFtPrint.cy);
pDC->MoveTo(rcc.left, rcc.top);
pDC->LineTo(rcc.right, rcc.top);
pDC->LineTo(rcc.right, rcc.bottom);
pDC->LineTo(rcc.left, rcc.bottom);
pDC->LineTo(rcc.left, rcc.top);
int box = 0;
CRect rc1;
int y = rcPage.top;
for (int t = 0; t < iNumCol; t++)
{
rc1.SetRect(rcPage.left+pDx[t], y, rcPage.left+pDx[t+1], y+szFtPrint.cy);
CString cs = pColTitle[t] ? pColTitle[t] : "";
pDC->Rectangle(rc1);
UINT ftm = DT_LEFT;
if (pFormat[t]&HDF_CENTER) ftm = DT_CENTER;
else if (pFormat[t]&HDF_RIGHT) ftm = DT_RIGHT;
pDC->DrawText(cs, rc1, ftm|DT_VCENTER);
pDC->MoveTo(rc1.left,rc1.bottom);
pDC->LineTo(rc1.left,rc1.top);
pDC->LineTo(rc1.right,rc1.top);
}
pDC->SelectObject(oft);
}
rcPage.top += szFtPrint.cy;
return CSize(0,szFtPrint.cy);
}
CSize
CFeeView::PrintListFoot(CDC * pDC, CRect & rcPage, bool bCalc)
{
return CSize(0,0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -