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

#include "stdafx.h"
#include "TestTLXLabelPaint.h"
#include "Winspool.h"

//	请包含两个头文件
//	并请将 TLXLabelPaint 放入库目录
/*
请注意：
请在正式的项目中仅使用 TLXLabelPaint.h 中 公开的接口函数，
文件后半部分的非公开的接口函数可能未经过完整的测试，且在后续的版本中可能不再被支持
*/
#include "TLXLabelPaint.h"
#include "TLXLabelPaintType.h"

#include "TestTLXLabelPaintDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


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

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

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

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

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CTestTLXLabelPaintDlg 对话框



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

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

BEGIN_MESSAGE_MAP(CTestTLXLabelPaintDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK, &CTestTLXLabelPaintDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDC_OPRN_LABEL, &CTestTLXLabelPaintDlg::OnBnClickedOprnLabel)
	ON_BN_CLICKED(IDOK2, &CTestTLXLabelPaintDlg::OnBnClickedOk2)
	ON_BN_CLICKED(IDC_SPRINT_SETPRN, &CTestTLXLabelPaintDlg::OnBnClickedSprintSetprn)
	ON_CBN_SELCHANGE(IDC_PRINTER, &CTestTLXLabelPaintDlg::OnCbnSelchangePrinter)
	ON_CBN_SELCHANGE(IDC_VARIABLE, &CTestTLXLabelPaintDlg::OnCbnSelchangeVariable)
	ON_BN_CLICKED(IDC_SETVAR, &CTestTLXLabelPaintDlg::OnBnClickedSetvar)
	ON_BN_CLICKED(IDC_SAVE, &CTestTLXLabelPaintDlg::OnBnClickedSave)
	ON_BN_CLICKED(IDC_SETVAR2, &CTestTLXLabelPaintDlg::OnBnClickedSetvar2)
	ON_BN_CLICKED(IDOK3, &CTestTLXLabelPaintDlg::OnBnClickedOk3)
	ON_BN_CLICKED(IDOK4, &CTestTLXLabelPaintDlg::OnBnClickedOk4)
	ON_BN_CLICKED(IDOK7, &CTestTLXLabelPaintDlg::OnBnClickedOk7)
END_MESSAGE_MAP()


// CTestTLXLabelPaintDlg 消息处理程序

BOOL CTestTLXLabelPaintDlg::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);		// 设置小图标

	///////////////////////////////////////////////////////////////////////////
	//	以下是调用函数库的过程，初始化

	//	请使用您的企业版本注册码注册DLL，如果电脑上已经安装了专业版，则启动会可以自动识别到专业版授权
//	TLXSetLicense(_T("00000-00000-00000-00000-00000"));

	//	获取文档信息，并显示 

	CString strInfo = _T("授权模式：未知授权模式");
	int license;
	int ret = TLXGetLicense(&license);

	switch (license)
	{
	case TLXLP_LICENSE_NONE:
		strInfo = _T("授权模式：未授权");
		break;
	case TLXLP_LICENSE_BASIC:
		strInfo = _T("授权模式：标准版授权");
		break;
	case TLXLP_LICENSE_PROFESSIONAL:
		strInfo = _T("授权模式：专业版授权");
		break;
	case TLXLP_LICENSE_ENTERPRISE:
		strInfo = _T("授权模式：企业版授权");
		break;
	}

	SetDlgItemText(IDC_G_INFO, strInfo);

	CComboBox* pLabelPrn = (CComboBox*)GetDlgItem(IDC_PRINTER);
	CString strPrnName;
