DataGrid的打印(二)

来源:互联网 发布:终端数据采集器 编辑:程序博客网 时间:2024/05/22 14:40


#include "StdAfx.h"
#include "./printhelper.h"
#include "math.h"
#include <string>

static const unsigned int GRID_WIDTH[] =
{
  13,
  22,
  22,
  29,
  29,
  20,
  25,
  32,
  23,
  23,
  35,
  20,
  32,
  32,
  32
};

static const unsigned int A3_PAPER_WIDTH = 420;

CPrintHelper::CPrintHelper(CDC * pDc, bool bHeader, bool bFooter) : _pDc(pDc)
, _bHeader(bHeader)
, _bFooter(bFooter)
{
 _dpi = _pDc->GetDeviceCaps(LOGPIXELSX); // UNDONE: GetDeviceCaps(HORZRES)/GetDeviceCaps(HORZSIZE)/25.4;

 // 紙寬(單位:mm)
 float pWidth = (float)ceil(25.4 * GetPaperWidth() / _dpi);

 _ratio = pWidth/A3_PAPER_WIDTH;

 // 要求左留白15mm,右留白16mm
 _leftMargin  = mm2pixel(15 * _ratio - GetPaperLMargin());
    _topMargin   = _bottomMargin = mm2pixel(float(15 * _ratio- GetPaperTMargin()) );
 _rightMargin = mm2pixel(float(16 * _ratio - GetPaperLMargin()) );

 _titleHeight = mm2pixel(20 * _ratio);
 _footerHeight = mm2pixel(5 * _ratio);

 _fontHeight = mm2pixel((float)3.5 * _ratio);
 _LineSpacing = mm2pixel(1.5f * _ratio);

 _gridMargin = mm2pixel((float)0.5 * _ratio);
 _gridNUmber = sizeof(CSV_ATTRI) / sizeof(char *);

 _headerHeight = 3 * GetLinePixelHeight(); //屬性打印的表頭行和表之間空開一行

 _pFont = new CFont();
 _pFont->CreateFont(_fontHeight, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0, DEFAULT_CHARSET,
  OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  DEFAULT_PITCH | FF_SWISS,  "MS ゴシック");
 _pDc->SelectObject(_pFont);

 _pPen = new CPen;
 _pPen->CreatePen(PS_SOLID, 3, RGB(0, 0, 0));
 _pDc->SelectObject(_pPen);

 TEXTMETRIC TextM;
 pDc->GetTextMetrics(&TextM);
 _CharWidth = pixel2mm(TextM.tmAveCharWidth);

 return;
}

CPrintHelper::~CPrintHelper() {
  _pFont->DeleteObject();
  _pPen->DeleteObject();
  delete _pFont;
  delete _pPen;
}

//頁眉的打印
void CPrintHelper::PrintHeader(CString title, CString sYear,
    CString sImage, CString eYear, CString eImage)
{
 if ( ! _bHeader )
  return;

 CTime t = CTime::GetCurrentTime();
 CString date = "印刷日時: " + t.Format("%Y/%m/%d  %H:%M:%S");

 //打印表頭標題 如:属性情報の印刷 -秋田地區 平面直角坐標係 第10係
 //打印時間,如:2006/01/01/ 10:10:00
 _pDc->TextOut(_leftMargin, _topMargin, title + "          " + date);

 //打印 撮影年和画像
 _pDc->TextOut(_leftMargin, _topMargin + GetLinePixelHeight(),
  "期首画像の撮影年:" + sYear + "    期首画像:" + sImage + "         "
  "期末画像の撮影年:" + eYear + "    期末画像:" + eImage);
}

