EasyPrinterV1.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. #include "stdafx.h"
  2. #include "EasyPrinterV1.h"
  3. #include <Shlwapi.h>
  4. CEasyPrinterV1::CEasyPrinterV1()
  5. {
  6. }
  7. CEasyPrinterV1::~CEasyPrinterV1()
  8. {
  9. }
  10. /************************************************************************/
  11. /* 函数:[3/7/2017 Jeff];
  12. /* 描述:;
  13. /* 参数:;
  14. /* [IN] strText:输入的文本内容(必须是ASCII或多字节);
  15. /* [IN] nRectWide:文本显示的区域宽度;
  16. /* [OUT] vtSubText:返回换行的字符串数组;
  17. /* 返回:void;
  18. /* 注意:任何字节打印的长度都是固定的,任何汉字的长度是2倍于字节打印长度;
  19. /* 示例:;
  20. /*
  21. /* 修改:;
  22. /* 日期:;
  23. /* 内容:;
  24. /************************************************************************/
  25. void CEasyPrinterV1::TextDownwards(IN TString strText, IN const INT& nRectWide, OUT vector<TString>& vtSubText)
  26. {
  27. if (nRectWide < 1)
  28. {
  29. TRACE("图元宽 - 2*边距值 < 0 \n");
  30. return;
  31. }
  32. TString str;
  33. SIZE fontSize;
  34. // 计算出一个字节的宽度(注:是一个byte的长度);
  35. GetTextExtentPoint32(m_dcPrinter, "1", 1, &fontSize);
  36. const INT nCharWide = fontSize.cx;
  37. // 字符数;
  38. INT nCharSum = 0;
  39. // 区域宽度/字符宽度 = 大约字符数 此值固定;
  40. const INT nCharCount = nRectWide / nCharWide;
  41. // 按 "\r\n" 分段;
  42. INT nIndex = 0;
  43. strText.append("\r\n");
  44. vector<TString> vtWrapText;
  45. do
  46. {
  47. if ((nIndex = strText.find("\r\n")) != TString::npos)
  48. {
  49. str = strText.substr(0, nIndex);
  50. if (str.size() == 0)
  51. vtWrapText.push_back("\r\n");
  52. else
  53. vtWrapText.push_back(str);
  54. strText = strText.substr(nIndex + strlen("\r\n"));
  55. }
  56. } while (strText.find("\r\n") != TString::npos);
  57. for (vector<TString>::iterator it = vtWrapText.begin(); it != vtWrapText.end(); it++)
  58. {
  59. strText = it->c_str();
  60. do
  61. {
  62. // 计算出大约字符数,也是分行的大约位置;
  63. nCharSum = nCharCount;
  64. if (strText.length() > nCharSum)
  65. {
  66. str = strText.substr(0, nCharSum);
  67. GetTextExtentPoint32(m_dcPrinter, str.c_str(), str.length(), &fontSize);
  68. if (nRectWide >= fontSize.cx)
  69. {
  70. do
  71. {
  72. if (nCharSum >= strText.size())
  73. break;
  74. // 仍短,加长度;
  75. str = strText.substr(0, ++nCharSum);
  76. // 是否有半个汉字;
  77. if (IsHalfChineseWord(str.c_str()))
  78. {
  79. str = strText.substr(0, ++nCharSum);
  80. }
  81. // 再获取字符串宽度;
  82. GetTextExtentPoint32(m_dcPrinter, str.c_str(), str.length(), &fontSize);
  83. } while (fontSize.cx < nRectWide);
  84. str = strText.substr(0, --nCharSum);
  85. if (IsHalfChineseWord(str.c_str()))
  86. {
  87. --nCharSum;
  88. str = strText.substr(0, nCharSum);
  89. }
  90. strText = strText.substr(nCharSum);
  91. vtSubText.push_back(str);
  92. }
  93. else/* if ( fontSize.cx > rcOld.Width() )*/
  94. {
  95. do
  96. {
  97. if (nCharSum <= 0)
  98. break;
  99. // 仍长,减长度;
  100. str = strText.substr(0, --nCharSum);
  101. // 是否有半个汉字;
  102. if (IsHalfChineseWord(str.c_str()))
  103. {
  104. str = strText.substr(0, --nCharSum);
  105. }
  106. // 再获取字符串宽度;
  107. GetTextExtentPoint32(m_dcPrinter, str.c_str(), str.length(), &fontSize);
  108. } while (fontSize.cx > nRectWide);
  109. str = strText.substr(0, ++nCharSum);
  110. if (IsHalfChineseWord(str.c_str()))
  111. {
  112. ++nCharSum;
  113. str = strText.substr(0, nCharSum);
  114. }
  115. strText = strText.substr(nCharSum);
  116. vtSubText.push_back(str);
  117. }
  118. GetTextExtentPoint32(m_dcPrinter, strText.c_str(), strText.length(), &fontSize);
  119. if (fontSize.cx <= nRectWide)
  120. vtSubText.push_back(strText);
  121. }
  122. else
  123. {
  124. // 长度不溢出;
  125. vtSubText.push_back(strText);
  126. GetTextExtentPoint32(m_dcPrinter, strText.c_str(), strText.length(), &fontSize);
  127. }
  128. } while (fontSize.cx > nRectWide);
  129. }
  130. }
  131. /************************************************************************/
  132. /* 函数:[3/10/2017 Jeff];
  133. /* 描述:;
  134. /* 参数:;
  135. /* [IN] :;
  136. /* [OUT] :;
  137. /* [IN/OUT] :;
  138. /* 返回:0 = 未画,1 = 画一半,2 = 全画完;
  139. /* 注意:;
  140. /* 示例:;
  141. /*
  142. /* 修改:;
  143. /* 日期:;
  144. /* 内容:;
  145. /************************************************************************/
  146. INT CEasyPrinterV1::DrawRectange(IN CRect rc, IN INT nLineWide, IN INT leftLineType, IN INT topLineType, IN INT rightLineType, IN INT bottomLineType)
  147. {
  148. INT nDrawResult = 0;
  149. if (rc.top >= m_printlist.rcPage.bottom)
  150. {
  151. // 未画,表示需要换页;
  152. return nDrawResult;
  153. }
  154. // 判断区域是否超过底部;
  155. if (rc.bottom > m_printlist.rcPage.bottom)
  156. {
  157. // 画一半,剩余要换页;
  158. nDrawResult = 1;
  159. rc.bottom = m_printlist.rcPage.bottom;
  160. // 如果只画了一半,下边透明;
  161. //topLineType = bottomLineType = 0;
  162. }
  163. else
  164. {
  165. // 全画;
  166. nDrawResult = 2;
  167. }
  168. CPen pen[4];
  169. // 画左边线;
  170. if (leftLineType == 0)
  171. pen[0].CreatePen(PS_NULL, nLineWide, RGB(0, 0, 0));
  172. else if (leftLineType == 1)
  173. pen[0].CreatePen(PS_DASH, nLineWide, RGB(0, 0, 0));
  174. else if (leftLineType == 2)
  175. pen[0].CreatePen(PS_SOLID, nLineWide, RGB(0, 0, 0));
  176. // 选入DC中;
  177. m_dcPrinter.SelectObject(&pen[0]);
  178. m_dcPrinter.MoveTo(rc.left, rc.top);
  179. m_dcPrinter.LineTo(rc.left, rc.bottom);
  180. pen[0].DeleteObject();
  181. // 画上边线;
  182. if (topLineType == 0)
  183. pen[1].CreatePen(PS_NULL, nLineWide, RGB(0, 0, 0));
  184. else if (topLineType == 1)
  185. pen[1].CreatePen(PS_DASH, nLineWide, RGB(0, 0, 0));
  186. else if (topLineType == 2)
  187. pen[1].CreatePen(PS_SOLID, nLineWide, RGB(0, 0, 0));
  188. // 选入DC中;
  189. m_dcPrinter.SelectObject(&pen[1]);
  190. m_dcPrinter.MoveTo(rc.left, rc.top);
  191. m_dcPrinter.LineTo(rc.right, rc.top);
  192. pen[1].DeleteObject();
  193. // 画右边线;
  194. if (rightLineType == 0)
  195. pen[2].CreatePen(PS_NULL, nLineWide, RGB(0, 0, 0));
  196. else if (rightLineType == 1)
  197. pen[2].CreatePen(PS_DASH, nLineWide, RGB(0, 0, 0));
  198. else if (rightLineType == 2)
  199. pen[2].CreatePen(PS_SOLID, nLineWide, RGB(0, 0, 0));
  200. // 选入DC中;
  201. m_dcPrinter.SelectObject(&pen[2]);
  202. m_dcPrinter.MoveTo(rc.right, rc.top);
  203. m_dcPrinter.LineTo(rc.right, rc.bottom);
  204. pen[2].DeleteObject();
  205. // 画下边线;
  206. if (bottomLineType == 0)
  207. pen[3].CreatePen(PS_NULL, nLineWide, RGB(0, 0, 0));
  208. else if (bottomLineType == 1)
  209. pen[3].CreatePen(PS_DASH, nLineWide, RGB(0, 0, 0));
  210. else if (bottomLineType == 2)
  211. pen[3].CreatePen(PS_SOLID, nLineWide, RGB(0, 0, 0));
  212. // 选入DC中;
  213. m_dcPrinter.SelectObject(&pen[3]);
  214. m_dcPrinter.MoveTo(rc.left, rc.bottom);
  215. m_dcPrinter.LineTo(rc.right, rc.bottom);
  216. pen[3].DeleteObject();
  217. return nDrawResult;
  218. }
  219. /************************************************************************/
  220. /* 函数:[3/9/2017 Jeff];
  221. /* 描述:;
  222. /* 参数:;
  223. /* [IN] nTextWide:文本字体长度;
  224. /* [OUT] :;
  225. /* [IN/OUT] :;
  226. /* 返回:void;
  227. /* 注意:;
  228. /* 示例:;
  229. /*
  230. /* 修改:;
  231. /* 日期:;
  232. /* 内容:;
  233. /************************************************************************/
  234. void CEasyPrinterV1::DrawText(IN TString strText, IN CRect rcText, IN INT nTextWide, IN INT nAlignType /* = 0 */)
  235. {
  236. switch (nAlignType)
  237. {
  238. case 0: // 左对齐,默认;
  239. break;
  240. case 1: // 水平居中;
  241. rcText.left += (rcText.Width() - nTextWide) / 2;
  242. break;
  243. case 2: // 右对齐;
  244. rcText.left += rcText.Width() - nTextWide;
  245. break;
  246. case 3: // 垂直居中;
  247. break;
  248. default:
  249. break;
  250. }
  251. m_dcPrinter.TextOut(rcText.left, rcText.top, strText.c_str());
  252. }
  253. /************************************************************************/
  254. /* 函数:[3/16/2017 Jeff];
  255. /* 描述:;
  256. /* 参数:;
  257. /* [IN] :;
  258. /* [OUT] :;
  259. /* [IN/OUT] :;
  260. /* 返回:void;
  261. /* 注意:;
  262. /* 示例:;
  263. /*
  264. /* 修改:;
  265. /* 日期:;
  266. /* 内容:;
  267. /************************************************************************/
  268. void CEasyPrinterV1::CalcPrinterCell(IN STPrintCell &PrintCell)
  269. {
  270. // 默认设置所有区域与原始区域一致;
  271. PrintCell.rcChg = PrintCell.rcOrg;
  272. // 填充模式;
  273. if (PrintCell.nFillerType == 0)
  274. {// 文本填充;
  275. SIZE fontSize;
  276. INT nTextlen = PrintCell.strTextContent.size();
  277. // 根据文本特性,计算出文本长度和高度;
  278. SetTextFont(PrintCell.nTextFontSize, PrintCell.bTextBold, PrintCell.bTextItalic, PrintCell.strTextFontName);
  279. INT nRet = GetTextExtentPoint32(m_dcPrinter, PrintCell.strTextContent.c_str(), nTextlen, &fontSize); // 在打印机设备DC上获取文本长度;
  280. if (fontSize.cx > PrintCell.rcOrg.Width())
  281. {// 字体内容溢出;
  282. TextDownwards(PrintCell.strTextContent, PrintCell.rcOrg.Width() - PrintCell.nCellMargins * 2, PrintCell.vtSubText);
  283. // 是否向下延伸;
  284. if (PrintCell.bAutoWraps && PrintCell.vtSubText.size())
  285. {
  286. // 如果是bDownwards属性,清空;
  287. PrintCell.strTextContent.clear();
  288. if (!PrintCell.rcOrg.IsRectEmpty() && !PrintCell.rcOrg.IsRectNull())
  289. {// 表格区域不能无效;
  290. // 新高 = 字体高*行数 + 上下边距*2 + (行 - 1)*行间距;
  291. PrintCell.rcChg.bottom = PrintCell.rcChg.top + PrintCell.vtSubText.size()*fontSize.cy + PrintCell.nCellMargins * 2 + PrintCell.nLineSpacing*(PrintCell.vtSubText.size() - 1);
  292. }
  293. }
  294. }
  295. }
  296. else if (PrintCell.nFillerType == 1)
  297. {// 图片填充,需要将图片压缩到指定大小;
  298. TRACE("图片显示区域大小不需要改变\n");
  299. }
  300. // 是否已经向下延伸了;
  301. if (PrintCell.rcOrg != PrintCell.rcChg)
  302. PrintCell.bHasWrapped = TRUE;
  303. else
  304. PrintCell.bHasWrapped = FALSE;
  305. }
  306. /************************************************************************/
  307. /* 函数:[3/16/2017 Jeff];
  308. /* 描述:;
  309. /* 参数:;
  310. /* [IN] :;
  311. /* [OUT] :;
  312. /* [IN/OUT] :;
  313. /* 返回:void;
  314. /* 注意:;
  315. /* 示例:;
  316. /*
  317. /* 修改:;
  318. /* 日期:;
  319. /* 内容:;
  320. /************************************************************************/
  321. void CEasyPrinterV1::CellTBTypesetting()
  322. {
  323. // 首先处理的是上下相邻的两个图元,将相衔接一起;
  324. vector<STPrintCell>::iterator it = m_printlist.vtPrintCell.begin();
  325. for (; it != m_printlist.vtPrintCell.end(); it++)
  326. {
  327. vector<STPrintCell>::iterator it1 = m_printlist.vtPrintCell.begin();
  328. for (; it1 != m_printlist.vtPrintCell.end(); it1++)
  329. {
  330. if (it != it1)
  331. {
  332. if (it->rcOrg.bottom == it1->rcOrg.top)
  333. {
  334. // 判断是否已经衔接;
  335. if (it->rcChg.bottom != it1->rcChg.top)
  336. {
  337. if (it1->bTypesetting)
  338. {
  339. if (it->rcChg.bottom > it1->rcChg.top)
  340. {
  341. it1->rcChg.OffsetRect(0, it->rcChg.bottom - it1->rcChg.top);
  342. }
  343. }
  344. else
  345. {
  346. if (it->rcChg.bottom > it1->rcChg.top)
  347. {
  348. it1->rcChg.OffsetRect(0, it->rcChg.bottom - it1->rcChg.top);
  349. }
  350. }
  351. }
  352. }
  353. }
  354. }
  355. }
  356. }
  357. void CEasyPrinterV1::CellBBTypesetting()
  358. {
  359. // 其次,再处理同底的图元,不使用offset;
  360. vector<STPrintCell>::iterator it = m_printlist.vtPrintCell.begin();
  361. for (; it != m_printlist.vtPrintCell.end(); it++)
  362. {
  363. vector<STPrintCell>::iterator it1 = m_printlist.vtPrintCell.begin();
  364. for (; it1 != m_printlist.vtPrintCell.end(); it1++)
  365. {
  366. if (it != it1)
  367. {
  368. if (it->rcOrg.bottom == it1->rcOrg.bottom)
  369. {
  370. if (it->rcChg.bottom != it1->rcChg.bottom)
  371. {
  372. if (it->rcChg.bottom > it1->rcChg.bottom)
  373. {
  374. it1->rcChg.bottom = it->rcChg.bottom;
  375. }
  376. else
  377. {
  378. it->rcChg.bottom = it1->rcChg.bottom;
  379. }
  380. // 变更了上下衔接;
  381. CellTBTypesetting();
  382. }
  383. }
  384. }
  385. }
  386. }
  387. }
  388. BOOL CEasyPrinterV1::PrintByJson()
  389. {
  390. // 1.初始打印机;
  391. if (FALSE == InitialzePrinter(m_printlist.strPrinterName.c_str()))
  392. return FALSE;
  393. // 3.计算每个图元在使用填充后的实际大小;
  394. vector<STPrintCell>::iterator it = m_printlist.vtPrintCell.begin();
  395. for (; it != m_printlist.vtPrintCell.end(); it++)
  396. {// 不建议图元同时具有向下和向右延伸的特性;
  397. CalcPrinterCell(*it);
  398. }
  399. CellTBTypesetting();
  400. CellBBTypesetting();
  401. #if 1// 让字体适应固定的图元区域;
  402. //////////////////////////////////////////////////////////////////////////
  403. // 如果固定大小的单元格变化了,根据变化后的区域重新计算文本字体大小;
  404. it = m_printlist.vtPrintCell.begin();
  405. for (; it != m_printlist.vtPrintCell.end(); it++)
  406. {
  407. if (!it->bAutoWraps)
  408. {
  409. SIZE fontSize;
  410. INT nTextlen = it->strTextContent.size();
  411. // 根据文本特性,计算出文本长度和高度;
  412. SetTextFont(it->nTextFontSize, it->bTextBold, it->bTextItalic, it->strTextFontName);
  413. GetTextExtentPoint32(m_dcPrinter, "1", 1, &fontSize); // 在打印机设备DC上获取文本长度;
  414. // 新高 = 字体高*行数 + 上下边距*2 + (行 - 1)*行间距;
  415. INT nHeight = it->rcChg.top + it->vtSubText.size()*fontSize.cy + it->nCellMargins * 2 + it->nLineSpacing*(it->vtSubText.size() - 1);
  416. while (nHeight > it->rcChg.bottom)
  417. {
  418. if (it->nTextFontSize < 0)
  419. {
  420. WriteTextLog("出错:固定区域文本字体缩小成负值仍无法显示");
  421. break;
  422. }
  423. it->nTextFontSize--;
  424. SetTextFont(it->nTextFontSize, it->bTextBold, it->bTextItalic, it->strTextFontName);
  425. GetTextExtentPoint32(m_dcPrinter, it->strTextContent.c_str(), nTextlen, &fontSize);
  426. it->vtSubText.clear();
  427. TextDownwards(it->strTextContent, it->rcChg.Width() - it->nCellMargins * 2, it->vtSubText);
  428. nHeight = it->rcChg.top + it->vtSubText.size()*fontSize.cy + it->nCellMargins * 2 + it->nLineSpacing*(it->vtSubText.size() - 1);
  429. }
  430. if (it->vtSubText.size())
  431. it->strTextContent.clear();
  432. }
  433. }
  434. // 注意:在最后一页时,需要再次重新计算;
  435. //////////////////////////////////////////////////////////////////////////
  436. #endif
  437. // 将变化后的单元格设置为原始单元格;
  438. it = m_printlist.vtPrintCell.begin();
  439. for (; it != m_printlist.vtPrintCell.end(); it++)
  440. {
  441. it->rcOrg = it->rcChg;
  442. }
  443. // 开始打印Json形成的表格;
  444. StartPrint();
  445. StartPage();
  446. // 画外围边框;
  447. INT nDrawResult = 0;
  448. if (m_printlist.bShowFrame)
  449. DrawRectange(m_printlist.rcPage, m_printlist.nLineWide, 2, 2, 2, 2);
  450. it = m_printlist.vtPrintCell.begin();
  451. for (; it != m_printlist.vtPrintCell.end();)
  452. {
  453. nDrawResult = DrawCell(*it);
  454. if (nDrawResult == 0 || nDrawResult == 1)
  455. {
  456. // 保留;
  457. it++;
  458. }
  459. else if (nDrawResult == 2)
  460. {
  461. // 移除;
  462. it = m_printlist.vtPrintCell.erase(it);
  463. }
  464. }
  465. while (m_printlist.vtPrintCell.size())
  466. {
  467. PrintChangePage();
  468. }
  469. EndofPage();
  470. EndofPrint();
  471. return TRUE;
  472. }
  473. INT CEasyPrinterV1::DrawCell(IN STPrintCell & PrtCell)
  474. {
  475. // 是否换页;
  476. BOOL bChangePage = FALSE;
  477. // 画表格;
  478. INT nDrawResult = DrawRectange(PrtCell.rcChg, PrtCell.nLineWide, PrtCell.nLeftLineType, PrtCell.nTopLineType, PrtCell.nRightLineType, PrtCell.nBottomLineType);
  479. if (nDrawResult == 0)
  480. {
  481. // 未画,表示表格需要换页;
  482. return nDrawResult;
  483. }
  484. SIZE fontSize;
  485. if (PrtCell.nFillerType == 0)
  486. {
  487. // 输出文本;
  488. CRect rcText = PrtCell.rcChg;
  489. rcText.DeflateRect(PrtCell.nCellMargins, PrtCell.nCellMargins, PrtCell.nCellMargins, 0);
  490. SetTextFont(PrtCell.nTextFontSize, PrtCell.bTextBold, PrtCell.bTextItalic, PrtCell.strTextFontName);
  491. // 获取字高;
  492. if (PrtCell.vtSubText.size())
  493. {
  494. GetTextExtentPoint32(m_dcPrinter, _T("1"), 1, &fontSize);
  495. INT nHeight = rcText.top + PrtCell.nLineSpacing + fontSize.cy; // 已输出文本高度;
  496. vector<TString>::iterator it = PrtCell.vtSubText.begin();
  497. for (; it != PrtCell.vtSubText.end();)
  498. {
  499. // 输出文本之前,判断下一行是否到达底部;
  500. if (m_printlist.rcPage.bottom < nHeight)
  501. {
  502. // 换页的,不操作任何;
  503. it++;
  504. }
  505. else
  506. {
  507. // 输出文本的区域仍在底部上方,输出文本;
  508. GetTextExtentPoint32(m_dcPrinter, it->c_str(), it->length(), &fontSize);
  509. DrawText(*it, rcText, fontSize.cx, PrtCell.nTextAlignType);
  510. rcText.top += PrtCell.nLineSpacing + fontSize.cy;
  511. rcText.bottom = rcText.top + PrtCell.nLineSpacing + fontSize.cy;
  512. // 未换页的,移除掉;
  513. it = PrtCell.vtSubText.erase(it);
  514. nHeight = rcText.bottom;
  515. }
  516. }
  517. }
  518. else
  519. {
  520. if (PrtCell.strTextContent.length())
  521. {
  522. GetTextExtentPoint32(m_dcPrinter, PrtCell.strTextContent.c_str(), PrtCell.strTextContent.length(), &fontSize);
  523. // 输出文本之前,判断显示区域能否完整容纳文本;
  524. if (m_printlist.rcPage.bottom < (rcText.top + fontSize.cy + PrtCell.nCellMargins * 2))
  525. {
  526. // 无法输出文本;
  527. // 换页的,不操作任何;
  528. }
  529. else
  530. {
  531. DrawText(PrtCell.strTextContent, rcText, fontSize.cx, PrtCell.nTextAlignType);
  532. // 输出后,清空文本;
  533. PrtCell.strTextContent.clear();
  534. }
  535. }
  536. }
  537. }
  538. else if (PrtCell.nFillerType == 1)
  539. {// 图片;
  540. TString strImageBase64 = PrtCell.strImgContent;
  541. BYTE *pImageBuffer = NULL;
  542. DWORD dwImageLength = 0;
  543. GetImgBufferFromBase64(PrtCell.strImgContent, &pImageBuffer, dwImageLength);
  544. Image *pImage = NULL;
  545. LoadImgFromBuffer(&pImage, pImageBuffer, dwImageLength);
  546. // 画图;
  547. Graphics graph(m_dcPrinter);
  548. graph.SetPageUnit(UnitPixel);
  549. graph.SetSmoothingMode(SmoothingModeHighQuality);
  550. RectF rf(PrtCell.rcOrg.left, PrtCell.rcOrg.top, PrtCell.rcOrg.Width(), PrtCell.rcOrg.Height());
  551. graph.DrawImage(pImage, rf, 0, 0, pImage->GetWidth(), pImage->GetHeight(), UnitPixel);
  552. // 释放资源;
  553. if (pImageBuffer)
  554. delete[]pImageBuffer;
  555. if (pImage)
  556. delete pImage;
  557. }
  558. if (nDrawResult == 1)
  559. {
  560. PrtCell.bDrawHalf = TRUE;
  561. // 画一半,剩余部分需要换页;
  562. if (PrtCell.nFillerType == 0)
  563. {// 文本;
  564. INT nHeightGap = PrtCell.rcChg.bottom - m_printlist.rcPage.bottom; // 高度差;
  565. PrtCell.rcChg.top = PrtCell.rcChg.bottom - nHeightGap;
  566. }
  567. else if (PrtCell.nFillerType == 1)
  568. {
  569. // 如果填充物是图片,需要在画单元格外框时提前换页;
  570. TRACE("暂时未处理\n");
  571. }
  572. }
  573. else if (nDrawResult == 2)
  574. {
  575. // 全画,未换页;
  576. TRACE("不需要处理\n");
  577. }
  578. return nDrawResult;
  579. }
  580. /************************************************************************/
  581. /* 函数:[3/14/2017 Jeff];
  582. /* 描述:;
  583. /* 参数:;
  584. /* [IN] :;
  585. /* [OUT] :;
  586. /* [IN/OUT] :;
  587. /* 返回:void;
  588. /* 注意:;
  589. /* 示例:;
  590. /*
  591. /* 修改:;
  592. /* 日期:;
  593. /* 内容:;
  594. /************************************************************************/
  595. void CEasyPrinterV1::PrintChangePage()
  596. {
  597. AutoChangePage();
  598. // 画单元格;
  599. NewAnPage();
  600. INT nDrawResult = 0;
  601. vector<STPrintCell>::iterator it = m_printlist.vtPrintCell.begin();
  602. for (; it != m_printlist.vtPrintCell.end();)
  603. {
  604. nDrawResult = DrawCell(*it);
  605. if (nDrawResult == 0 || nDrawResult == 1)
  606. {
  607. // 保留;
  608. it++;
  609. }
  610. else if (nDrawResult == 2)
  611. {
  612. // 移除;
  613. it = m_printlist.vtPrintCell.erase(it);
  614. }
  615. }
  616. }
  617. /************************************************************************/
  618. /* 函数:[3/14/2017 Jeff];
  619. /* 描述:;
  620. /* 参数:;
  621. /* [IN] :;
  622. /* [OUT] :;
  623. /* [IN/OUT] :;
  624. /* 返回:void;
  625. /* 注意:;
  626. /* 示例:;
  627. /*
  628. /* 修改:;
  629. /* 日期:;
  630. /* 内容:;
  631. /************************************************************************/
  632. void CEasyPrinterV1::AutoChangePage()
  633. {
  634. // 换页计算,偏移到下一页位置;
  635. INT nOffsetGap = m_printlist.rcPage.top - m_printlist.rcPage.bottom;
  636. vector<STPrintCell>::iterator it = m_printlist.vtPrintCell.begin();
  637. for (; it != m_printlist.vtPrintCell.end(); it++)
  638. {
  639. // 所有单元格y坐标偏移nOffsetGap;
  640. if (it->bDrawHalf)
  641. {
  642. it->rcOrg.OffsetRect(0, nOffsetGap);
  643. it->rcOrg.top = m_printlist.rcPage.top;
  644. it->rcChg.OffsetRect(0, nOffsetGap);
  645. it->rcChg.top = m_printlist.rcPage.top;
  646. }
  647. else
  648. {
  649. it->rcOrg.OffsetRect(0, nOffsetGap);
  650. it->rcChg.OffsetRect(0, nOffsetGap);
  651. }
  652. }
  653. // 需要重新对文本框计算区域大小;
  654. it = m_printlist.vtPrintCell.begin();
  655. for (; it != m_printlist.vtPrintCell.end(); it++)
  656. {
  657. if (it->nFillerType == 0)
  658. {
  659. if (it->vtSubText.size() != 0)
  660. {// 已经换过行的;
  661. SIZE fontSize;
  662. // 根据文本特性,计算出文本长度和高度;
  663. SetTextFont(it->nTextFontSize, it->bTextBold, it->bTextItalic, it->strTextFontName);
  664. GetTextExtentPoint32(m_dcPrinter, "1", 1, &fontSize);
  665. // 计算出新高;
  666. INT nHeight = it->vtSubText.size()*fontSize.cy + it->nCellMargins * 2 + it->nLineSpacing*(it->vtSubText.size() - 1);
  667. if (it->rcOrg.Height() < nHeight)
  668. {
  669. if (it->bAutoWraps)
  670. {// 字体固定,表格区域在适应字体;
  671. it->rcChg.bottom = it->rcChg.top + nHeight;
  672. }
  673. else
  674. {// 区域固定,字体要适应区域;
  675. do
  676. {
  677. SetTextFont(it->nTextFontSize--, it->bTextBold, it->bTextItalic, it->strTextFontName);
  678. GetTextExtentPoint32(m_dcPrinter, "1", 1, &fontSize);
  679. nHeight = it->vtSubText.size()*fontSize.cy + it->nCellMargins * 2 + it->nLineSpacing*(it->vtSubText.size() - 1);
  680. } while (it->rcOrg.Height() < nHeight);
  681. }
  682. }
  683. }
  684. }
  685. else if (it->nFillerType == 1)
  686. {
  687. TRACE("图片显示区域大小不需要改变\n");
  688. }
  689. }
  690. CellTBTypesetting();
  691. CellBBTypesetting();
  692. }