/*
	系统中的打印机
	DWORD dwByte;
	DWORD dwMaxPrinter;
	EnumPrinters(PRINTER_ENUM_LOCAL + PRINTER_ENUM_CONNECTIONS, NULL, 2, NULL, 0, &dwByte, &dwMaxPrinter);
	PRINTER_INFO_2 * pinf = (PRINTER_INFO_2 *)::GlobalAlloc(LPTR, dwByte);
	PRINTER_INFO_2 * pinf1 = pinf;
	EnumPrinters(PRINTER_ENUM_LOCAL + PRINTER_ENUM_CONNECTIONS, NULL, 2, (LPBYTE)pinf, dwByte, &dwByte, &dwMaxPrinter);

	int nidx;
	for (DWORD j = 0; j < dwMaxPrinter; j++)
	{
		strPrnName = pinf->pPrinterName;
		nidx = pLabelPrn->AddString(strPrnName);
		pinf++;
	}
	::GlobalFree(pinf1);
*/
	//	获取系统中支持的打印机
	LPCTSTR lpszPrinters = NULL;
	TLXGetSysPrinterNames(&lpszPrinters);
	int i = 0;
	while (::AfxExtractSubString(strPrnName, lpszPrinters, i++, _T('\n')))
	{
		if (strPrnName.GetLength() > 0)
			pLabelPrn->AddString(strPrnName);
	}

	m_strLoadFileName = AfxGetApp()->GetProfileString(_T("settings"), _T("labelfile"));
	SetDlgItemText(IDC_LABEL_NAME, m_strLoadFileName);

	if (m_strLoadFileName.GetLength() > 0)
		OpenLabelFormat(m_strLoadFileName);

	((CSpinButtonCtrl *)GetDlgItem(IDC_SPRINT_SPINQTY))->SetRange(1, 32767);
	((CSpinButtonCtrl *)GetDlgItem(IDC_SPRINT_SPINCOPY))->SetRange(1, 32767);
	SetDlgItemInt(IDC_SPRINT_QTY, 1);
	SetDlgItemInt(IDC_SPRINT_COPY, 1);

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

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

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

void CTestTLXLabelPaintDlg::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
	{
		//	这里的代码用于画标签的图像
		CPaintDC dc(this); // device context for painting
						   // TODO: 在此处添加消息处理程序代码
						   // 不为绘图消息调用 CDialog::OnPaint()

		if (m_hBit)
		{
			CRect rectPaint;
			GetDlgItem(IDC_PVIEW)->GetWindowRect(&rectPaint);
			this->ScreenToClient(&rectPaint);
			rectPaint.left += 6;
			rectPaint.right -= 6;
			rectPaint.bottom -= 6;
			rectPaint.top += 16;
			//		dc.Rectangle(&rectPaint);

			CDC dcImage;
			if (!dcImage.CreateCompatibleDC(&dc))
				return;

			dc.SetBkMode(TRANSPARENT);
			BITMAP bm;
			::GetObject(m_hBit, sizeof(BITMAP), &bm);

			int x = (rectPaint.Width() - bm.bmWidth) / 2 + rectPaint.left;
			int y = (rectPaint.Height() - bm.bmHeight) / 2 + rectPaint.top;

			HGDIOBJ pOldBitmap = dcImage.SelectObject((HBITMAP)m_hBit);
			dc.BitBlt(x, y, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
			dcImage.SelectObject(pOldBitmap);
		}
	}
}

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

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