//MsFlexGrid頭
void CPrintHelper::PrintGridHeader()
{
 int x = _leftMargin + 1;
 int y = _topMargin + GetHeaderHeight();
 PrintLineGrid(y);

 CRect r;
 for(int i = 0; i <= sizeof(CSV_ATTRI) / sizeof(char *); ++i) {
  r.SetRect(x, y, x + mm2pixel(float(GRID_WIDTH[i]) * _ratio), y + GetLinePixelHeight());
  _pDc->DrawText(CSV_ATTRI[i], &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  x += mm2pixel(float(GRID_WIDTH[i])* _ratio) ;
 }

};

//打印頁脚的内容:第 X 頁
void CPrintHelper::PrintFooter(int currentPage) //, int totalPage)
{
 if ( ! _bFooter )
  return;

 CString tmp;
 //tmp.Format("第 %d 頁/全 %d 頁", currentPage, totalPage);
 tmp.Format("第 %d 頁", currentPage);

 int x = int( GetPagePixelWidth() - 0.5 * tmp.GetLength() * GetFontPixelHeight());
 int y = GetPagePixelHeight() - _bottomMargin - GetFooterHeight();

 _pDc->TextOut(x/2, y, tmp);
};

int CPrintHelper::PrintPage(CMsflexgrid1 &grid, int rowPos)
{
 // 打印行位置、一頁的行數量
 int printLinePos = 1;

 while(rowPos < grid.get_Rows() && printLinePos < LinesPerPage())
 {
  if ( ! PrintLineText(grid, rowPos, printLinePos) )
   break;

  rowPos++;
 }
 return rowPos;
}

// 打印一條row數據需要多少行
int CPrintHelper::GetPrintLineNumber(CMsflexgrid1 &grid, int rowPos)
{
 int maxHeight = 1;
 CString cell;
 for(int i = 1; i < grid.GetCols(); i++)
 {
  cell = grid.GetTextMatrix(rowPos, i);
  float charsInCell = ((GRID_WIDTH[i] * _ratio - pixel2mm(_gridMargin * 2)) / _CharWidth);
  int height = (int) ceil( cell.GetLength() / charsInCell);

  if ( maxHeight < height )
   maxHeight = height;
 }

 return maxHeight;
}

 

// 畫一行格子( 格子大小取自GRID_WIDTH[] )
void CPrintHelper::PrintLineGrid(int top, int height) {
 int buttom = top + height * GetLinePixelHeight();

 // 最左邊的竪綫
 _pDc->MoveTo(_leftMargin, top);
 _pDc->LineTo(_leftMargin, buttom);

 // 畫各個格子的右邊竪綫
 int x = 0;
 for(int i = 0; i < _gridNUmber; ++i) {
  x += GRID_WIDTH[i];
  _pDc->MoveTo(_leftMargin + mm2pixel(x * _ratio), top);
  _pDc->LineTo(_leftMargin + mm2pixel(x * _ratio), buttom);
 }

 // 畫兩根橫綫
 _pDc->MoveTo(_leftMargin, top);
 _pDc->LineTo(_leftMargin + mm2pixel(x * _ratio), top);
 _pDc->MoveTo(_leftMargin, buttom);
 _pDc->LineTo(_leftMargin + mm2pixel(x * _ratio), buttom);

 return;
}


// 打印一行數據
bool CPrintHelper::PrintLineText(CMsflexgrid1 &grid, int row, int& linePosition) {

 int x = _leftMargin + 1;
 int y = _topMargin + linePosition * GetLinePixelHeight();
 y += GetHeaderHeight();

 // 當前row需要打印多少行
 int rowHeight = GetPrintLineNumber(grid, row);
 if ( linePosition + rowHeight > LinesPerPage() )
  return false;

 PrintLineGrid(y, rowHeight);

 CRect r;
 CString cellString;
 CString currString;

 for(int line = 0; line < rowHeight; line++) // 對各行
 {
  int x = 0;
  r.top = y;
  r.bottom = y + GetLinePixelHeight();

  for(int i = 0; i < _gridNUmber; i++) // 對各個格子
  {
   r.left = _leftMargin + mm2pixel(x * _ratio) + _gridMargin;
   x += GRID_WIDTH[i];
   r.right = _leftMargin + mm2pixel(x * _ratio) - _gridMargin;

   cellString = grid.GetTextMatrix(row, i); // 獲得文字
   int charsInCell = (int) floor((GRID_WIDTH[i] * _ratio - pixel2mm(_gridMargin * 2)) / _CharWidth); // 格子一行幾個字
   currString = GetSubString( cellString, line * charsInCell, charsInCell );   // cellString.Mid(line * charsInCell, charsInCell); // 當前打印的文字串

   if ( ! currString.IsEmpty() ) {
    _pDc->DrawText(currString, &r, (i == 0 ? DT_CENTER : DT_LEFT) | DT_VCENTER | DT_SINGLELINE);
   }

  }

  y += GetLinePixelHeight();
 }

 linePosition += rowHeight;
 return true;
}


CString CPrintHelper::GetSubString( const CString s, int iStartPos, int iLen ) const{
 CString sRet = "";
 int len = s.GetLength();
 int iCutLen = 0;

 int i = 0;
 char c;
 if ( iStartPos < len ){
  // get real cutting start pos
  int iNextPos = 0;
  while( iNextPos < iStartPos ){
   c = s[iNextPos];
   if ( c < 0 ){
    iNextPos += 2;
   }
   else{
    iNextPos ++;
   }
  }
  if ( iNextPos > iStartPos ){
   // return to start pos of 2 bytes char
   i = iStartPos - 1;
  }
  else{
   i = iStartPos;
  }

  while(  i < len && iCutLen < iLen ){
   c = s[i];
  
   if ( c > 0 ){
    sRet += CString( c );
    i++;
    iCutLen ++;
   }
   else{
    // 2Bytes Char
    iCutLen +=2;
    if ( iCutLen <= iLen ){
     sRet += s.Mid( i, 2 );
    }
    i+= 2;
   }
  }
 }

 return( sRet );
}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小米笔记本电脑忘记开机密码怎么办 小米笔记本忘记开机密码怎么办 小米手机儿童模式忘记密码怎么办 小米应用锁密码忘了怎么办 小米air密码忘了怎么办 小米4密码忘了怎么办 小米手机开机密码忘了怎么办? 小米笔记本电脑开机密码忘了怎么办 小米笔记本开机密码忘了怎么办 htc手机忘记解锁图案怎么办 红米手机屏幕锁定怎么解锁怎么办 小米5s有id怎么办 手机密码找不回来了怎么办? 手机密码图案忘了怎么办 手机屏幕图案锁忘了怎么办 捡到苹果7有id锁怎么办 魅族什么都忘了怎么办 海信电视百事通登陆失败怎么办 去哪儿换号了怎么办 ipan充不进去电怎么办 安卓数据线松了怎么办 索尼z5耳机掉漆怎么办 索尼z5无限重启怎么办 苹果8基带坏了怎么办 oppo手机忘记图案密码怎么办 电池充不进去电怎么办 电脑充不进去电怎么办 苹果5c白苹果怎么办 港行不支持电信卡怎么办 安卓导航不开机怎么办 鞭炮放一半不响怎么办 禁止鸣笛的地方鸣笛了怎么办 手被炮仗炸了怎么办 手被猴子抓伤了怎么办 炸东西剩的油怎么办 炸臭豆腐剩的油怎么办 油炸久了油发黑怎么办 炸鱼的时候粘锅怎么办 吃了葱蒜有味怎么办 哺乳期喝了抹茶怎么办 干炸小黄鱼凉了怎么办