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

#include "stdafx.h"
#include "MFC_CPCL.h"
#include "MFC_CPCLDlg.h"
#include "afxdialogex.h"
#include "cpcl_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_CPCLDlg 对话框



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

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

BEGIN_MESSAGE_MAP(CMFC_CPCLDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &CMFC_CPCLDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDCANCEL, &CMFC_CPCLDlg::OnBnClickedCancel)
END_MESSAGE_MAP()


// CMFC_CPCLDlg 消息处理程序

BOOL CMFC_CPCLDlg::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_CPCLDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

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

void CMFC_CPCLDlg::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_CPCLDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


CString GetFormatError(int Result)
{
	int langid = 0;
	char temp[1024];
	CString str;

	DemFormatError(Result, langid, temp, 0, 1024);
	str.Format(_T("%s"), CStringW(temp));

	return str;
}

void CMFC_CPCLDlg::OnBnClickedButton1()
{
	// TODO:  在此添加控件通知处理程序代码
	int result = -1;
	CString strError;
	

	if (E_SUCCESS != (result = DemPrinterCreator(&hprinter, "HM-A300")))
	{
		strError = GetFormatError(result);
		hprinter = NULL;
		AfxMessageBox(strError);
		return;
	}
	if (E_SUCCESS != (result = DemPortOpen(hprinter,"USB")))
	{
		strError = GetFormatError(result);
		AfxMessageBox(strError);
		return;
	}

	//Set Size
	result = DemAddLabel(hprinter, 0, 1000, 1);

	//Set Text Underline  and Bold
	result = DemSetTextUnderline(hprinter, 1);
	result = DemSetTextBold(hprinter, 2);
	result = DemAddText(hprinter, ROTATE_NONE, 4, 7, 8, 0, "SetTextUnderline  TextBold");

	//normal Text
	result = DemSetTextUnderline(hprinter, 0);
	result = DemSetTextBold(hprinter, 0);
	result = DemAddText(hprinter, ROTATE_NONE, 0, 0, 8, 56, "NORMAL TEXT");

	result = DemAddBarCodeText(hprinter, 1, 7, 0, 0);
	//CODE 128
	result = DemAddBarCode(hprinter, ROTATE_NONE, BARCODE_128, 2, 2, 50, 8, 98, "code128");
	

	//CODE 39
	result = DemAddBarCode(hprinter, ROTATE_NONE, BARCODE_39, 2, 2, 50, 8, 176, "CODE39");
	
	//CODE 93
	result = DemAddBarCode(hprinter, ROTATE_NONE, BARCODE_93, 2, 2, 50, 8, 252, "CODE93");
	
	//BARCODE
	result = DemAddBarCode(hprinter, ROTATE_NONE, BARCODE_CODABAR, 2, 2, 50, 8, 328, "A1234567890A");
	
	//ENA13
	result = DemAddBarCode(hprinter, ROTATE_NONE, BARCODE_EAN13, 2, 2, 50, 8, 400, "123456789012");

	//ENA8
	result = DemAddBarCode(hprinter, ROTATE_NONE, BARCODE_EAN8, 2, 2, 50, 8, 472, "1234567");
	
	//QRCODE 2
	result = DemAddQRCode(hprinter,ROTATE_NONE, 8, 560, 2, 5, ECC_LEVEL_H, "QRCODE654321");
	//QRCODE 1
	result = DemAddQRCode(hprinter, ROTATE_NONE, 200, 560, 1, 5, ECC_LEVEL_H, "QRCODE123456");

	//Image
	result = DemAddImage(hprinter, ROTATE_NONE, 200, 700, "0.jpg");

	//LINE
	result = DemAddLine(hprinter, 8, 700, 8, 900, 2);

	//BOX
	result = DemAddBox(hprinter, 24, 700, 100, 900, 2);

	result = DemPrint(hprinter);
	result = DemPortClose(hprinter);
	result = DemPrinterDestroy(hprinter);

	////DirectIO 使用示例  必须给定纸张大小以及开启打印功能
	//unsigned char write[] = { 0x21, 0x20, 0x30, 0x20, 0x32, 0x30, 0x30, 0x20, 0x32, 0x30, 0x30, 0x20, 0x32, 0x30, 0x30, 0x20, 0x31, 0x0d, 0x0a, //AddLabel
	//                          0x50, 0x52, 0x45, 0x46, 0x45, 0x45, 0x44, 0x20, 0x32, 0x30, 0x0d, 0x0a,   //PREFEED 20
	//                          0x50, 0x52, 0x49, 0x4e, 0x54, 0x0d, 0x0a };//PRINT
	//unsigned int * inum=0;
	//result = DemDirectIO(hprinter, write, sizeof(write) / sizeof(write[0]), write, 0, inum);
}





void CMFC_CPCLDlg::OnBnClickedCancel()
{
	// TODO:  在此添加控件通知处理程序代码
	CDialogEx::OnCancel();
}
