MyPrint.cpp
上传用户:jzscgs158
上传日期:2022-05-25
资源大小:8709k
文件大小:5k
源码类别:

百货/超市行业

开发平台:

Visual C++

  1. #include "StdAfx.h"
  2. #include "MyPrint.h" 
  3. //构造函数
  4. CMyPrint::CMyPrint ()
  5. {
  6. m_CDC=new CDC();
  7. strDocName="打印";
  8. blnVal=FALSE;
  9. m_rcPrintMargin.left=25;
  10. m_rcPrintMargin.right=25;
  11. m_rcPrintMargin.top=40;
  12. m_rcPrintMargin.bottom=30;
  13. nXPixelsOfInch=300;
  14. nYPixelsOfInch=300;
  15. rcContent.left=m_rcPrintMargin.left;
  16. rcContent.top=m_rcPrintMargin.top;
  17. rcContent.right=600;
  18. rcContent.bottom=800;
  19. }
  20. //析构函数
  21. CMyPrint::~CMyPrint()
  22. {
  23. if (m_hDevNames != NULL)
  24.         GlobalFree(m_hDevNames);
  25. if (m_hDevMode != NULL)
  26.         GlobalFree(m_hDevMode);
  27. if(m_CDC->m_hDC!=NULL)
  28. {
  29. m_CDC->ReleaseOutputDC();
  30. m_CDC->~CDC();
  31. }
  32. }
  33. //释放内存
  34. void CMyPrint::FreeAllMemory()
  35. {
  36. if (m_hDevNames != NULL)
  37.         GlobalFree(m_hDevNames);
  38. if (m_hDevMode != NULL)
  39.         GlobalFree(m_hDevMode);
  40. if(m_CDC->m_hDC!=NULL)
  41. {
  42. m_CDC->ReleaseOutputDC();
  43. }
  44. }
  45. //返回CDC指针
  46. CDC* CMyPrint::GetMyCdc()
  47. {
  48. // 打印设置
  49. GetPrinterDeviceDefaults();
  50. // 设置默认打印方向为横向
  51. SetPrintOrientation(blnVal);
  52. PRINTDLG pd;
  53. memset(&pd, 0, sizeof(PRINTDLG));
  54. pd.lStructSize = sizeof(PRINTDLG);
  55. pd.Flags = PD_RETURNDC | PD_NOSELECTION;
  56. pd.hDC = NULL;
  57. pd.hwndOwner = NULL;
  58. pd.hInstance = NULL;
  59. pd.nMaxPage = 1;
  60. pd.nMinPage = 1;
  61. pd.nFromPage = 1;
  62. pd.nToPage = 1;
  63. pd.nCopies = 1;
  64. pd.hDevMode = NULL;
  65. pd.hDevNames = NULL;
  66. pd.hDevMode = m_hDevMode;
  67. //显示打印对话框,由用户来设定纸张大小等。
  68. if (!PrintDlg(&pd))
  69. return FALSE;
  70. ASSERT(pd.hDC != NULL);
  71. int nHorRes = GetDeviceCaps(pd.hDC, HORZRES); 
  72. int nVerRes = GetDeviceCaps(pd.hDC, VERTRES);
  73. nXPixelsOfInch = GetDeviceCaps(pd.hDC, LOGPIXELSX);
  74. nYPixelsOfInch = GetDeviceCaps(pd.hDC, LOGPIXELSY);
  75. // 计算页面可写面积
  76. rcContent.left=m_rcPrintMargin.left;
  77. rcContent.top=m_rcPrintMargin.top;
  78. rcContent.right=(long)(nHorRes*GetPrintProportionX()-m_rcPrintMargin.right);
  79. rcContent.bottom=(long)(nVerRes*GetPrintProportionY()-m_rcPrintMargin.bottom);
  80. // 开始打印
  81. DOCINFO di;
  82. di.cbSize = sizeof(DOCINFO);
  83. di.lpszDocName =strDocName; 
  84. di.lpszOutput = (LPTSTR) NULL; 
  85. di.lpszDatatype = (LPTSTR) NULL; 
  86. di.fwType = 0; 
  87. StartDoc(pd.hDC, &di);
  88. if(m_CDC->m_hDC!=NULL)
  89. {
  90. m_CDC->ReleaseOutputDC();
  91. }
  92. m_CDC->m_hDC=pd.hDC;
  93. return m_CDC;
  94. }
  95. //人为的获得打印机的驱动
  96. CDC* CMyPrint::GetManCdc(CString strDriver,CString strDevice, CString strOutput, CONST DEVMODE* lpInitData)
  97. {
  98. if(m_CDC->m_hDC!=NULL)
  99. {
  100. m_CDC->ReleaseOutputDC();
  101. }
  102. m_CDC->m_hDC=CreateDC((LPCSTR)strDriver,(LPCSTR)strDevice,(LPCSTR)strOutput,lpInitData);
  103. DOCINFO df={sizeof(DOCINFO),"打印",NULL};
  104. int nHorRes = GetDeviceCaps(m_CDC->m_hDC, HORZRES); 
  105. int nVerRes = GetDeviceCaps(m_CDC->m_hDC, VERTRES);
  106. nXPixelsOfInch = GetDeviceCaps(m_CDC->m_hDC, LOGPIXELSX);
  107. nYPixelsOfInch = GetDeviceCaps(m_CDC->m_hDC, LOGPIXELSY);
  108. // 计算页面可写面积
  109. rcContent.left=m_rcPrintMargin.left;
  110. rcContent.top=m_rcPrintMargin.top;
  111. rcContent.right=(long)(nHorRes*GetPrintProportionX()-m_rcPrintMargin.right);
  112. rcContent.bottom=(long)(nVerRes*GetPrintProportionY()-m_rcPrintMargin.bottom);
  113. m_CDC->StartDoc(&df);
  114. return m_CDC;
  115. }
  116. //得到系统的打印设备属性
  117. BOOL CMyPrint::GetPrinterDeviceDefaults(void)
  118. {
  119. PRINTDLG printDlg;
  120. if(AfxGetApp()->GetPrinterDeviceDefaults(&printDlg))
  121. {
  122. LPDEVNAMES pDevNames =
  123. (LPDEVNAMES)::GlobalLock(printDlg.hDevNames);
  124. LPDEVMODE  pDevMode  = (LPDEVMODE)::GlobalLock(printDlg.hDevMode);
  125. if (m_hDevNames != NULL)
  126. ::GlobalFree(m_hDevNames);
  127. if (m_hDevMode != NULL)
  128. ::GlobalFree(m_hDevMode);
  129. m_hDevNames = ::GlobalAlloc(GPTR,
  130. ::GlobalSize(printDlg.hDevNames));
  131. m_hDevMode = ::GlobalAlloc(GPTR, ::GlobalSize(printDlg.hDevMode));
  132. LPDEVNAMES pNewDevNames = (LPDEVNAMES)::GlobalLock(m_hDevNames);
  133. LPDEVMODE  pNewDevMode  = (LPDEVMODE)::GlobalLock(m_hDevMode);
  134. memcpy(pNewDevNames, pDevNames,
  135. (size_t)::GlobalSize(printDlg.hDevNames));
  136. memcpy(pNewDevMode, pDevMode,
  137. (size_t)::GlobalSize(printDlg.hDevMode));
  138. ::GlobalUnlock(printDlg.hDevNames);
  139. ::GlobalUnlock(printDlg.hDevMode);
  140. ::GlobalUnlock(m_hDevNames);
  141. ::GlobalUnlock(m_hDevMode);
  142. return TRUE;
  143. }
  144. else
  145. {
  146. return FALSE;
  147.     }
  148. }
  149. // 设置打印方向
  150. // TRUE:横向
  151. // FALSE:纵向
  152. BOOL CMyPrint::SetPrintOrientation(BOOL bVal)
  153. {
  154. if (m_hDevMode == NULL)
  155. return FALSE;
  156. LPDEVMODE  pNewDevMode  = (LPDEVMODE)::GlobalLock(m_hDevMode);
  157. if (bVal)
  158. pNewDevMode->dmOrientation = DMORIENT_LANDSCAPE; // 横向
  159. else
  160. pNewDevMode->dmOrientation = DMORIENT_PORTRAIT; // 纵向
  161. ::GlobalUnlock(m_hDevMode);
  162. return TRUE;
  163. }
  164. //开始打印
  165. void CMyPrint::DoPrint()
  166. {
  167. m_CDC->EndDoc();
  168. m_CDC->DeleteDC();
  169. }
  170. //得到像素向英寸转换的比例
  171. //像素:英寸=?:1
  172. double CMyPrint::GetPrintProportionX()
  173. {
  174. double pProportion;
  175. //分辨率 每英寸的象素点 1250
  176. pProportion=nXPixelsOfInch/1250.0;
  177. return pProportion;
  178. }
  179. //由像素转换成所需的英寸
  180. int CMyPrint::IntToRealIntX(int i)
  181. {
  182. int iTemp;
  183. iTemp=int(i*1.0/GetPrintProportionX());
  184. return iTemp;
  185. }
  186. double CMyPrint::GetPrintProportionY()
  187. {
  188. double pProportion;
  189. //分辨率 每英寸的象素点 1250
  190. pProportion=nYPixelsOfInch/1250.0;
  191. return pProportion;
  192. }
  193. //由像素转换成所需的英寸
  194. int CMyPrint::IntToRealIntY(int i)
  195. {
  196. int iTemp;
  197. iTemp=int(i*1.0/GetPrintProportionY());
  198. return iTemp;
  199. }