
// MFC_ZPL_DemoDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "MFC_ZPL_Demo.h"
#include "MFC_ZPL_DemoDlg.h"
#include "afxdialogex.h"
#include "printer_base.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

	// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

	// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CMFC_ZPL_DemoDlg 对话框



CMFC_ZPL_DemoDlg::CMFC_ZPL_DemoDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CMFC_ZPL_DemoDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMFC_ZPL_DemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CMFC_ZPL_DemoDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(btntest, &CMFC_ZPL_DemoDlg::OnBnClickedbtntest)
END_MESSAGE_MAP()


// CMFC_ZPL_DemoDlg 消息处理程序

BOOL CMFC_ZPL_DemoDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO:  在此添加额外的初始化代码

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CMFC_ZPL_DemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CMFC_ZPL_DemoDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMFC_ZPL_DemoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CMFC_ZPL_DemoDlg::OnBnClickedbtntest()
{
	// TODO:  在此添加控件通知处理程序代码
	void* pHandle = NULL;
	char* cUtf8Data = NULL;
	unsigned char cRfidData[8] ={0}; 
	unsigned int preadedNum = 0;
	int iResult = 0;
	int dwStateData = 0;
	iResult = PrinterCreator(&pHandle, "HT300");
	if (iResult == 0)
	{
		iResult = PortOpen(pHandle, "USB");
		if (iResult == 0)
		{
			//开始
			iResult = StartFormat(pHandle);
			//设置了打印浓度
			iResult = SetPrintDarkness(pHandle, 20);
			//设置标签长度
			iResult = SetLabelLength(pHandle,1400);
			//设置是否切刀、剥离等
			iResult = SetPrintMode(pHandle,'T','N');
			iResult = SetPrintQuantity(pHandle,1,0,1,'N');
			//打印文本
			iResult = SetChangeFontEncoding(pHandle, 14);
			iResult = PrintText(pHandle, 100, 100, 16, 0, 40, 40,"中文");

			//打印Pdf417
            iResult = Pdf417(pHandle, 100, 250, 0, 2, 20, 2, 5, 5, 'N', "0010101101");

			//打印MicroPdf417
		//	iResult = MicroPdf417(pHandle, 100, 250, 0, 2, 20, 23, "0010101101");

			//打印二维码
			iResult = QRCode(pHandle, 500, 300, 0, 1, 3, 'M', 'M','N', "1000000001");	

			//打印BarCode39条码
			iResult = BarCode39(pHandle, 100, 400, 0, 3, 50, 'Y', 'Y', 'Y', "66886868");

			//打印CodeEan8条码
			iResult = CodeEan8(pHandle, 100, 500, 0, 3, 60, 'Y', 'Y', "90381010");
			 
			//打印UPC-A
            iResult = UpcaBarcode(pHandle, 100, 600, 0, 3, 50, 'Y', 'Y', 'N', "47631664454");

            //打印UPC扩展条码
            iResult = UpcExtensions(pHandle, 100, 700, 0, 3, 50, 'Y', 'Y', "01234");

			//打印图片
			iResult = PrintImage(pHandle, 100, 850, "D:\\1.bmp");

		    //打印图形圆圈
            iResult = GraphicCircle(pHandle, 100, 1000, 150, 5);

            //打印图形椭圆
            iResult = GraphicEllipse(pHandle, 300, 1000, 150, 100, 10);

            //打印对角线
            iResult = GraphicDiagonaline(pHandle, 500, 1000, 0x52, 50, 50, 20);

            //打印图形框
            iResult = GraphicBox(pHandle, 100, 1200, 100, 100, 60, 3);

            //打印符号
            iResult = GraphicSymbol(pHandle, 300, 1200, 180, 200, 200, "A");

			//获取状态
			iResult = GetPrinterStatus(pHandle,&dwStateData);
	
			//RFID读写
			iResult = RfidWrite(pHandle, 'H', 2, 4, 1, "12345678");
			iResult = RfidRead(pHandle, 'H', 2, 4, 1, "","");

			//结束
			iResult = EndFormat(pHandle);

			iResult = ReadData(pHandle,cRfidData,8,&preadedNum);

			//删除打印机
			iResult = PortClose(pHandle);

			//销毁
			iResult = PrinterDestroy(pHandle);
		}
		else
		{
			AfxMessageBox(L"Usb Device not found.");
			return;
		}
	}
	else
	{
		AfxMessageBox(L"Printer Device not found.");
		return;
	}
}
