// ZDlg.cpp : implementation file
//
#include "stdafx.h"
#include "BarCode.h"
#include "ZDlg.h"
#include "Help.h"
#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");
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()
{
CPrintDialog cp(0);
if(cp.DoModal()!=IDOK) return;
HDC hDC=cp.GetPrinterDC();
CDC*pDC=CDC::FromHandle(hDC);
P_DrawBarCode(pDC,1);
}
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;
iT=rcT.top;
iB=rcT.bottom;
}
//--------------------------------------------
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);
}