| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- // ZDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "BarCode.h"
- #include "ZDlg.h"
- #include "Printkernel.h"
- #include "Help.h"
- #include <Imm.h>
- #pragma comment(lib, "Imm32.lib")
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- ZDlg::ZDlg(CWnd* pParent /*=NULL*/)
- : CDialog(ZDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(ZDlg)
- //}}AFX_DATA_INIT
- }
- ZDlg::~ZDlg()
- {
- }
- void ZDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(ZDlg)
- DDX_Control(pDX, BTN_PRINT, btn_Menu);
- DDX_Control(pDX, ED_MOVE, ed_Move);
- DDX_Control(pDX, ED_PRINTY, ed_PrintY);
- DDX_Control(pDX, ED_PRINTX, ed_PrintX);
- DDX_Control(pDX, ED_PRINTH, ed_PrintH);
- DDX_Control(pDX, ED_PENW, ed_PenW);
- DDX_Control(pDX, CMB_STYLE, cmb_Style);
- DDX_Control(pDX, BTN_ONTOP, btn_OnTop);
- DDX_Control(pDX, CMB_MAPMODE, cmb_MapMode);
- DDX_Control(pDX, ED_CODE, ed_Code);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(ZDlg, CDialog)
- //{{AFX_MSG_MAP(ZDlg)
- ON_WM_PAINT()
- ON_BN_CLICKED(IDOK, On_IDOK)
- ON_BN_CLICKED(BTN_HELP, On_BtnHelp)
- ON_WM_DESTROY()
- ON_BN_CLICKED(BTN_CLOSE, On_BtnClose)
- ON_WM_LBUTTONDOWN()
- ON_BN_CLICKED(BTN_ONTOP, On_BtnOnTop)
- ON_CBN_SELCHANGE(CMB_STYLE, On_CmbStyleSelChange)
- ON_CBN_SELCHANGE(CMB_MAPMODE, On_CmbMapModeSelChange)
- ON_BN_CLICKED(BTN_PRINT, On_BtnPrint)
- ON_EN_CHANGE(ED_CODE, On_EdCodeChange)
- ON_EN_CHANGE(ED_PENW, On_EdPenWidthChange)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // ZDlg message handlers
- void ZDlg::On_IDOK()
- {
- }
- void ZDlg::On_BtnClose()
- {
- OnCancel();
- }
- void ZDlg::OnLButtonDown(UINT nFlags, CPoint point)
- {
- CDialog::OnLButtonDown(nFlags, point);
- SendMessage(WM_NCLBUTTONDOWN,HTCAPTION);
- }
- void ZDlg::On_BtnOnTop()
- {
- CString cs;
- btn_OnTop.GetWindowText(cs);
- if(cs.CompareNoCase("OnTop")==0) btn_OnTop.SetWindowText("OnNormal");
- else btn_OnTop.SetWindowText("OnTop");
- P_SetOnTop();
- }
- void ZDlg::P_SetOnTop()
- {
- CString cs;
- btn_OnTop.GetWindowText(cs);
- HWND hWnd=HWND_NOTOPMOST;
- if(cs.CompareNoCase("OnTop")!=0) hWnd=HWND_TOPMOST;
- ::SetWindowPos(m_hWnd,hWnd,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
- }
- void ZDlg::OnDestroy()
- {
- CDialog::OnDestroy();
- }
- int ZDlg::P_AddOrGet(CComboBox &box, BOOL bAdd)
- {
- struct IntString
- {
- int iV;
- char*psz;
- };
- IntString ps[]=
- {
- {MM_TEXT, "MM_TEXT"},
- {MM_LOENGLISH, "MM_LOENGLISH"},
- {MM_HIENGLISH, "MM_HIENGLISH"},
- {MM_LOMETRIC, "MM_LOMETRIC"},
- {MM_TWIPS, "MM_TWIPS"},
- };
-
- int i,iNum=sizeof(ps)/sizeof(ps[0]);
- if(bAdd==0)
- {
- i=box.GetCurSel();
- if((i<0)||(i>=iNum)) return ps[0].iV;
- return ps[i].iV;
- }
- for(i=0;i<iNum;i++)
- {
- box.AddString(ps[i].psz);
- }
- return 0;
- }
- BOOL ZDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- SetWindowText("Using barcode library");
- P_AddOrGet(cmb_MapMode,1);
- char*pCode[]=
- {
- "Code39",
- "Code93",
- "Code128A",
- "Code128B",
- "Code128C",
- "CodeI2of5",
- "CodeEan13",
- };
- int i,iNum=sizeof(pCode)/sizeof(pCode[0]);
- for(i=0;i<iNum;i++)
- {
- cmb_Style.AddString(pCode[i]);
- }
-
- cmb_Style.SetCurSel(0);
- cmb_MapMode.SetCurSel(1);
- ed_Code.SetWindowText("0123456789");
- ed_PenW.SetWindowText("1");
- ed_PrintX.SetWindowText("100");
- ed_PrintY.SetWindowText("100");
- ed_PrintH.SetWindowText("100");
- /*
- HWND hIMCWND = NULL;
- HIMC hImc = ImmGetContext(hIMCWND);
- if(ImmGetOpenStatus(hImc))
- ImmSetOpenStatus(hImc, FALSE); // 输入法状态 TRUE..On / FALSE..Off
- ImmReleaseContext(hIMCWND, hImc);
- */
- return 1;
- }
- void ZDlg::On_BtnHelp()
- {
- char sz[1024];
- ::GetModuleFileName(0,sz,1020);
- char*p=strrchr(sz,'\\');
- p++;
- *p=0;
- strcat(sz,"help.htm");
- ::ShellExecute(0,0,sz,0,0,SW_SHOW);
- }
- void ZDlg::On_CmbStyleSelChange()
- {
- RedrawWindow();
- }
- void ZDlg::On_CmbMapModeSelChange()
- {
- RedrawWindow();
- }
- void ZDlg::On_EdCodeChange()
- {
- RedrawWindow();
- }
- void ZDlg::On_EdPenWidthChange()
- {
- RedrawWindow();
- }
- void ZDlg::OnPaint()
- {
- CPaintDC dc(this);
- P_DrawBarCode(&dc,0);
- }
- void ZDlg::On_BtnPrint() // 打印;
- {
- #if 0
- CPrintDialog cp(0);
- if(cp.DoModal()!=IDOK) return;
- HDC hDC=cp.GetPrinterDC();
- CDC*pDC=CDC::FromHandle(hDC);
- P_DrawBarCode(pDC,1);
- #else
- CPrinter pter;
- pter.InitToPrint(NULL);
- pter.SetMargins(0,0,0,0);
- pter.StartPrint();
- pter.StartPage();
- CRect rcBar(2,2,60,60);
- pter.DrawBarCode(_T("A11000234545467"), rcBar);
- LOGFONT log;
- log.lfHeight = -MulDiv(5, GetDeviceCaps(pter.GetHDC(), LOGPIXELSY), 72);
- log.lfWidth = 0;
- log.lfEscapement = 0;
- log.lfOrientation = 0;
- log.lfWeight = FW_REGULAR;
- log.lfItalic = false;
- log.lfUnderline = false;
- log.lfStrikeOut = 0;
- log.lfCharSet = ANSI_CHARSET;
- log.lfOutPrecision = OUT_DEFAULT_PRECIS;
- log.lfClipPrecision = CLIP_DEFAULT_PRECIS;
- log.lfQuality = DEFAULT_QUALITY;
- log.lfPitchAndFamily = DEFAULT_PITCH || FF_ROMAN;
- _tcscpy_s(log.lfFaceName,_T("Arial"));
-
- rcBar.top += 55;
- rcBar.right = rcBar.left + 950;
- rcBar.bottom = rcBar.top + 50;
- pter.DrawTextEx(_T("订单号:20160402-001"), rcBar, log, FORMAT_NORMAL);
- rcBar.top += 55;
- rcBar.bottom = rcBar.top + 50;
- pter.DrawTextEx(_T("客人:王五、李四"), rcBar, log, FORMAT_NORMAL);
- rcBar.top += 55;
- rcBar.bottom = rcBar.top + 50;
- pter.DrawTextEx(_T("商品:7寸水晶摆台"), rcBar, log, FORMAT_NORMAL);
- pter.EndPage();
- pter.EndPrint();
- #endif
- }
- void ZDlg::P_DrawBarCode(CDC*pDC,const BOOL bPrint)
- {
- const int iSel=cmb_Style.GetCurSel();
- if(iSel<0) return;
- //--------------------------------------------
- int iL,iT,iB;
- const int iMapMode=pDC->SetMapMode(P_AddOrGet(cmb_MapMode,0));
-
- {
- CRect rcT;
- if(bPrint)
- {
- rcT.left =Help::GetValue(ed_PrintX);
- rcT.top =Help::GetValue(ed_PrintY);
- rcT.bottom =rcT.top+Help::GetValue(ed_PrintH);
- //temp, useless value
- rcT.right=rcT.left+100;
- }
- else
- {
- GetWindowRect(&rcT);
- ::MapWindowPoints(0,m_hWnd,(POINT*)&rcT,2);
- rcT.left+=4;
- rcT.bottom+=30;
- }
- pDC->DPtoLP(&rcT);
- iL=rcT.left + 20;
- iT=rcT.top - 80;
- iB=rcT.bottom + 80;
- }
- //--------------------------------------------
- int iPenW=Help::GetValue(ed_PenW);
- if(iPenW==0) iPenW=1;
- CString csStyle;
- cmb_Style.GetWindowText(csStyle);
- CString csCode;
- ed_Code.GetWindowText(csCode);
- COLORREF clrBar =RGB(0,0,0);
- COLORREF clrSpace =RGB(255,255,255);
- if(bPrint)
- {
- pDC->StartDoc("Hello");
- pDC->StartPage();
- }
- if(csStyle.CompareNoCase("Code39")==0)
- {
- Barcode39 code;
- code.Encode39(csCode);
- P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code);
- }
- else
- if(csStyle.CompareNoCase("Code93")==0)
- {
- Barcode93 code;
- code.Encode93(csCode);
- P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code);
- }
- else
- if(csStyle.CompareNoCase("CodeI2of5")==0)
- {
- BarcodeI2of5 code;
- code.EncodeI2of5(csCode);
- P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code);
- }
- else
- if(csStyle.CompareNoCase("Code128A")==0)
- {
- Barcode128 code;
- code.Encode128A(csCode);
- P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code);
- }
- else
- if(csStyle.CompareNoCase("Code128B")==0)
- {
- Barcode128 code;
- code.Encode128B(csCode);
- P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code);
- }
- else
- if(csStyle.CompareNoCase("Code128C")==0)
- {
- Barcode128 code;
- code.Encode128C(csCode);
- P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code);
- }
- else
- if(csStyle.CompareNoCase("CodeEan13")==0)
- {
- BarcodeEan13 code;
- code.EncodeEan13(csCode);
- P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code);
- }
-
- if(bPrint)
- {
- pDC->EndPage();
- pDC->EndDoc();
- }
- pDC->SetMapMode(iMapMode);
- }
- void ZDlg::P_DrawBarcode(CDC*pDC,int iX,int iY0,int iY10,int iY11,COLORREF clrBar,COLORREF clrSpace,int iPenW,BarcodeBase*pBc)
- {
- pBc->DrawBarcode(pDC->m_hDC,iX,iY0,iY10,iY11,clrBar,clrSpace,iPenW);
- }
- LRESULT ZDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- CRect rc;
- if(message==WM_USER)
- {
- GetWindowRect(&rc);
- rc.left+=wParam;
- rc.right+=wParam;
- MoveWindow(&rc);
- }
- else
- if(message==WM_USER+1)
- {
- GetWindowRect(&rc);
- rc.top+=wParam;
- rc.bottom+=wParam;
- MoveWindow(&rc);
- }
- return CDialog::WindowProc(message, wParam, lParam);
- }
|