ZDlg.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // ZDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "BarCode.h"
  5. #include "ZDlg.h"
  6. #include "Printkernel.h"
  7. #include "Help.h"
  8. #include <Imm.h>
  9. #pragma comment(lib, "Imm32.lib")
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. /////////////////////////////////////////////////////////////////////////////
  16. ZDlg::ZDlg(CWnd* pParent /*=NULL*/)
  17. : CDialog(ZDlg::IDD, pParent)
  18. {
  19. //{{AFX_DATA_INIT(ZDlg)
  20. //}}AFX_DATA_INIT
  21. }
  22. ZDlg::~ZDlg()
  23. {
  24. }
  25. void ZDlg::DoDataExchange(CDataExchange* pDX)
  26. {
  27. CDialog::DoDataExchange(pDX);
  28. //{{AFX_DATA_MAP(ZDlg)
  29. DDX_Control(pDX, BTN_PRINT, btn_Menu);
  30. DDX_Control(pDX, ED_MOVE, ed_Move);
  31. DDX_Control(pDX, ED_PRINTY, ed_PrintY);
  32. DDX_Control(pDX, ED_PRINTX, ed_PrintX);
  33. DDX_Control(pDX, ED_PRINTH, ed_PrintH);
  34. DDX_Control(pDX, ED_PENW, ed_PenW);
  35. DDX_Control(pDX, CMB_STYLE, cmb_Style);
  36. DDX_Control(pDX, BTN_ONTOP, btn_OnTop);
  37. DDX_Control(pDX, CMB_MAPMODE, cmb_MapMode);
  38. DDX_Control(pDX, ED_CODE, ed_Code);
  39. //}}AFX_DATA_MAP
  40. }
  41. BEGIN_MESSAGE_MAP(ZDlg, CDialog)
  42. //{{AFX_MSG_MAP(ZDlg)
  43. ON_WM_PAINT()
  44. ON_BN_CLICKED(IDOK, On_IDOK)
  45. ON_BN_CLICKED(BTN_HELP, On_BtnHelp)
  46. ON_WM_DESTROY()
  47. ON_BN_CLICKED(BTN_CLOSE, On_BtnClose)
  48. ON_WM_LBUTTONDOWN()
  49. ON_BN_CLICKED(BTN_ONTOP, On_BtnOnTop)
  50. ON_CBN_SELCHANGE(CMB_STYLE, On_CmbStyleSelChange)
  51. ON_CBN_SELCHANGE(CMB_MAPMODE, On_CmbMapModeSelChange)
  52. ON_BN_CLICKED(BTN_PRINT, On_BtnPrint)
  53. ON_EN_CHANGE(ED_CODE, On_EdCodeChange)
  54. ON_EN_CHANGE(ED_PENW, On_EdPenWidthChange)
  55. //}}AFX_MSG_MAP
  56. END_MESSAGE_MAP()
  57. /////////////////////////////////////////////////////////////////////////////
  58. // ZDlg message handlers
  59. void ZDlg::On_IDOK()
  60. {
  61. }
  62. void ZDlg::On_BtnClose()
  63. {
  64. OnCancel();
  65. }
  66. void ZDlg::OnLButtonDown(UINT nFlags, CPoint point)
  67. {
  68. CDialog::OnLButtonDown(nFlags, point);
  69. SendMessage(WM_NCLBUTTONDOWN,HTCAPTION);
  70. }
  71. void ZDlg::On_BtnOnTop()
  72. {
  73. CString cs;
  74. btn_OnTop.GetWindowText(cs);
  75. if(cs.CompareNoCase("OnTop")==0) btn_OnTop.SetWindowText("OnNormal");
  76. else btn_OnTop.SetWindowText("OnTop");
  77. P_SetOnTop();
  78. }
  79. void ZDlg::P_SetOnTop()
  80. {
  81. CString cs;
  82. btn_OnTop.GetWindowText(cs);
  83. HWND hWnd=HWND_NOTOPMOST;
  84. if(cs.CompareNoCase("OnTop")!=0) hWnd=HWND_TOPMOST;
  85. ::SetWindowPos(m_hWnd,hWnd,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
  86. }
  87. void ZDlg::OnDestroy()
  88. {
  89. CDialog::OnDestroy();
  90. }
  91. int ZDlg::P_AddOrGet(CComboBox &box, BOOL bAdd)
  92. {
  93. struct IntString
  94. {
  95. int iV;
  96. char*psz;
  97. };
  98. IntString ps[]=
  99. {
  100. {MM_TEXT, "MM_TEXT"},
  101. {MM_LOENGLISH, "MM_LOENGLISH"},
  102. {MM_HIENGLISH, "MM_HIENGLISH"},
  103. {MM_LOMETRIC, "MM_LOMETRIC"},
  104. {MM_TWIPS, "MM_TWIPS"},
  105. };
  106. int i,iNum=sizeof(ps)/sizeof(ps[0]);
  107. if(bAdd==0)
  108. {
  109. i=box.GetCurSel();
  110. if((i<0)||(i>=iNum)) return ps[0].iV;
  111. return ps[i].iV;
  112. }
  113. for(i=0;i<iNum;i++)
  114. {
  115. box.AddString(ps[i].psz);
  116. }
  117. return 0;
  118. }
  119. BOOL ZDlg::OnInitDialog()
  120. {
  121. CDialog::OnInitDialog();
  122. SetWindowText("Using barcode library");
  123. P_AddOrGet(cmb_MapMode,1);
  124. char*pCode[]=
  125. {
  126. "Code39",
  127. "Code93",
  128. "Code128A",
  129. "Code128B",
  130. "Code128C",
  131. "CodeI2of5",
  132. "CodeEan13",
  133. };
  134. int i,iNum=sizeof(pCode)/sizeof(pCode[0]);
  135. for(i=0;i<iNum;i++)
  136. {
  137. cmb_Style.AddString(pCode[i]);
  138. }
  139. cmb_Style.SetCurSel(0);
  140. cmb_MapMode.SetCurSel(1);
  141. ed_Code.SetWindowText("0123456789");
  142. ed_PenW.SetWindowText("1");
  143. ed_PrintX.SetWindowText("100");
  144. ed_PrintY.SetWindowText("100");
  145. ed_PrintH.SetWindowText("100");
  146. /*
  147. HWND hIMCWND = NULL;
  148. HIMC hImc = ImmGetContext(hIMCWND);
  149. if(ImmGetOpenStatus(hImc))
  150. ImmSetOpenStatus(hImc, FALSE); // 输入法状态 TRUE..On / FALSE..Off
  151. ImmReleaseContext(hIMCWND, hImc);
  152. */
  153. return 1;
  154. }
  155. void ZDlg::On_BtnHelp()
  156. {
  157. char sz[1024];
  158. ::GetModuleFileName(0,sz,1020);
  159. char*p=strrchr(sz,'\\');
  160. p++;
  161. *p=0;
  162. strcat(sz,"help.htm");
  163. ::ShellExecute(0,0,sz,0,0,SW_SHOW);
  164. }
  165. void ZDlg::On_CmbStyleSelChange()
  166. {
  167. RedrawWindow();
  168. }
  169. void ZDlg::On_CmbMapModeSelChange()
  170. {
  171. RedrawWindow();
  172. }
  173. void ZDlg::On_EdCodeChange()
  174. {
  175. RedrawWindow();
  176. }
  177. void ZDlg::On_EdPenWidthChange()
  178. {
  179. RedrawWindow();
  180. }
  181. void ZDlg::OnPaint()
  182. {
  183. CPaintDC dc(this);
  184. P_DrawBarCode(&dc,0);
  185. }
  186. void ZDlg::On_BtnPrint() // 打印;
  187. {
  188. #if 0
  189. CPrintDialog cp(0);
  190. if(cp.DoModal()!=IDOK) return;
  191. HDC hDC=cp.GetPrinterDC();
  192. CDC*pDC=CDC::FromHandle(hDC);
  193. P_DrawBarCode(pDC,1);
  194. #else
  195. CPrinter pter;
  196. pter.InitToPrint(NULL);
  197. pter.SetMargins(0,0,0,0);
  198. pter.StartPrint();
  199. pter.StartPage();
  200. CRect rcBar(2,2,60,60);
  201. pter.DrawBarCode(_T("A11000234545467"), rcBar);
  202. LOGFONT log;
  203. log.lfHeight = -MulDiv(5, GetDeviceCaps(pter.GetHDC(), LOGPIXELSY), 72);
  204. log.lfWidth = 0;
  205. log.lfEscapement = 0;
  206. log.lfOrientation = 0;
  207. log.lfWeight = FW_REGULAR;
  208. log.lfItalic = false;
  209. log.lfUnderline = false;
  210. log.lfStrikeOut = 0;
  211. log.lfCharSet = ANSI_CHARSET;
  212. log.lfOutPrecision = OUT_DEFAULT_PRECIS;
  213. log.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  214. log.lfQuality = DEFAULT_QUALITY;
  215. log.lfPitchAndFamily = DEFAULT_PITCH || FF_ROMAN;
  216. _tcscpy_s(log.lfFaceName,_T("Arial"));
  217. rcBar.top += 55;
  218. rcBar.right = rcBar.left + 950;
  219. rcBar.bottom = rcBar.top + 50;
  220. pter.DrawTextEx(_T("订单号:20160402-001"), rcBar, log, FORMAT_NORMAL);
  221. rcBar.top += 55;
  222. rcBar.bottom = rcBar.top + 50;
  223. pter.DrawTextEx(_T("客人:王五、李四"), rcBar, log, FORMAT_NORMAL);
  224. rcBar.top += 55;
  225. rcBar.bottom = rcBar.top + 50;
  226. pter.DrawTextEx(_T("商品:7寸水晶摆台"), rcBar, log, FORMAT_NORMAL);
  227. pter.EndPage();
  228. pter.EndPrint();
  229. #endif
  230. }
  231. void ZDlg::P_DrawBarCode(CDC*pDC,const BOOL bPrint)
  232. {
  233. const int iSel=cmb_Style.GetCurSel();
  234. if(iSel<0) return;
  235. //--------------------------------------------
  236. int iL,iT,iB;
  237. const int iMapMode=pDC->SetMapMode(P_AddOrGet(cmb_MapMode,0));
  238. {
  239. CRect rcT;
  240. if(bPrint)
  241. {
  242. rcT.left =Help::GetValue(ed_PrintX);
  243. rcT.top =Help::GetValue(ed_PrintY);
  244. rcT.bottom =rcT.top+Help::GetValue(ed_PrintH);
  245. //temp, useless value
  246. rcT.right=rcT.left+100;
  247. }
  248. else
  249. {
  250. GetWindowRect(&rcT);
  251. ::MapWindowPoints(0,m_hWnd,(POINT*)&rcT,2);
  252. rcT.left+=4;
  253. rcT.bottom+=30;
  254. }
  255. pDC->DPtoLP(&rcT);
  256. iL=rcT.left + 20;
  257. iT=rcT.top - 80;
  258. iB=rcT.bottom + 80;
  259. }
  260. //--------------------------------------------
  261. int iPenW=Help::GetValue(ed_PenW);
  262. if(iPenW==0) iPenW=1;
  263. CString csStyle;
  264. cmb_Style.GetWindowText(csStyle);
  265. CString csCode;
  266. ed_Code.GetWindowText(csCode);
  267. COLORREF clrBar =RGB(0,0,0);
  268. COLORREF clrSpace =RGB(255,255,255);
  269. if(bPrint)
  270. {
  271. pDC->StartDoc("Hello");
  272. pDC->StartPage();
  273. }
  274. if(csStyle.CompareNoCase("Code39")==0)
  275. {
  276. Barcode39 code;
  277. code.Encode39(csCode);
  278. P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code);
  279. }
  280. else
  281. if(csStyle.CompareNoCase("Code93")==0)
  282. {
  283. Barcode93 code;
  284. code.Encode93(csCode);
  285. P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code);
  286. }
  287. else
  288. if(csStyle.CompareNoCase("CodeI2of5")==0)
  289. {
  290. BarcodeI2of5 code;
  291. code.EncodeI2of5(csCode);
  292. P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code);
  293. }
  294. else
  295. if(csStyle.CompareNoCase("Code128A")==0)
  296. {
  297. Barcode128 code;
  298. code.Encode128A(csCode);
  299. P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code);
  300. }
  301. else
  302. if(csStyle.CompareNoCase("Code128B")==0)
  303. {
  304. Barcode128 code;
  305. code.Encode128B(csCode);
  306. P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code);
  307. }
  308. else
  309. if(csStyle.CompareNoCase("Code128C")==0)
  310. {
  311. Barcode128 code;
  312. code.Encode128C(csCode);
  313. P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code);
  314. }
  315. else
  316. if(csStyle.CompareNoCase("CodeEan13")==0)
  317. {
  318. BarcodeEan13 code;
  319. code.EncodeEan13(csCode);
  320. P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code);
  321. }
  322. if(bPrint)
  323. {
  324. pDC->EndPage();
  325. pDC->EndDoc();
  326. }
  327. pDC->SetMapMode(iMapMode);
  328. }
  329. void ZDlg::P_DrawBarcode(CDC*pDC,int iX,int iY0,int iY10,int iY11,COLORREF clrBar,COLORREF clrSpace,int iPenW,BarcodeBase*pBc)
  330. {
  331. pBc->DrawBarcode(pDC->m_hDC,iX,iY0,iY10,iY11,clrBar,clrSpace,iPenW);
  332. }
  333. LRESULT ZDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  334. {
  335. CRect rc;
  336. if(message==WM_USER)
  337. {
  338. GetWindowRect(&rc);
  339. rc.left+=wParam;
  340. rc.right+=wParam;
  341. MoveWindow(&rc);
  342. }
  343. else
  344. if(message==WM_USER+1)
  345. {
  346. GetWindowRect(&rc);
  347. rc.top+=wParam;
  348. rc.bottom+=wParam;
  349. MoveWindow(&rc);
  350. }
  351. return CDialog::WindowProc(message, wParam, lParam);
  352. }