//	打开一个标签模板
void CTestTLXLabelPaintDlg::OnBnClickedOprnLabel()
{
	CString strFilter = _T("标签模板文件(*.lsdx)|*.lsdx|所有文件(*.*)|*.*||");
	CFileDialog dlg(TRUE, _T(".lsdx"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strFilter, this);
	dlg.m_ofn.lpstrFile = m_strLoadFileName.GetBuffer(_MAX_PATH);

	int nResult = dlg.DoModal();
	m_strLoadFileName.ReleaseBuffer();

	if (nResult == IDOK)
	{
		SetDlgItemText(IDC_LABEL_NAME, m_strLoadFileName);
		AfxGetApp()->WriteProfileString(_T("settings"), _T("labelfile"), m_strLoadFileName);

		OpenLabelFormat(m_strLoadFileName);
	}
}
//	打开标签模板
BOOL CTestTLXLabelPaintDlg::OpenLabelFormat(LPCTSTR lpszLabelFormat)
{
	SetDlgItemText(IDC_L_INFO, _T("标签模板："));

	if (m_labelHandle != 0)
	{
		TLXCloseDocument(m_labelHandle);
		m_labelHandle = 0;
	}

	CComboBox* pLabelPrn = (CComboBox*)GetDlgItem(IDC_PRINTER);

	//	打开标签模板
	int ret = TLXOpenDocument(m_strLoadFileName,&m_labelHandle);

	if (m_labelHandle == 0)
		return FALSE;

	//	获取文档授权状态
	int level;
	ret = TLXGetDocumentLevel(m_labelHandle, &level);

	CString strInfo = _T("标签模板：未知授权状态");

	switch (level)
	{
	case TLXLP_DOCUMENT_UNAUTHORIZED:
		strInfo = _T("标签模板：未授权的标签模板");
		break;
	case TLXLP_DOCUMENT_AUTHORIZED:
		strInfo = _T("标签模板：已经授权的标签模板");
		break;
	}
	SetDlgItemText(IDC_L_INFO, strInfo);

	//	由于系统中未安装的LabelShop打印机的影响，打印机可能会发生变化，重新获取打印机
	CString strPrnName;
	LPCTSTR lpszPrinters = NULL;
	TLXGetSysPrinterNames(&lpszPrinters);
	int i = 0;
	pLabelPrn->ResetContent();
	while (::AfxExtractSubString(strPrnName, lpszPrinters, i++, _T('\n')))
	{
		if (strPrnName.GetLength() > 0)
			pLabelPrn->AddString(strPrnName);
	}

	//	标签模板的默认打印机
	LPCTSTR  lpszPrnName = NULL;
	ret = TLXGetPrinterName(m_labelHandle, &lpszPrnName);
	pLabelPrn->SelectString(-1, lpszPrnName);

	//	命名变量
	CComboBox* pNamedVar = (CComboBox*)GetDlgItem(IDC_VARIABLE);
	pNamedVar->ResetContent();
	//	获取标签模板中的命名变量的变量名串，由 \n 分隔
	LPCTSTR lpszVarNames = NULL;
	ret = TLXGetNamedVarNames(m_labelHandle, &lpszVarNames);
	CString strVarNames = lpszVarNames;
	CString s1;
	i = 0;
	while (::AfxExtractSubString(s1, strVarNames, i++, _T('\n')))
	{
		if (s1.GetLength() > 0)
			pNamedVar->AddString(s1);
	}
	pNamedVar->SetCurSel(0);
	OnCbnSelchangeVariable();

	//	是否旋转180度打印
	int rotate = 0;
	TLXGetPrinterRotate180(m_labelHandle, &rotate);
	CheckDlgButton(IDC_PRINT_ORIENTATION, (rotate != 0)	? BST_CHECKED : BST_UNCHECKED);

	//	重画标签图片
	RefreshLabelView();
	return TRUE;
}

BOOL CTestTLXLabelPaintDlg::RefreshLabelView()
{
	if (m_hBit)
		::DeleteObject(m_hBit);

	m_hBit = NULL;

	CRect rectPaint;
	GetDlgItem(IDC_PVIEW)->GetWindowRect(&rectPaint);
	this->ScreenToClient(&rectPaint);
	rectPaint.left += 6;
	rectPaint.right -= 6;
	rectPaint.bottom -= 6;
	rectPaint.top += 16;

	//	获取标签模板的显示略图
	int ret = TLXGetThumbnail(m_labelHandle, &m_hBit,min(rectPaint.Width(), rectPaint.Height()));

	Invalidate();

	return TRUE;
}

//	打印标签模板
void CTestTLXLabelPaintDlg::OnBnClickedOk2()
{
	int qty = GetDlgItemInt(IDC_SPRINT_QTY);
	int copy = GetDlgItemInt(IDC_SPRINT_COPY);

	//	是否旋转180度打印
	int rotate = 0;
	if (IsDlgButtonChecked(IDC_PRINT_ORIENTATION) == BST_CHECKED)
		rotate = 1;
	else
		rotate = 0;

	TLXSetPrinterRotate180(m_labelHandle, rotate);

	//	打印标签模板
	int err = 2;
	TLXOutputDocument(m_labelHandle, qty, copy, err);
}

void CTestTLXLabelPaintDlg::OnBnClickedSprintSetprn()
{
	//	设置打印机属性
	TLXPrinterProperties(m_labelHandle,GetSafeHwnd());
}

//	当选择其它目标打印机时
void CTestTLXLabelPaintDlg::OnCbnSelchangePrinter()
{
	CComboBox* pLabelPrn = (CComboBox*)GetDlgItem(IDC_PRINTER);
	if (pLabelPrn->GetCurSel() == CB_ERR)
		return;

	CString strPrinter;
	pLabelPrn->GetLBText(pLabelPrn->GetCurSel(), strPrinter);

	//	设置目标打印机
	TLXSetPrinterName(m_labelHandle, strPrinter);
}

//	更改当前变量
void CTestTLXLabelPaintDlg::OnCbnSelchangeVariable()
{
	CComboBox* pNamedVar = (CComboBox*)GetDlgItem(IDC_VARIABLE);
	if (pNamedVar->GetCurSel() == CB_ERR)
	{
		SetDlgItemText(IDC_VALUE, _T(""));
		return;
	}

	CString strVarName;
	pNamedVar->GetLBText(pNamedVar->GetCurSel(), strVarName);

	//	获取指令命名变量的值
	LPCTSTR lpszVarData = NULL;
	int ret = TLXGetNamedVarValue(m_labelHandle, strVarName, &lpszVarData);
	SetDlgItemText(IDC_VALUE, lpszVarData);
}

//	设置变量的内容
void CTestTLXLabelPaintDlg::OnBnClickedSetvar()
{
	CComboBox* pNamedVar = (CComboBox*)GetDlgItem(IDC_VARIABLE);
	if (pNamedVar->GetCurSel() == CB_ERR)
		return;

	CString strVarName;
	pNamedVar->GetLBText(pNamedVar->GetCurSel(), strVarName);

	CString strVarData;

	GetDlgItemText(IDC_VALUE, strVarData);

	//	设置指定命名变量的值
	TLXSetNamedVariable(m_labelHandle, strVarName, strVarData);

	RefreshLabelView();
}

//	保存
void CTestTLXLabelPaintDlg::OnBnClickedSave()
{
	TLXSaveDocument(m_labelHandle);
}

//	查询记录
void CTestTLXLabelPaintDlg::OnBnClickedSetvar2()
{
	CString strKeyData;

	GetDlgItemText(IDC_VALUE2, strKeyData);

	if (TLXSearchRecord(m_labelHandle, strKeyData))
		RefreshLabelView();

}

//	打印
void CTestTLXLabelPaintDlg::OnBnClickedOk3()
{
	int qty = GetDlgItemInt(IDC_SPRINT_QTY);
	int copy = GetDlgItemInt(IDC_SPRINT_COPY);

	//	打印标签模板
	int err = 2;
	TLXPreviewDocument(m_labelHandle, qty, copy, err);
}

//	批量打印
void CTestTLXLabelPaintDlg::OnBnClickedOk4()
{
	TLXNamedVarsClearData(m_labelHandle);
	//  第一行数据
	TLXNamedVarsAddVarData(m_labelHandle, 0, _T("NAME"), _T("读卡机"));
	TLXNamedVarsAddVarData(m_labelHandle, 0, _T("BARCODE"), _T("00000000"));
	//  第二行数据
	TLXNamedVarsAddVarData(m_labelHandle, 1, _T("NAME"), _T("扫描仪"));
	TLXNamedVarsAddVarData(m_labelHandle, 1, _T("BARCODE"), _T("12345678"));
	//  打印
	TLXOutputDocument(m_labelHandle, 2, 1, 0);
}

BOOL CTestTLXLabelPaintDlg::OpenLabelFormatXML(LPCTSTR lpszXML)
{
	SetDlgItemText(IDC_L_INFO, _T("标签模板："));

	if (m_labelHandle != 0)
	{
		TLXCloseDocument(m_labelHandle);
		m_labelHandle = 0;
	}

	CComboBox* pLabelPrn = (CComboBox*)GetDlgItem(IDC_PRINTER);

	//	打开标签模板
	int ret = TLXOpenDocumentXML(lpszXML, &m_labelHandle);

	if (m_labelHandle == 0)
		return FALSE;

	//	获取文档授权状态
	int level;
	ret = TLXGetDocumentLevel(m_labelHandle, &level);

	CString strInfo = _T("标签模板：未知授权状态");

	switch (level)
	{
	case TLXLP_DOCUMENT_UNAUTHORIZED:
		strInfo = _T("标签模板：未授权的标签模板");
		break;
	case TLXLP_DOCUMENT_AUTHORIZED:
		strInfo = _T("标签模板：已经授权的标签模板");
		break;
	}
	SetDlgItemText(IDC_L_INFO, strInfo);

	//	由于系统中未安装的LabelShop打印机的影响，打印机可能会发生变化，重新获取打印机
	CString strPrnName;
	LPCTSTR lpszPrinters = NULL;
	TLXGetSysPrinterNames(&lpszPrinters);
	int i = 0;
	pLabelPrn->ResetContent();
	while (::AfxExtractSubString(strPrnName, lpszPrinters, i++, _T('\n')))
	{
		if (strPrnName.GetLength() > 0)
			pLabelPrn->AddString(strPrnName);
	}

	//	标签模板的默认打印机
	LPCTSTR  lpszPrnName = NULL;
	ret = TLXGetPrinterName(m_labelHandle, &lpszPrnName);
	pLabelPrn->SelectString(-1, lpszPrnName);

	//	命名变量
	CComboBox* pNamedVar = (CComboBox*)GetDlgItem(IDC_VARIABLE);
	pNamedVar->ResetContent();
	//	获取标签模板中的命名变量的变量名串，由 \n 分隔
	LPCTSTR lpszVarNames = NULL;
	ret = TLXGetNamedVarNames(m_labelHandle, &lpszVarNames);
	CString strVarNames = lpszVarNames;
	CString s1;
	i = 0;
	while (::AfxExtractSubString(s1, strVarNames, i++, _T('\n')))
	{
		if (s1.GetLength() > 0)
			pNamedVar->AddString(s1);
	}
	pNamedVar->SetCurSel(0);
	OnCbnSelchangeVariable();

	//	是否旋转180度打印
	int rotate = 0;
	TLXGetPrinterRotate180(m_labelHandle, &rotate);
	CheckDlgButton(IDC_PRINT_ORIENTATION, (rotate != 0) ? BST_CHECKED : BST_UNCHECKED);

	//	重画标签图片
	RefreshLabelView();
	return TRUE;
}


void CTestTLXLabelPaintDlg::OnBnClickedOk7()
{
	// 编辑标签
	LPCTSTR pszXML = NULL;

	if (TLXGetDocumentXML(m_labelHandle, &pszXML) != TLXLP_OK)
		return;

	CString strXML = pszXML;
	int ret = TLXEditTemplateXML(strXML, &pszXML);
	if (ret != TLXLP_OK)
		return;

	strXML = pszXML;
	OpenLabelFormatXML(strXML);
}
