Printkernel.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #include "StdAfx.h"
  2. #include "Printkernel.h"
  3. /************************************************************************/
  4. /* 函数:GIsHalfHZ[4/25/2016 IT];
  5. /* 描述:判断输入的字符串最后一个字符是否是汉字;
  6. /* 参数:;
  7. /* [IN] str:输入的字符串;
  8. /* 返回:如果是一个完整的汉字,返回FALSE。否则返回TRUE;
  9. /************************************************************************/
  10. BOOL GIsHalfHZ(const CString &str)
  11. {
  12. INT nLength = str.GetLength();
  13. BOOL IsHalf = FALSE;
  14. for ( INT i = 0; i < nLength; )
  15. {
  16. if ( str.GetAt(i) < 0 )
  17. {
  18. i += 2;
  19. if ( i > nLength )
  20. IsHalf = TRUE;
  21. }
  22. else
  23. {
  24. i++;
  25. }
  26. }
  27. return IsHalf;
  28. }
  29. CPrinter::CPrinter()
  30. {
  31. for (int i=0; i<10; i++)
  32. {
  33. m_iFace[i] = 0;
  34. for (int j=0; j<4; j++)
  35. m_font[i][j] = new (CFont);
  36. }
  37. m_nFonts = 0;
  38. m_iFont = 0;
  39. m_pPrintDlg = NULL;
  40. m_Margins.SetRect(0, 0, 0, 0);
  41. m_yCur = 0;
  42. m_xCur = 0;
  43. m_iPage = 1;
  44. m_nCopies = 1;
  45. }
  46. CPrinter::~CPrinter()
  47. {
  48. for (int i=0; i<10; i++)
  49. for (int j=0; j<4; j++)
  50. delete (m_font[i][j]);
  51. if (m_pPrintDlg)
  52. delete (m_pPrintDlg);
  53. }
  54. int CPrinter::DrawText(TCHAR *str, int iFont, int iFace, int format)
  55. {
  56. m_DC.TextOut(m_xCur, m_yCur, str);
  57. return 1;
  58. }
  59. int CPrinter::DrawText(TCHAR *str, CRect& m_rt, int iFont, int iFace, int format)
  60. {
  61. SIZE Size;
  62. GetTextExtentPoint32(m_DC.GetSafeHdc(), str, _tcslen(str), &Size);
  63. int left, top;
  64. if (format & FORMAT_HCENTER)
  65. {
  66. left = CALCX(m_rt.left) + CALCX(m_rt.Width())/2 - Size.cx/2;
  67. if (left < CALCX(m_rt.left))
  68. left = CALCX(m_rt.left);
  69. }
  70. else if(format & FORMAT_RIGHT)
  71. {
  72. left = CALCX(m_rt.left) + CALCX(m_rt.Width()) - Size.cx;
  73. if (left < CALCX(m_rt.left))
  74. left = CALCX(m_rt.left);
  75. }
  76. else
  77. left = CALCX(m_rt.left);
  78. if (format & FORMAT_VCENTER)
  79. {
  80. top = CALCY(m_rt.top) + CALCY(m_rt.Height())/2 - Size.cy/2;
  81. if (top < CALCY(m_rt.top))
  82. top = CALCY(m_rt.top);
  83. }
  84. else top = CALCY(m_rt.top);
  85. m_DC.TextOut(left, top, str);
  86. return 1;
  87. }
  88. /************************************************************************/
  89. /* 函数:[4/25/2016 IT];
  90. /* 描述:;
  91. /* 参数:;
  92. /* [IN] :;
  93. /* [OUT] :;
  94. /* [IN/OUT] :;
  95. /* 返回:void;
  96. /* 注意:LOGFONT需要说明的是成员变量lfHeight和lfWidth均为逻辑坐标值;
  97. /* 示例:;
  98. /*
  99. /* 修改:;
  100. /* 日期:;
  101. /* 内容:;
  102. /************************************************************************/
  103. int CPrinter::DrawTextEx(IN LPCTSTR lpStr, IN CRect& rtStr, IN LOGFONT& lf, IN const int& nAlign)
  104. {
  105. if ( lpStr == NULL )
  106. {
  107. _tprintf_s(_T("字符串指针空\n"));
  108. return -1;
  109. }
  110. if ( rtStr.IsRectEmpty() )
  111. {
  112. _tprintf_s(_T("字符串区域空\n"));
  113. return -1;
  114. }
  115. CFont cf;
  116. CFont *oldf = NULL;
  117. SIZE Size;
  118. int nLeft = 0, nTop = 0;
  119. cf.CreateFontIndirect(&lf);
  120. oldf = m_DC.SelectObject(&cf);
  121. // GetTextExtentPoint32返回字符串宽高(按逻辑坐标返回SIZE);
  122. if ( !GetTextExtentPoint32(m_DC.GetSafeHdc(), lpStr, _tcslen(lpStr), &Size) )
  123. return -1;
  124. // 处理情况;
  125. if ( Size.cx <= rtStr.Width() )
  126. {
  127. if (nAlign & FORMAT_HCENTER)
  128. {
  129. nLeft = rtStr.left + rtStr.Width()/2 - Size.cx/2;
  130. if ( nLeft < rtStr.left )
  131. nLeft = rtStr.left;
  132. }
  133. else if(nAlign & FORMAT_RIGHT)
  134. {
  135. nLeft = rtStr.left + rtStr.Width() - Size.cx;
  136. if (nLeft < rtStr.left)
  137. nLeft = rtStr.left;
  138. }
  139. else
  140. nLeft = rtStr.left;
  141. if (nAlign & FORMAT_VCENTER)
  142. {
  143. nTop = rtStr.top + rtStr.Height()/2 - Size.cy/2;
  144. if (nTop < rtStr.top)
  145. nTop = rtStr.top;
  146. }
  147. else
  148. nTop = rtStr.top;
  149. // 输出文本,注意TextOut使用的x,y是逻辑坐标值;
  150. m_DC.TextOut(nLeft, nTop, lpStr);
  151. }
  152. else
  153. {
  154. }
  155. m_DC.SelectObject(oldf);
  156. return 1;
  157. }
  158. int CPrinter::DrawHLine(int x_left, int y_left, int x_right, int y_right)
  159. {
  160. // CDC::MoveTo和CDC::LineTo的x,y坐标使用的是逻辑坐标值;
  161. m_DC.MoveTo(CALCX(m_Margins.left + x_left), CALCY(m_Margins.top + y_left));
  162. m_DC.LineTo(CALCX(m_Margins.left + x_right), CALCY(m_Margins.top + y_right));
  163. return 0;
  164. }
  165. int CPrinter::DrawHLine(int x_left, int y_left, int x_right, int y_right, CPen &newpen)
  166. {
  167. // CDC::MoveTo和CDC::LineTo的x,y坐标使用的是逻辑坐标值;
  168. CPen* oldPen = m_DC.SelectObject(&newpen);
  169. m_DC.MoveTo(CALCX(m_Margins.left + x_left), CALCY(m_Margins.top + y_left));
  170. m_DC.LineTo(CALCX(m_Margins.left + x_right), CALCY(m_Margins.top + y_right));
  171. m_DC.SelectObject(oldPen);
  172. return 0;
  173. }
  174. int CPrinter::DrawVLine(int x_up, int y_up, int x_bottom, int y_bottom)
  175. {
  176. // CDC::MoveTo和CDC::LineTo的x,y坐标使用的是逻辑坐标值;
  177. m_DC.MoveTo(CALCX(m_Margins.left + x_up), CALCY(m_Margins.top + y_up));
  178. m_DC.LineTo(CALCX(m_Margins.left + x_bottom), CALCY(m_Margins.top + y_bottom));
  179. return 0;
  180. }
  181. int CPrinter::DrawVLine(int x_up, int y_up, int x_bottom, int y_bottom, CPen &newpen)
  182. {
  183. // CDC::MoveTo和CDC::LineTo的x,y坐标使用的是逻辑坐标值;
  184. CPen* oldPen = m_DC.SelectObject(&newpen);
  185. m_DC.MoveTo(CALCX(m_Margins.left + x_up), CALCY(m_Margins.top + y_up));
  186. m_DC.LineTo(CALCX(m_Margins.left + x_bottom), CALCY(m_Margins.top + y_bottom));
  187. m_DC.SelectObject(oldPen);
  188. return 0;
  189. }
  190. /************************************************************************/
  191. /* 函数:[4/24/2016 IT];
  192. /* 描述:;
  193. /* 参数:;
  194. /* [IN] :;
  195. /* [OUT] :;
  196. /* [IN/OUT] :;
  197. /* 返回:void;
  198. /* 注意:;
  199. /* 示例:;
  200. /*
  201. /* 修改:;
  202. /* 日期:;
  203. /* 内容:;
  204. /************************************************************************/
  205. int CPrinter::DrawBarCode(TCHAR *str, CRect& rcBar)
  206. {
  207. Barcode128 code;
  208. code.SetRatio(1);
  209. code.Encode128B(str);
  210. COLORREF clrBar = RGB(0,0,0);
  211. COLORREF clrSpace = RGB(255,255,255);
  212. //rcBar = m_Margins;
  213. //P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code);
  214. //m_DC.DPtoLP(rcBar);
  215. //m_DC.SetMapMode(MM_TEXT);
  216. code.DrawBarcode(m_hPrinter, rcBar.left, rcBar.top, rcBar.right, rcBar.bottom, clrBar, clrSpace, 2);
  217. return 0;
  218. }
  219. int CPrinter::AddFont(CFont &newfont)
  220. {
  221. return -1;
  222. }
  223. int CPrinter::AddFont(LOGFONT *lf)
  224. {
  225. if (m_nFonts == 10)
  226. return -1;
  227. m_font[m_nFonts][FACE_NORMAL]->CreateFontIndirect(lf);
  228. lf->lfWeight = FW_BLACK;
  229. m_font[m_nFonts][FACE_NORMALBOLD]->CreateFontIndirect(lf);
  230. lf->lfWeight = FW_REGULAR;
  231. lf->lfHeight = CALCF(25);
  232. m_font[m_nFonts][FACE_BIG]->CreateFontIndirect(lf);
  233. lf->lfWeight = FW_BLACK;
  234. m_font[m_nFonts][FACE_BIGBOLD]->CreateFontIndirect(lf);
  235. m_nFonts ++;
  236. return 0;
  237. }
  238. int CPrinter::SetFontFace(int iFont, int iFace)
  239. {
  240. ASSERT(iFont < 10);
  241. ASSERT(iFace < 4);
  242. m_iFont = iFont;
  243. m_iFace[m_iFont] = iFace;
  244. m_DC.SelectObject(m_font[m_iFont][m_iFace[m_iFont]]);
  245. return 0;
  246. }
  247. /************************************************************************/
  248. /* 函数:[4/24/2016 IT];
  249. /* 描述:;
  250. /* 参数:;
  251. /* [IN] :;
  252. /* [OUT] :;
  253. /* [IN/OUT] :;
  254. /* 返回:void;
  255. /* 注意:;
  256. /* 示例:;
  257. /*
  258. /* 修改:;
  259. /* 日期:;
  260. /* 内容:;
  261. /************************************************************************/
  262. int CPrinter::InitToPrint(TCHAR *PrinterName, int Copies )
  263. {
  264. if (m_pPrintDlg == NULL) // delete it in ~CPrinter()
  265. m_pPrintDlg = new CPrintDialog(FALSE,PD_DISABLEPRINTTOFILE);
  266. ASSERT(m_pPrintDlg != NULL);
  267. if (PrinterName == NULL) // which printer ist desired, and how much copies?
  268. {
  269. if (m_pPrintDlg->DoModal() == IDCANCEL)
  270. return -1;
  271. // 获取打印机设备上的DC值;
  272. m_hPrinter = m_pPrintDlg->GetPrinterDC();
  273. // 获取要打印的份数;
  274. m_nCopies = m_pPrintDlg->GetCopies();
  275. }
  276. else // a printer is given by name..
  277. {
  278. if (m_PrinterDC.CreateDC(NULL, PrinterName, NULL, NULL) == 0)
  279. {
  280. m_LastErrNo = PRERR_CANTCREATEPRINTERDC;
  281. return (-1); // failed, then set the num of Last Error
  282. }
  283. m_hPrinter = m_PrinterDC;
  284. m_nCopies = Copies;
  285. }
  286. if (m_DC.Attach(m_hPrinter) == 0)
  287. return -1;
  288. m_DC.m_bPrinting = TRUE;
  289. if (m_pPrintDlg)
  290. delete (m_pPrintDlg);
  291. m_pPrintDlg = NULL;
  292. LOGFONT log;
  293. log.lfHeight = CALCF(13); // 13号字体;
  294. log.lfWidth = 0;
  295. log.lfEscapement = 0;
  296. log.lfOrientation = 0;
  297. log.lfWeight = FW_REGULAR;
  298. log.lfItalic = false;
  299. log.lfUnderline = false;
  300. log.lfStrikeOut = 0;
  301. log.lfCharSet = ANSI_CHARSET;
  302. log.lfOutPrecision = OUT_DEFAULT_PRECIS;
  303. log.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  304. log.lfQuality = DEFAULT_QUALITY;
  305. log.lfPitchAndFamily = DEFAULT_PITCH || FF_ROMAN;
  306. _tcscpy_s(log.lfFaceName,_T("Arial"));
  307. AddFont(&log);
  308. m_iFont = 0; //set default font is index of 0
  309. return 0;
  310. }
  311. int CPrinter::StartPrint()
  312. {
  313. CString strTitle;
  314. // 打印文档对象;
  315. DOCINFO di;
  316. strTitle.LoadString(AFX_IDS_APP_TITLE);
  317. ::ZeroMemory(&di, sizeof (DOCINFO));
  318. di.cbSize = sizeof (DOCINFO);
  319. // 设置打印文档的名称;
  320. di.lpszDocName = strTitle;
  321. // 开始打印工作;
  322. if FAILED(m_DC.StartDoc(&di) == -1) return (-1);
  323. // 获取打印机打印区域的宽度和高度(像素);
  324. m_WorkSize.cx = m_DC.GetDeviceCaps(HORZRES);
  325. m_WorkSize.cy = m_DC.GetDeviceCaps(VERTRES);
  326. return 0;
  327. }
  328. int CPrinter::EndPrint()
  329. {
  330. m_DC.EndDoc(); // end a print job
  331. m_DC.Detach(); // detach the printer DC
  332. return 0;
  333. }
  334. /************************************************************************/
  335. /* 函数:SetMargins[4/24/2016 IT];
  336. /* 描述:设置页边距;
  337. /* 参数:;
  338. /* [IN] :;
  339. /* [OUT] :;
  340. /* [IN/OUT] :;
  341. /* 返回:void;
  342. /* 注意:;
  343. /* 示例:;
  344. /************************************************************************/
  345. int CPrinter::SetMargins(int Top, int Bottom, int Left, int Right)
  346. {
  347. if (Top > 0)
  348. m_Margins.top = Top;
  349. if (Bottom > 0)
  350. m_Margins.bottom = Bottom;
  351. if (Left > 0)
  352. m_Margins.left = Left;
  353. if (Right > 0)
  354. m_Margins.right = Right;
  355. m_yCur = CALCY(m_Margins.top);
  356. m_xCur = CALCX(m_Margins.left);
  357. return 0;
  358. }
  359. void CPrinter::SetDistance(int punkte)
  360. {
  361. m_Abstand = punkte;
  362. }
  363. int CPrinter::StartPage()
  364. {
  365. if (m_DC.StartPage() < 0)
  366. {
  367. m_LastErrNo = PRERR_STARTPAGEFAILED;
  368. return (-1);
  369. }
  370. m_yCur = CALCY(m_Margins.top);
  371. m_xCur = CALCX(m_Margins.left);
  372. return 0;
  373. }
  374. void CPrinter::EndPage()
  375. {
  376. m_DC.EndPage();
  377. }
  378. void CPrinter::NewPage()
  379. {
  380. m_DC.EndPage();
  381. m_DC.StartPage();
  382. m_yCur = CALCY(m_Margins.top);
  383. m_xCur = CALCX(m_Margins.left);
  384. }
  385. int CPrinter::GetWidth()
  386. {
  387. return MulDiv(m_WorkSize.cx, 72, GetDeviceCaps(m_DC.m_hDC, LOGPIXELSX));
  388. }
  389. int CPrinter::GetStrSize(TCHAR *str, CSize &size)
  390. {
  391. GetTextExtentPoint32(m_DC.GetSafeHdc(), str, _tcslen(str), &size);
  392. size.cx = MulDiv(size.cx, 72, GetDeviceCaps(m_DC.m_hDC, LOGPIXELSX));
  393. size.cy = MulDiv(size.cy, 72, GetDeviceCaps(m_DC.m_hDC, LOGPIXELSY));
  394. return 0;
  395. }
  396. void CPrinter::MoveTo(int xCur, int yCur)
  397. {
  398. m_xCur = CALCX(m_Margins.left + xCur);
  399. m_yCur = CALCY(m_Margins.right + yCur);
  400. }