PrintRX.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. ///////////////////////////////////////////////////////////////////////
  2. // PrintRX.cpp: implementation of the CPrintRX class. //
  3. // It is written by Robin-Fox, (2004-03-03)it is free to use; //
  4. // I am glad it will give you some help in learning do printing. //
  5. // and i will also be glad that you make it works better though your //
  6. // rewrite.let the open sourse project make the world better. //
  7. // ----in BeiJing //
  8. ///////////////////////////////////////////////////////////////////////
  9. #include "stdafx.h"
  10. #include "YLGL.h"
  11. #include "PrintRX.h"
  12. #include "Barcode.h"
  13. #ifdef _DEBUG
  14. #undef THIS_FILE
  15. static char THIS_FILE[]=__FILE__;
  16. #define new DEBUG_NEW
  17. #endif
  18. //////////////////////////////////////////////////////////////////////
  19. /*
  20. Procedure for use :
  21. =========================================================================
  22. 1. Declare an object of CPrintRX.
  23. 2. Call the member-function InitToPrint() to choose a Printer and initialize the object.
  24. 3. Call the member-function StartPrint(). This function MUST be called immediately
  25. after InitToPrint(), before other class-functions are be called. It creats the
  26. print-document in the printer-spooler (at the end EndPrint() will be called, in
  27. order to release it).
  28. 4. Add the fonts, which are needed, with the member-function AddFont().
  29. Principle it is enough to generate one font. The function returns a handle of the
  30. type int, which is then needed from various other member-functions. For
  31. each necessary font the function must be called once. There are only 10 different
  32. fonts possible.After this function you should call SetFontFace() then.
  33. Default is m_Fonts[0][0].
  34. 5. Adjust the margins and the line-space with the member-funktions SetMargins() and
  35. SetDistance(). MoveTo() set the point of the cursor aginst of m_Margnis.
  36. 6. MoveTo() set the point of the cursor aginst of m_Margnis.
  37. 7. Call member-function StartPage(), in order to begin a new page. If the page ends
  38. call EndPage(). Should it be necessary to print more lines between the call of
  39. these two functions, than on one page fits, the class ensures that that EndPage()
  40. is called followed by a StartPage() automaticaly. A possibly necessary head- and
  41. footingline are created.
  42. 8. Lines are printed with the function DrawHLine() and DrwawVLine().
  43. etc.
  44. 9. Call the member-funktion EndPrint(), in order to release the document in the
  45. spooler.
  46. 10. there are some thing to do to better it, Draw BItamp, etc
  47. */
  48. // Construction/Destruction
  49. //////////////////////////////////////////////////////////////////////
  50. CPrintRX::CPrintRX()
  51. {
  52. for (int i=0; i<10; i++)
  53. {
  54. m_iFace[i] = 0;
  55. for (int j=0; j<4; j++)
  56. m_font[i][j] = new (CFont);
  57. }
  58. m_nFonts = 0;
  59. m_iFont = 0;
  60. m_pPrintDlg = NULL;
  61. m_Margins.SetRect(0, 0, 0, 0);
  62. m_yCur = 0;
  63. m_xCur = 0;
  64. m_iPage = 1;
  65. m_nCopies = 1;
  66. }
  67. CPrintRX::~CPrintRX()
  68. {
  69. deletePrinter();
  70. }
  71. void CPrintRX::deletePrinter() // Jeff.add;
  72. {
  73. for (int i=0; i<10; i++)
  74. for (int j=0; j<4; j++)
  75. delete (m_font[i][j]);
  76. if (m_pPrintDlg)
  77. delete (m_pPrintDlg);
  78. if(m_newfont.GetSafeHandle())
  79. m_newfont.DeleteObject ();
  80. }
  81. int CPrintRX::DrawText(char *str, int iFont, int iFace, int format)
  82. {
  83. m_DC.TextOut(m_xCur, m_yCur, str);
  84. m_memdc.TextOut(m_xCur, m_yCur, str);
  85. return 1;
  86. }
  87. void DrawPrintBit(HDC hdc, Image *img, CRect rc)
  88. {
  89. try
  90. {
  91. if(img)
  92. {
  93. Graphics graph(hdc);
  94. // SetPageUnit设置graphics对象的页面坐标系测量单位;
  95. // SetPageUnit将页面坐标系转换为设备坐标系;
  96. graph.SetPageUnit(UnitPixel); // UnitPixel每个页面单位为1个像素,此时页面单位与设备单位相同;
  97. // SetSmoothingMode设置graphics对象的渲染质量;SmoothingModeHighQuality平滑模式高质量;
  98. graph.SetSmoothingMode(SmoothingModeHighQuality);
  99. int width=img->GetWidth();
  100. int height=img->GetHeight();
  101. Rect destinationRect(rc.left , rc.top , rc.Width (), rc.Height ());
  102. graph.DrawImage(img, destinationRect, 0,0,width,height,UnitPixel);
  103. }
  104. }
  105. catch(...)
  106. {
  107. }
  108. }
  109. void CPrintRX::SetColor(COLORREF col, COLORREF col2)
  110. {
  111. if(col==0 && col2==0)
  112. {
  113. m_DC.SetTextColor (col);
  114. m_DC.SetBkColor (0xffffff);
  115. m_memdc.SetTextColor (col);
  116. m_memdc.SetBkColor (0xffffff);
  117. }
  118. else
  119. {
  120. m_DC.SetTextColor (col);
  121. m_DC.SetBkColor (col2);
  122. m_memdc.SetTextColor (col);
  123. m_memdc.SetBkColor (col2);
  124. }
  125. }
  126. void CPrintRX::DrawImage(Image *pImg, CRect& m_rt)
  127. {
  128. DrawPrintBit( m_DC.GetSafeHdc (), pImg , CRect(CALCX(m_rt.left),CALCY(m_rt.top),CALCX(m_rt.right),CALCY(m_rt.bottom) ));
  129. DrawPrintBit( m_memdc.GetSafeHdc (), pImg , CRect(CALCX(m_rt.left),CALCY(m_rt.top),CALCX(m_rt.right),CALCY(m_rt.bottom) ));
  130. }
  131. void CPrintRX::DrawRect(CRect& m_rt, int col)
  132. {
  133. m_DC.FillSolidRect (CRect(CALCX(m_rt.left),CALCY(m_rt.top),CALCX(m_rt.right),CALCY(m_rt.bottom) ), col);
  134. m_memdc.FillSolidRect (CRect(CALCX(m_rt.left),CALCY(m_rt.top),CALCX(m_rt.right),CALCY(m_rt.bottom) ), col);
  135. }
  136. int CPrintRX::DrawText(char *str, CRect& m_rt, int iFont, int iFace, int format)
  137. {
  138. SIZE Size;
  139. GetTextExtentPoint32(m_DC.GetSafeHdc(), str, strlen(str), &Size);
  140. int left, top;
  141. if (format & FORMAT_HCENTER) // 水平居中;
  142. {
  143. left = CALCX(m_rt.left) + CALCX(m_rt.Width())/2 - Size.cx/2;
  144. if (left < CALCX(m_rt.left))
  145. left = CALCX(m_rt.left);
  146. }
  147. else if(format & FORMAT_RIGHT) // 向右对齐;
  148. {
  149. left = CALCX(m_rt.left) + CALCX(m_rt.Width()) - Size.cx;
  150. if (left < CALCX(m_rt.left))
  151. left = CALCX(m_rt.left);
  152. }
  153. else // 向左对齐;
  154. left = CALCX(m_rt.left);
  155. if (format & FORMAT_VCENTER) // 垂直居中
  156. {
  157. top = CALCY(m_rt.top) + CALCY(m_rt.Height())/2 - Size.cy/2;
  158. if (top < CALCY(m_rt.top))
  159. top = CALCY(m_rt.top);
  160. }
  161. else top = CALCY(m_rt.top);
  162. m_DC.TextOut(left, top, str);
  163. m_memdc.TextOut(left, top, str);
  164. return 1;
  165. }
  166. int CPrintRX::DrawHLine(int x_left, int y_left, int x_right, int y_right)
  167. {
  168. m_DC.MoveTo(CALCX(m_Margins.left + x_left), CALCY(m_Margins.top + y_left));
  169. m_DC.LineTo(CALCX(m_Margins.left + x_right), CALCY(m_Margins.top + y_right));
  170. m_memdc.MoveTo(CALCX(m_Margins.left + x_left), CALCY(m_Margins.top + y_left));
  171. m_memdc.LineTo(CALCX(m_Margins.left + x_right), CALCY(m_Margins.top + y_right));
  172. return 0;
  173. }
  174. int CPrintRX::DrawHLine(int x_left, int y_left, int x_right, int y_right, CPen &newpen)
  175. {
  176. CPen* oldPen = m_DC.SelectObject(&newpen);
  177. m_DC.MoveTo(CALCX(m_Margins.left + x_left), CALCY(m_Margins.top + y_left));
  178. m_DC.LineTo(CALCX(m_Margins.left + x_right), CALCY(m_Margins.top + y_right));
  179. m_DC.SelectObject(oldPen);
  180. oldPen = m_memdc.SelectObject(&newpen);
  181. m_memdc.MoveTo(CALCX(m_Margins.left + x_left), CALCY(m_Margins.top + y_left));
  182. m_memdc.LineTo(CALCX(m_Margins.left + x_right), CALCY(m_Margins.top + y_right));
  183. m_memdc.SelectObject(oldPen);
  184. return 0;
  185. }
  186. void CPrintRX::DrawRect2(CRect &rc, int mode)
  187. {
  188. if(mode==0)return;
  189. else if(mode==1)
  190. {
  191. DrawHLine(rc.left , rc.top , rc.right , rc.top );
  192. DrawHLine(rc.left , rc.bottom , rc.right , rc.bottom );
  193. DrawVLine(rc.left , rc.top , rc.left , rc.bottom );
  194. DrawVLine(rc.right , rc.top , rc.right , rc.bottom );
  195. }
  196. else if(mode==2)
  197. {
  198. DrawHLine(rc.left , rc.bottom , rc.right , rc.bottom );
  199. }
  200. }
  201. int CPrintRX::DrawVLine(int x_up, int y_up, int x_bottom, int y_bottom)
  202. {
  203. m_DC.MoveTo(CALCX(m_Margins.left + x_up), CALCY(m_Margins.top + y_up));
  204. m_DC.LineTo(CALCX(m_Margins.left + x_bottom), CALCY(m_Margins.top + y_bottom));
  205. m_memdc.MoveTo(CALCX(m_Margins.left + x_up), CALCY(m_Margins.top + y_up));
  206. m_memdc.LineTo(CALCX(m_Margins.left + x_bottom), CALCY(m_Margins.top + y_bottom));
  207. return 0;
  208. }
  209. int CPrintRX::DrawVLine(int x_up, int y_up, int x_bottom, int y_bottom, CPen &newpen)
  210. {
  211. CPen* oldPen = m_DC.SelectObject(&newpen);
  212. m_DC.MoveTo(CALCX(m_Margins.left + x_up), CALCY(m_Margins.top + y_up));
  213. m_DC.LineTo(CALCX(m_Margins.left + x_bottom), CALCY(m_Margins.top + y_bottom));
  214. m_DC.SelectObject(oldPen);
  215. oldPen = m_memdc.SelectObject(&newpen);
  216. m_memdc.MoveTo(CALCX(m_Margins.left + x_up), CALCY(m_Margins.top + y_up));
  217. m_memdc.LineTo(CALCX(m_Margins.left + x_bottom), CALCY(m_Margins.top + y_bottom));
  218. m_memdc.SelectObject(oldPen);
  219. return 0;
  220. }
  221. int CPrintRX::DrawBarCode(IN LPCTSTR lpOrderNum, IN LPCTSTR lpCustomer1, IN LPCTSTR lpCustomer2, IN LPCTSTR lpProduct, IN LPCTSTR lpAutoId)
  222. {
  223. CString strtemp = _T("");
  224. strtemp.Format(_T("#%s"), lpAutoId);
  225. printf("--------------\n\t条码:%s\n",strtemp);
  226. Barcode128 code;
  227. code.SetRatio(1); // Code128B
  228. code.Encode128B(strtemp);
  229. COLORREF clrBar = RGB(0,0,0);
  230. COLORREF clrSpace = RGB(255,255,255);
  231. int nStartX; // 打印区域的 x 坐标起始位置;
  232. int nStartY; // 打印区域的 y 坐标起始位置;
  233. int nBarHeight; // 打印条码的高度;
  234. int nBarHeight2; // 打印条码的高度2;
  235. int nPenWidth; // 打印条码的宽度;
  236. int nLineSpace; // 行间距;
  237. #if 1 // 30*20mm的卷标纸;
  238. nStartX = 5;
  239. nStartY = 5;
  240. nBarHeight = 40;
  241. nBarHeight2 = 40;
  242. nPenWidth = 2;
  243. nLineSpace = 3;
  244. #endif
  245. #if 0 // 40*50mm的卷标纸;
  246. nStartX = 2;
  247. nStartY = 2;
  248. nBarHeight = 40;
  249. nBarHeight2 = 40;
  250. nPenWidth = 2;
  251. nLineSpace= 20;
  252. #endif
  253. code.DrawBarcode(m_hPrinter, nStartX, nStartY, nBarHeight, nBarHeight2, clrBar, clrSpace, nPenWidth);
  254. LOGFONT log;
  255. // 设置为6号字体;
  256. log.lfHeight = -MulDiv(6, GetDeviceCaps(m_hPrinter, LOGPIXELSY), 72);
  257. log.lfWidth = 0;
  258. log.lfEscapement = 0;
  259. log.lfOrientation = 0;
  260. log.lfWeight = FW_REGULAR;
  261. log.lfItalic = false;
  262. log.lfUnderline = false;
  263. log.lfStrikeOut = 0;
  264. log.lfCharSet = ANSI_CHARSET;
  265. log.lfOutPrecision = OUT_DEFAULT_PRECIS;
  266. log.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  267. log.lfQuality = DEFAULT_QUALITY;
  268. log.lfPitchAndFamily = DEFAULT_PITCH || FF_ROMAN;
  269. strcpy(log.lfFaceName,_T("Arial"));
  270. CFont cf;
  271. cf.CreateFontIndirect(&log);
  272. CFont *oldf = m_DC.SelectObject(&cf);
  273. CRect rt;
  274. #if 1 // 30*20mm的卷标纸;
  275. rt.left = nStartX;
  276. rt.top = nBarHeight + 5;
  277. rt.right = 300 - nStartX;
  278. rt.bottom = 200;
  279. #endif
  280. #if 0 // 50*40mm的卷标纸;
  281. rt.left = 2;
  282. rt.top = 45;
  283. rt.right = 450;
  284. rt.bottom = 100;
  285. #endif
  286. // 打印AutoId;
  287. strtemp.Format(_T("#%s"),lpAutoId);
  288. SIZE Size;
  289. GetTextExtentPoint32(m_DC.GetSafeHdc(), strtemp, strlen(strtemp), &Size);
  290. m_DC.TextOut(rt.left, rt.top, strtemp);
  291. m_memdc.TextOut(rt.left, rt.top, strtemp);
  292. // 打印订单号;
  293. rt.top += Size.cy + nLineSpace;
  294. rt.bottom = rt.top + Size.cy;
  295. strtemp.Format(_T("%s"),lpOrderNum);
  296. GetTextExtentPoint32(m_DC.GetSafeHdc(), strtemp, strlen(strtemp), &Size);
  297. m_DC.TextOut(rt.left, rt.top, strtemp);
  298. m_memdc.TextOut(rt.left, rt.top, strtemp);
  299. // 打印顾客信息;
  300. rt.top += Size.cy + nLineSpace;
  301. rt.bottom = rt.top + Size.cy;
  302. strtemp.Format(_T("%s,%s"),lpCustomer1, lpCustomer2);
  303. GetTextExtentPoint32(m_DC.GetSafeHdc(), strtemp, strlen(strtemp), &Size);
  304. m_DC.TextOut(rt.left, rt.top, strtemp);
  305. m_memdc.TextOut(rt.left, rt.top, strtemp);
  306. // 打印商品名;
  307. rt.top += Size.cy + nLineSpace;
  308. rt.bottom = rt.top + Size.cy;
  309. strtemp.Format(_T("%s"),lpProduct);
  310. GetTextExtentPoint32(m_DC.GetSafeHdc(), strtemp, strlen(strtemp), &Size);
  311. m_DC.TextOut(rt.left, rt.top, strtemp);
  312. m_memdc.TextOut(rt.left, rt.top, strtemp);
  313. return 0;
  314. }
  315. int CPrintRX::AddFont(CFont &newfont)
  316. {
  317. return -1;
  318. }
  319. int CPrintRX::AddFont(LOGFONT *lf)
  320. {
  321. if (m_nFonts == 10)
  322. return -1;
  323. m_font[m_nFonts][FACE_NORMAL]->CreateFontIndirect(lf);
  324. lf->lfWeight = FW_BLACK;
  325. m_font[m_nFonts][FACE_NORMALBOLD]->CreateFontIndirect(lf);
  326. lf->lfWeight = FW_REGULAR;
  327. lf->lfHeight = CALCF(25);
  328. m_font[m_nFonts][FACE_BIG]->CreateFontIndirect(lf);
  329. lf->lfWeight = FW_BLACK;
  330. m_font[m_nFonts][FACE_BIGBOLD]->CreateFontIndirect(lf);
  331. m_nFonts ++;
  332. return 0;
  333. }
  334. int CPrintRX::SetFontFace(int iFont, BOOL bWeight, CString name)
  335. {
  336. LOGFONT log;
  337. log.lfHeight = CALCF(iFont); //add a kind of fond,let height equles 13
  338. log.lfWidth = 0;
  339. log.lfEscapement = 0;
  340. log.lfOrientation = 0;
  341. if(bWeight)
  342. log.lfWeight = FW_BLACK;
  343. else
  344. log.lfWeight = FW_REGULAR;
  345. log.lfItalic = false;
  346. log.lfUnderline = false;
  347. log.lfStrikeOut = 0;
  348. log.lfCharSet = ANSI_CHARSET;
  349. log.lfOutPrecision = OUT_DEFAULT_PRECIS;
  350. log.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  351. log.lfQuality = DEFAULT_QUALITY;
  352. log.lfPitchAndFamily = DEFAULT_PITCH || FF_ROMAN;
  353. strcpy (log.lfFaceName,name);
  354. if(m_newfont.GetSafeHandle ())
  355. m_newfont.DeleteObject ();
  356. m_newfont.CreateFontIndirect(&log);
  357. m_DC.SelectObject(&m_newfont);
  358. m_memdc.SelectObject(&m_newfont);
  359. return 0;
  360. }
  361. int CPrintRX::InitToPrint(char *PrinterName /*= NULL*/, int Copies/* = 1*/,const BOOL bNoShowDlg)
  362. {
  363. if (m_pPrintDlg == NULL) // delete it in ~CPrintRX()
  364. // m_pPrintDlg = new CPrintDialog(FALSE,PD_DISABLEPRINTTOFILE);
  365. m_pPrintDlg = new CPrintDialog(FALSE,PD_ALLPAGES|PD_NOPAGENUMS,NULL);
  366. //CPrintDialog printDialog(false,PD_ALLPAGES|PD_NOPAGENUMS,NULL);
  367. ASSERT(m_pPrintDlg != NULL);
  368. if (PrinterName == NULL) // which printer ist desired, and how much copies?
  369. {
  370. // if (m_pPrintDlg->DoModal() == IDCANCEL)
  371. //return (-1);
  372. if ( bNoShowDlg == FALSE )
  373. {
  374. if(!m_pPrintDlg->GetDefaults())
  375. {
  376. return -1;
  377. }
  378. m_hPrinter = m_pPrintDlg->GetPrinterDC();
  379. m_nCopies = m_pPrintDlg->GetCopies();
  380. }
  381. //else // 按默认方式打印;
  382. //{
  383. // //m_pPrintDlg->
  384. // m_hPrinter = m_pPrintDlg->GetPrinterDC();
  385. // m_nCopies = m_pPrintDlg->GetCopies();
  386. //}
  387. }
  388. else // a printer is given by name..
  389. {
  390. if (m_PrinterDC.CreateDC(NULL, PrinterName, NULL, NULL) == 0)
  391. {
  392. m_LastErrNo = PRERR_CANTCREATEPRINTERDC;
  393. return (-1); // failed, then set the num of Last Error
  394. }
  395. m_hPrinter = m_PrinterDC;
  396. m_nCopies = Copies;
  397. }
  398. if (m_DC.Attach(m_hPrinter) == 0)
  399. return (-1);
  400. m_DC.m_bPrinting = TRUE;
  401. if (m_pPrintDlg)
  402. delete (m_pPrintDlg);
  403. m_pPrintDlg = NULL;
  404. LOGFONT log;
  405. log.lfHeight = CALCF(4); //add a kind of fond,let height equles 13
  406. // log.lfHeight = CALCF(13); //add a kind of fond,let height equles 13
  407. log.lfWidth = 0;
  408. log.lfEscapement = 0;
  409. log.lfOrientation = 0;
  410. log.lfWeight = FW_REGULAR;
  411. log.lfItalic = false;
  412. log.lfUnderline = false;
  413. log.lfStrikeOut = 0;
  414. log.lfCharSet = ANSI_CHARSET;
  415. log.lfOutPrecision = OUT_DEFAULT_PRECIS;
  416. log.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  417. log.lfQuality = DEFAULT_QUALITY;
  418. log.lfPitchAndFamily = DEFAULT_PITCH || FF_ROMAN;
  419. strcpy (log.lfFaceName,"Arial");
  420. AddFont(&log);
  421. m_iFont = 0; //set default font is index of 0
  422. if(m_bmp.m_hObject )
  423. {
  424. m_bmp.DeleteObject ();
  425. }
  426. if(m_memdc.m_hDC==NULL)
  427. m_memdc.CreateCompatibleDC (NULL);
  428. int width= m_DC.GetDeviceCaps(HORZRES);
  429. int height= m_DC.GetDeviceCaps(VERTRES);
  430. m_bmp.CreateCompatibleBitmap (&m_DC, width, height);
  431. m_memdc.SelectObject (&m_bmp);
  432. m_memdc.FillSolidRect (0,0,width, height,0xffffff);
  433. return 0;
  434. }
  435. int CPrintRX::StartPrint()
  436. {
  437. CString strTitle; // Get the application title
  438. DOCINFO di; // Initialise print document details
  439. strTitle.LoadString(AFX_IDS_APP_TITLE);
  440. ::ZeroMemory(&di, sizeof (DOCINFO));
  441. di.cbSize = sizeof (DOCINFO);
  442. di.lpszDocName = strTitle;
  443. // Begin a new print job
  444. //"winerror.h" #define FAILED(Status) ((HRESULT)(Status)<0)
  445. if FAILED(m_DC.StartDoc(&di) == -1) return (-1);
  446. // Get the printing extents and store in the m_DimDraw fields
  447. m_WorkSize.cx = m_DC.GetDeviceCaps(HORZRES);
  448. m_WorkSize.cy = m_DC.GetDeviceCaps(VERTRES);
  449. return 0;
  450. }
  451. int CPrintRX::EndPrint()
  452. {
  453. m_DC.EndDoc(); // end a print job
  454. m_DC.Detach(); // detach the printer DC
  455. return 0;
  456. }
  457. int CPrintRX::SetMargins(int Top, int Bottom, int Left, int Right)
  458. {
  459. if (Top > 0)
  460. m_Margins.top = Top;
  461. if (Bottom > 0)
  462. m_Margins.bottom = Bottom;
  463. if (Left > 0)
  464. m_Margins.left = Left;
  465. if (Right > 0)
  466. m_Margins.right = Right;
  467. m_yCur = CALCY(m_Margins.top);
  468. m_xCur = CALCX(m_Margins.left);
  469. return 0;
  470. }
  471. void CPrintRX::SetDistance(int punkte)
  472. {
  473. m_Abstand = punkte;
  474. }
  475. int CPrintRX::StartPage()
  476. {
  477. if (m_DC.StartPage() < 0)
  478. {
  479. m_LastErrNo = PRERR_STARTPAGEFAILED;
  480. return (-1);
  481. }
  482. m_yCur = CALCY(m_Margins.top);
  483. m_xCur = CALCX(m_Margins.left);
  484. return 0;
  485. }
  486. void CPrintRX::EndPage()
  487. {
  488. /* DEVMODE* pDevMode;
  489. pDevMode=(DEVMODE*)m_pPrintDlg->GetDevMode();
  490. pDevMode->dmCopies =2;
  491. m_DC.ResetDC(pDevMode);
  492. */
  493. m_DC.EndPage();
  494. }
  495. void CPrintRX::NewPage()
  496. {
  497. m_DC.EndPage();
  498. m_DC.StartPage();
  499. m_yCur = CALCY(m_Margins.top);
  500. m_xCur = CALCX(m_Margins.left);
  501. }
  502. int CPrintRX::GetWidth()
  503. {
  504. return MulDiv(m_WorkSize.cx, 72, GetDeviceCaps(m_DC.m_hDC, LOGPIXELSX));
  505. }
  506. int CPrintRX::GetStrSize(char *str, CSize &size)
  507. {
  508. GetTextExtentPoint32(m_DC.GetSafeHdc(), str, strlen(str), &size);
  509. size.cx = MulDiv(size.cx, 72, GetDeviceCaps(m_DC.m_hDC, LOGPIXELSX));
  510. size.cy = MulDiv(size.cy, 72, GetDeviceCaps(m_DC.m_hDC, LOGPIXELSY));
  511. return 0;
  512. }
  513. void CPrintRX::MoveTo(int xCur, int yCur)
  514. {
  515. m_xCur = CALCX(m_Margins.left + xCur);
  516. m_yCur = CALCY(m_Margins.right + yCur);
  517. }
  518. void CPrintRX::CalcuRc(CRect &rc)
  519. {
  520. rc.left =CALCX(rc.left);
  521. rc.right =CALCX(rc.right);
  522. rc.top =CALCY(rc.top );
  523. rc.bottom =CALCY(rc.bottom );
  524. }
  525. CDC * CPrintRX::GetPDC()
  526. {
  527. return &m_memdc;
  528. }
  529. void CPrintRX::GetWidHei(int &w, int &h)
  530. {
  531. BITMAP bm;
  532. m_bmp.GetBitmap (&bm);
  533. w=bm.bmWidth ;
  534. h=bm.bmHeight ;
  535. }