// zyPrtLib_DemoDlg.cpp : Implementation file
//

#include "stdafx.h"
#include "zyPrtLib_Demo.h"
#include "zyPrtLib_DemoDlg.h"

#include<string>
using std::string;

#include "ZLG_IMG.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CzyPrtLib_DemoDlg Dialog box
CzyPrtLib_DemoDlg::CzyPrtLib_DemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CzyPrtLib_DemoDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CzyPrtLib_DemoDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);

    DDX_Control(pDX, IDC_COMBO_COM, m_combo_com);
    DDX_Control(pDX, IDC_COMBO_BAUD, m_combo_baud);
	DDX_Control(pDX, IDC_COMBO_FLOWCTRL, m_combo_flowctrl);

    DDX_Control(pDX, IDC_BTN_BAUD, m_btn_baud);
    DDX_Control(pDX, IDC_BTN_COM, m_btn_com);
	DDX_Control(pDX, IDC_BTN_FLOWCTRL, m_btn_flowctrl);

    DDX_Control(pDX, IDC_BTN_PAPER, m_btn_paper);
    DDX_Control(pDX, IDC_BTN_HEAT, m_btn_heat);
    DDX_Control(pDX, IDC_BTN_VOLTAGE, m_btn_overvoltage);
    DDX_Control(pDX, IDC_BTN_AXO, m_btn_axoopen);
    DDX_Control(pDX, IDC_BTN_CUTTER, m_btn_cutter);    
}

BEGIN_MESSAGE_MAP(CzyPrtLib_DemoDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()

	ON_MESSAGE(MSG_OVERHEAT, OnOverHeat) 
	ON_MESSAGE(MSG_OVERHEAT_RESUME, OnOverHeatResume) 
	ON_MESSAGE(MSG_OFPAPER, OnOfPaper) 
	ON_MESSAGE(MSG_OFPAPER_RESUME, OnOfPaperResume) 
	ON_MESSAGE(MSG_OVERVOLATGE, OnOverVoltage) 
	ON_MESSAGE(MSG_OVERVOLATGE_RESUME, OnOverVoltageResume) 
	ON_MESSAGE(MSG_AXOOPEN, OnAxoOpen) 
	ON_MESSAGE(MSG_AXOCLOSE, OnAxoClose) 
	ON_MESSAGE(MSG_CUTTERON, OnCutterOn) 
	ON_MESSAGE(MSG_CUTTEROFF, OnCutterOff) 

	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDCANCEL, &CzyPrtLib_DemoDlg::OnBnClickedCancel)

	ON_BN_CLICKED(IDC_BTN_COM, &CzyPrtLib_DemoDlg::OnBnClickedBtnCom)
	ON_BN_CLICKED(IDC_BTN_BAUD, &CzyPrtLib_DemoDlg::OnBnClickedBtnBaud)
	ON_BN_CLICKED(IDC_BTN_FLOWCTRL, &CzyPrtLib_DemoDlg::OnBnClickedBtnFlowctrl)

	ON_BN_CLICKED(IDC_BTN_1, &CzyPrtLib_DemoDlg::OnBnClickedBtn1)
	ON_BN_CLICKED(IDC_BTN_2, &CzyPrtLib_DemoDlg::OnBnClickedBtn2)
	ON_BN_CLICKED(IDC_BTN_3, &CzyPrtLib_DemoDlg::OnBnClickedBtn3)
	ON_BN_CLICKED(IDC_BTN_4, &CzyPrtLib_DemoDlg::OnBnClickedBtn4)
	ON_BN_CLICKED(IDC_BTN_5, &CzyPrtLib_DemoDlg::OnBnClickedBtn5)
	ON_BN_CLICKED(IDC_BTN_6, &CzyPrtLib_DemoDlg::OnBnClickedBtn6)
	ON_BN_CLICKED(IDC_BTN_7, &CzyPrtLib_DemoDlg::OnBnClickedBtn7)
	ON_BN_CLICKED(IDC_BTN_8, &CzyPrtLib_DemoDlg::OnBnClickedBtn8)
	ON_BN_CLICKED(IDC_BTN_9, &CzyPrtLib_DemoDlg::OnBnClickedBtn9)
	ON_BN_CLICKED(IDC_BTN_10, &CzyPrtLib_DemoDlg::OnBnClickedBtn10)
	ON_BN_CLICKED(IDC_BTN_11, &CzyPrtLib_DemoDlg::OnBnClickedBtn11)

    ON_BN_CLICKED(IDC_BTN_SYNC, &CzyPrtLib_DemoDlg::OnBnClickedBtnSync)

END_MESSAGE_MAP()


// CzyPrtLib_DemoDlg Message handler

BOOL CzyPrtLib_DemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialogue, when this dialogue is not the major displayed window
	// the frame will execute this operation automatically
	SetIcon(m_hIcon, TRUE);			// Set the large icon
	SetIcon(m_hIcon, FALSE);		// Set the small icon

	// TODO: Add additional initialization code here
	m_pIcon = NULL;
	
	InitLogo();	
	InitCbx();	
	InitBtn();

	// Set ZYTP message processing window
	SetZYTPWnd(this->m_hWnd);	
    
	return TRUE;  // If focus is not set on the control, return TRUE
}

//  To add minimize button to dialog box, the following code should be used to draw the icon. 
//  However, for the NFC application using file/Viewport model, it will be done by the architecture automatically

void CzyPrtLib_DemoDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // Draw the device context

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

		// Set the icon to the center of the rectangle
		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;

		// Draw icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//When user drag the minimize window, system will call this function to get cursor display
//
HCURSOR CzyPrtLib_DemoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CzyPrtLib_DemoDlg::OnBnClickedCancel()
{
	// TODO: Add control notification handler codes here

	// Delete LOGO
	if (m_pIcon) {
		delete m_pIcon;
	}

	OnCancel();
}

void CzyPrtLib_DemoDlg::InitLogo(void)
{
	CRect rc;
	m_btn_com.GetWindowRect(rc);
	ScreenToClient(rc);
	rc.top -= 15;
	rc.left = rc.right + 13;
	rc.bottom = rc.top + 128;
	rc.right = rc.left + 128;
	m_pIcon = new CStatic;
	if( m_pIcon->Create( NULL, WS_CHILD | SS_ICON | WS_VISIBLE | SS_CENTERIMAGE , rc, this, 0) )
		m_pIcon->SetIcon( (HICON)::LoadImage( AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ZLG), IMAGE_ICON, 128, 128, 0));
}


void CzyPrtLib_DemoDlg::InitCom(void)
{
	HKEY   hKey;  	

	if(::RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Hardware\\DeviceMap\\SerialComm", NULL, KEY_READ, &hKey) == ERROR_SUCCESS)
	{  	
		int   i=0; 		
		char   portName[256],commName[256];  		
		DWORD   dwLong,dwSize;  		

		while(1)  			
		{  			
			dwLong   =   dwSize   =   sizeof(portName);  
			if(::RegEnumValue(hKey, i, portName, &dwLong, NULL, NULL, (PUCHAR)commName, &dwSize) == ERROR_NO_MORE_ITEMS )
			{	// enum serial port	
				break;
			}

			m_combo_com.InsertString(i, commName);   // commName is serial port's name

			string str = commName;    //com port NO.		
			str = str.substr( 3 );
			int n = 0;
			sscanf_s( str.c_str(), "%d", &n);
			m_combo_com.SetItemData(i, n);

			i++;  			
		}  		

		if(m_combo_com.GetCount() == 0)  			
		{  			
			::AfxMessageBox("The seiral port available was not found in this computer.", MB_ICONINFORMATION);  
		}  		
		RegCloseKey(hKey);  		
	}
	m_combo_com.SetCurSel(0);
}

void CzyPrtLib_DemoDlg::InitCbx(void)
{
	InitCom();
	
	/* Baud rate is set to 9600 by default on software running */
	/* Not aLL the baud rates are supported by the serial port in computer */
	m_combo_baud.InsertString(0,"9600");
	m_combo_baud.InsertString(1,"115200");
	m_combo_baud.InsertString(2,"256000");
	m_combo_baud.InsertString(3,"500000");
	m_combo_baud.InsertString(4,"1000000");
	m_combo_baud.SetCurSel(0);
	
	m_combo_flowctrl.InsertString(0,"None");
	m_combo_flowctrl.InsertString(1,"Hardware flow control");
	m_combo_flowctrl.InsertString(2,"Software flow control");
	m_combo_flowctrl.SetCurSel(0);
}

void CzyPrtLib_DemoDlg::InitBtn(void) 
{
	m_btn_com.SetShade(CShadeButtonST::SHS_SOFTBUMP);
	m_btn_com.SetIcon(IDI_ICON_OFF);

	m_btn_baud.SetShade(CShadeButtonST::SHS_SOFTBUMP);

	m_btn_flowctrl.SetShade(CShadeButtonST::SHS_SOFTBUMP);

	m_btn_heat.SetShade(CShadeButtonST::SHS_NOISE);
	m_btn_heat.SetIcon(IDI_ICON_OFF);

	m_btn_paper.SetShade(CShadeButtonST::SHS_NOISE);
	m_btn_paper.SetIcon(IDI_ICON_OFF);

	m_btn_axoopen.SetShade(CShadeButtonST::SHS_NOISE);
	m_btn_axoopen.SetIcon(IDI_ICON_OFF);

	m_btn_overvoltage.SetShade(CShadeButtonST::SHS_NOISE);
	m_btn_overvoltage.SetIcon(IDI_ICON_OFF);

	m_btn_cutter.SetShade(CShadeButtonST::SHS_NOISE);
	m_btn_cutter.SetIcon(IDI_ICON_OFF);
}

void CzyPrtLib_DemoDlg::OnBnClickedBtnCom()
{
	// TODO: Add control notification handler codes here
	if (IsOpen())
	{
		Close();
        InitBtn();

		m_btn_com.SetWindowText("Enable serial port");
		m_btn_com.SetIcon(IDI_ICON_OFF);
	}
	else
	{
		CString cstr, cstrTemp;
		int iPort = (int)m_combo_com.GetItemData(m_combo_com.GetCurSel());
		int iBaudrate = 0;
		m_combo_baud.GetLBText(m_combo_baud.GetCurSel(), cstr);
		sscanf_s(cstr, "%d", &iBaudrate);

		if(Open(iPort, iBaudrate))
		{	            
            // Set overtime to none
            LPCOMMTIMEOUTS _CO = GetTimeouts();
            _CO->ReadIntervalTimeout = 0;
            _CO->ReadTotalTimeoutConstant = MAXWORD;
            _CO->ReadTotalTimeoutMultiplier = 1;
            _CO->WriteTotalTimeoutConstant = MAXWORD;
            _CO->WriteTotalTimeoutMultiplier = 1;
            SetTimeouts(_CO);

            // Set flow control mode 
            int iflowctrl = (int)m_combo_flowctrl.GetCurSel();	
            DCB* pDcb = GetState();
            switch (iflowctrl) {
            case 0:
                /* None	*/
                pDcb->fOutX = FALSE;
                pDcb->fInX = FALSE;
                pDcb->fOutxCtsFlow = FALSE;		
                pDcb->fOutxDsrFlow = FALSE;
                pDcb->fRtsControl = DTR_CONTROL_DISABLE;
                pDcb->fDtrControl = DTR_CONTROL_DISABLE;			

                break;
            case 1:
                /* CTS flow control */
                pDcb->fOutX = FALSE;
                pDcb->fInX = FALSE;
                pDcb->fOutxCtsFlow = TRUE;		
                pDcb->fOutxDsrFlow = FALSE;
                pDcb->fRtsControl = DTR_CONTROL_DISABLE;
                pDcb->fDtrControl = DTR_CONTROL_DISABLE;	

                break;
            case 2:
                /* XonXoff flow control	*/
                pDcb->fOutX = TRUE;
                pDcb->fInX = FALSE;
                pDcb->fOutxCtsFlow = FALSE;		
                pDcb->fOutxDsrFlow = FALSE;
                pDcb->fRtsControl = DTR_CONTROL_DISABLE;
                pDcb->fDtrControl = DTR_CONTROL_DISABLE;			
                pDcb->XonChar = 0x11; 
                pDcb->XoffChar = 0x13;

                break;
            default:
                break;
            }	
            SetState(pDcb);

            m_btn_com.SetWindowText("Disable seiral port");
            m_btn_com.SetIcon(IDI_ICON_ON);
        }
		else
		{
			MessageBox("The serial port selected is in used or operation is incorrect, clear the selected seiral port");  
		}
	}
}

void CzyPrtLib_DemoDlg::OnBnClickedBtnBaud()
{
	// TODO: Add control notification handler codes here 

	if (!IsOpen()) {
        return;
    }

    CString cstr;
    int iBaudrate = 0;
    m_combo_baud.GetLBText(m_combo_baud.GetCurSel(), cstr);
    sscanf_s(cstr, "%d", &iBaudrate);
    
	EndThread();
		
	O_.SafeClear(true);

    //Clear the transmission buffer of serial port
    Purge(PURGE_TXABORT | PURGE_TXCLEAR);	

    // Turn off the flow control temporarily 

    DCB DCB_BACK_;
    memcpy(&DCB_BACK_, &DCB_, sizeof(DCB));
    /* None	*/
    DCB_.fOutX = FALSE;
    DCB_.fInX = FALSE;
    DCB_.fOutxCtsFlow = FALSE;		
    DCB_.fOutxDsrFlow = FALSE;
    DCB_.fRtsControl = DTR_CONTROL_DISABLE;
    DCB_.fDtrControl = DTR_CONTROL_DISABLE;
    SetState();          

	BeginThread();

    //Clear printer buffer 
    printerClearBuffer();	

    printerBaudRateSet(iBaudrate);	
	
	// Wait transmission complete, while Flush is invaild
   	Sleep(100); 

    // Resume flow control
    memcpy(&DCB_, &DCB_BACK_, sizeof(DCB));
   
    // Modify baudrate
    DCB_.BaudRate = iBaudrate;

    SetState();
}

void CzyPrtLib_DemoDlg::OnBnClickedBtnFlowctrl()
{
    // TODO: Add control notification handler codes here
    if (!IsOpen()) {
        return;
    }

	EndThread();

	O_.SafeClear(true);

    // Clear transmission buffer of seiral port 
    Purge(PURGE_TXABORT | PURGE_TXCLEAR);		

    //Turn off the flow control temporarily 
    
	/* None	*/
    DCB_.fOutX = FALSE;
    DCB_.fInX = FALSE;
    DCB_.fOutxCtsFlow = FALSE;		
    DCB_.fOutxDsrFlow = FALSE;
    DCB_.fRtsControl = DTR_CONTROL_DISABLE;
    DCB_.fDtrControl = DTR_CONTROL_DISABLE;
    SetState();     
	
	BeginThread();

    // Clear printer buffer
    printerClearBuffer();	

    /* Modify flow control mode */
    int iflowctrl = (int)m_combo_flowctrl.GetCurSel();	
    switch (iflowctrl) {
    case 0:
        printerFlowCtrlSet(FLOW_CTRL_NONE); 
        /* None	*/
        DCB_.fOutX = FALSE;
        DCB_.fInX = FALSE;
        DCB_.fOutxCtsFlow = FALSE;		
        DCB_.fOutxDsrFlow = FALSE;
        DCB_.fRtsControl = RTS_CONTROL_DISABLE;
        DCB_.fDtrControl = DTR_CONTROL_DISABLE;			

        break;
    case 1:        
		/* Send hardware flow control enable command */
        printerFlowCtrlSet(FLOW_CTRL_HARDWARE);  
        /* CTS flow control	*/
        DCB_.fOutX = FALSE;
        DCB_.fInX = FALSE;
        DCB_.fOutxCtsFlow = TRUE;		
        DCB_.fOutxDsrFlow = FALSE;
        DCB_.fRtsControl = RTS_CONTROL_DISABLE;
        DCB_.fDtrControl = DTR_CONTROL_DISABLE;	  	
        break;
    case 2:
        /* Send software flow control enable command */
        printerFlowCtrlSet(FLOW_CTRL_SOFTWARE);
        /* XonXoff flow control	*/
        DCB_.fOutX = TRUE;
        DCB_.fInX = FALSE;
        DCB_.fOutxCtsFlow = FALSE;		
        DCB_.fOutxDsrFlow = FALSE;
        DCB_.fRtsControl = RTS_CONTROL_DISABLE;
        DCB_.fDtrControl = DTR_CONTROL_DISABLE;			
        DCB_.XonChar = 0x11;
        DCB_.XoffChar = 0x13;		
        break;
    default:

        break;
    }

	// Wait transmission complete
	Sleep(100); 

    SetState();
}




void CzyPrtLib_DemoDlg::OnBnClickedBtn1()
{
	// TODO: Add control notification handler codes here
	/*
     *  Printing example-Font
     */
	fontTypeSet(FONT_TYPE_EMPHASIZE | FONT_TYPE_ITALIC);            /* Print example information                 */
    uartSendStr("Printing example: Font");
    print(); 
    fontTypeSet(FONT_TYPE_NORMAL);                                      /*  Reset the font to default setting        */ 

    fontTypeSet(FONT_TYPE_DOUBLE_HEIGHT | FONT_TYPE_DOUBLE_WIDTH);      /*  Set font as requred before printing      */
    uartSendStr("Double height+ doule width");				/*  Send the character string to be printed  */  
    fontTypeSet(FONT_TYPE_ITALIC | FONT_TYPE_UNDERLINE);     
    uartSendStr("Italic + underline");
    print();                                                            /*  Execute print() for printing         */
                                                                        /*  If the buffer within the printer is  */
                                                                        /*  full, printer will execute print()   */
                                                                        /*  function for printing                */
  
    fontTypeSet(FONT_TYPE_NORMAL);                                      /*  Reset the font to default setting    */

	print();                                                        /*  print() can be used to print blank line */
	print();                                                        /*  print() can be used to print blank line */
}

void CzyPrtLib_DemoDlg::OnBnClickedBtn2()
{
	// TODO: Add control notification handler codes here
	 /*
     * Printing example - Print and feed paper (Unit: dot)
     */ 
	fontTypeSet(FONT_TYPE_EMPHASIZE | FONT_TYPE_ITALIC);            /* Print example information               */
    uartSendStr("Print example: Print and feed paper (Unit: dot)");
    print();     
    fontTypeSet(FONT_TYPE_NORMAL);                                      /*  Reset the font to default setting      */ 
                                           
    uartSendStr("Print and feed paper for 100 dots");                   /*  Print information                      */
    printAndFeedNDotLine(100);                                          /*  Feed paper for 100 dots after printing */
                                                                        /*  A special line space can be made by    */
                                                                        /*  using paper feed                       */
    uartSendStr("Print and feed paper back for 24 dots "); 
    printAndBackFeedNDotLine(24);                                       /*  Feed paper bcak for 24 dots after printing */
                                                                        /*  Multiple printing can be done by using paper feed back */
    uartSendStr("Feed paper back and print ");                           
    print();

	/*
     *  Print exampel-Print and feed paper (Unit: dots)
     */
	print();                                                        /* print() can be used to print blank line     */
    fontTypeSet(FONT_TYPE_EMPHASIZE | FONT_TYPE_ITALIC);                /* Printing example information                */
    uartSendStr("Print exampel-Print and feed paper (Unit: dots)");
    print();      
    fontTypeSet(FONT_TYPE_NORMAL);                                      /*  Reset the font to default setting      */ 
                                         
    uartSendStr("Print and feed paper for 2 lines");                    /*  Print information                      */
    printAndFeedNFontLine(2);                                           /*  Feed paper for 2 lines after printing  */
                                                                        /*  A special line space can be made by */
                                                                        /*  using paper feed                    */
    uartSendStr("Print and feed paper back for 1 line"); 
    printAndBackFeedNFontLine(1);                                       /*  Feed paper back for 1 line after printing */
                                                                        /*  Multiple printing can be done by using paper feed back */
    uartSendStr("                 Feed paper and print");               /*  Notice that the height of line is affected by */
                                                                        /*  the setting of fontTypeSet */
    print();

	print();                                                        /*  print() can be used to print blank line */
	print();                                                        /*  print() can be used to print blank line */
}

void CzyPrtLib_DemoDlg::OnBnClickedBtn3()
{
	// TODO: Add control notification handler codes here
	/*
     * Printing example-Margin     */
	fontTypeSet(FONT_TYPE_EMPHASIZE | FONT_TYPE_ITALIC);            /* Print example information 		*/
    uartSendStr("Print example: Margin");
    print();
    fontTypeSet(FONT_TYPE_NORMAL);                                      /*  Reset the font to default setting   */ 

    leftMarginSet(10);                                                  /*  Set left margin to 80 dots          */
    rightMarginSet(10);                                                 /*  Set right margin to 80 dots         */
    uartSendStr("Left margin and right margin both are set to 80 dots, and printer buffer will control the printing automatically based on the margin setting");
    print();

	leftMarginSet(0);                                               /*  Reset left margin to 0 dots  	*/
    rightMarginSet(0);                                                  /*  Reset right margin to 0 dots        */

	print();                                                        /*  print() can be used to print blank line */
	print();                                                        /*  print() can be used to print blank line */
}


void CzyPrtLib_DemoDlg::OnBnClickedBtn4()
{
	// TODO: Add control notification handler codes here
	/*
     *  Printing example-absolute print position
     */
    fontTypeSet(FONT_TYPE_EMPHASIZE | FONT_TYPE_ITALIC);                /*  Print example information          */
    uartSendStr("Printing example: absolute print position");
    print();
    fontTypeSet(FONT_TYPE_NORMAL);                                      /*  Reset the font to default setting  */        
        
    abscissaSet(100);                                                   /*  Set the absolute print position to 100 dots     */
                                                                        /*  The setting by this function is effective until */
                                                                        /*  print() is executed                */
                                                                        /*  Margin setting dosen't affect the setting by this command */
                                                                       
    uartSendStr("Absolute print position is set to 100 dots");
    print();
    uartSendStr("The setting of absolute print position is only effective at one time");
	print();

	print();                                                           /*  print() can be used to print blank line */
	print();                                                           /*  print() can be used to print blank line */
}

void CzyPrtLib_DemoDlg::OnBnClickedBtn5()
{
	// TODO: Add control notification handler codes here
	/*
     *  Printing example-printing grayscale
     */
    fontTypeSet(FONT_TYPE_EMPHASIZE | FONT_TYPE_ITALIC);                /*  Print example information                  */
    uartSendStr("Printing example: grayscale");
    print();
    fontTypeSet(FONT_TYPE_NORMAL);                                      /*  Reset the font to default setting          */     

    fontGrayscaleSet(8);                                                /*  Set printing grayscale to 8£¨darkest£©     */
    uartSendStr("Printing grayscale is set to 8");                                         
    print();
    fontGrayscaleSet(4);                                                /*  Set printing grayscale to 4£¨moderate£©    */
    uartSendStr("Printing grayscale is set to 4");
    print();

	print();                                                        /*  print() can be used to print blank line    */
	print();                                                        /*  print() can be used to print blank line    */
}

void CzyPrtLib_DemoDlg::OnBnClickedBtn6()
{
	// TODO: Add control notification handler codes here
	/*
     * Horizontal tab position settign data, unit: 1mm (8 dots), vaild range: 0~47
     */
    unsigned char ucTab[] = {12, 24, 36};                               /*  Store horizontal tab position settings     */

	/*
     *  Printing example-Horizontal tab printing 
     */
    fontTypeSet(FONT_TYPE_EMPHASIZE | FONT_TYPE_ITALIC);                /*  Printing example information               */
    uartSendStr("Printing example: Horizontal tab printing");
    print();
    fontTypeSet(FONT_TYPE_NORMAL);                                      /*  Reset the font to default setting  */ 

    horizontalTabSet(ucTab, 3);                                         /*  Set the horizontal tab position    */

    fontTypeSet(FONT_TYPE_EMPHASIZE);                                   /*  Set the header font to bold        */
    uartSendStr("Item A");  
    horizontalTab();                                                    /*  Shift to next tab position         */
    uartSendStr("Item B"); 
    horizontalTab();                                                    /*  Shift to next tab position         */
    uartSendStr("Item C");                                                                            
    horizontalTab();                                                    /*  Shift to next tab position         */
    uartSendStr("Item D");
    print();                                                            /*  Print this line                    */

    fontTypeSet(FONT_TYPE_NORMAL);                                      /*  Set header font to normal          */
    uartSendStr("Data1");  
    horizontalTab();                                                    /*  Shift to next tab position         */
    uartSendStr("Data2");                                                   
    horizontalTab();                                                    /*  Shift to next tab position         */
    uartSendStr("Data3"); 
    horizontalTab();                                                    /*  Shift to next tab position         */
    uartSendStr("Data4");
	print();

	print();                                                        /*  print() can be used to print blank line */
	print();                                                        /*  print() can be used to print blank line */
}

void CzyPrtLib_DemoDlg::OnBnClickedBtn7()
{
	// TODO: Add control notification handler codes here
	
	unsigned char formLinePos[5] = {0, 8, 16, 24, 32};              /*  Set the vertical line position of vertical table */
    formItem formItem1[4] = {{ 2, 0x02, 9, "  Item A  "},               /*  Content in table item                    */
                             {10, 0x01, 7, "  Data 1"},
                             {18, 0x01, 7, "  Data 2"},
                             {26, 0x01, 7, "  Data 3"}};
    formItem formItem2[4] = {{ 2, 0x02, 9, "  Item B  "},
                             {10, 0x01, 7, "  Data 1"},
                             {18, 0x01, 7, "  Data 2"},
                             {26, 0x01, 7, "  Data 3"}};
    formItem formItem3[4] = {{ 2, 0x02, 9, "  Item C  "},
                             {10, 0x01, 7, "  Data 1"},
                             {18, 0x01, 7, "  Data 2"},
                             {26, 0x01, 7, "  Data 3"}};
    formItem formItem4[4] = {{ 2, 0x02, 9, "  Item D "},
                             {10, 0x01, 7, "  Data 1"},
                             {18, 0x01, 7, "  Data 2"},
                             {26, 0x01, 7, "  Data 3"}};
	
	/*
     *  Printing example-Print vertical table
     */
    fontTypeSet(FONT_TYPE_EMPHASIZE | FONT_TYPE_ITALIC);                /*  Printing example information       */
    uartSendStr("Printign example: Print vertical table; part of models are supported");
    print();
    fontTypeSet(FONT_TYPE_NORMAL);                                      /*  Reset the font to default setting  */   

    formPrintV(5, formLinePos, 4, &formItem1[0]);                       /*  Print vertical table               */
    formPrintV(5, formLinePos, 4, &formItem2[0]);
    formPrintV(5, formLinePos, 4, &formItem3[0]);
    formPrintV(5, formLinePos, 4, &formItem4[0]);

	print();                                                        /*  print() can be used to print blank line */
	print();                                                        /*  print() can be used to print blank line */
}

void CzyPrtLib_DemoDlg::OnBnClickedBtn8()
{
	// TODO: Add control notification handler codes here
	/*
     *  Printing example-Print raster bit image 
     */
    fontTypeSet(FONT_TYPE_EMPHASIZE | FONT_TYPE_ITALIC);                /*  Printing example information            */
    uartSendStr("Printing example: Print raster image");
    print();
    fontTypeSet(FONT_TYPE_NORMAL);                                      /*  Reset the font to default setting       */ 
    picturePrintH(PICTURE_TYPE_NORMAL,                                  /*  Set the rester bit image, the number of horizontal bytes: 48  */
                  48,                                                   /*  The number of vertical dots: 91         */
                  91,                                                  
                  (unsigned char *)&gImage_HPic[0]);
	print();                                                        /*  print() can be used to print blank line */
	print();                                                        /*  print() can be used to print blank line */
}


void CzyPrtLib_DemoDlg::OnBnClickedBtn9()
{
	// TODO: Add control notification handler codes here
	/*
	*  Printing example-Alignment mode
	*/
	fontTypeSet(FONT_TYPE_EMPHASIZE | FONT_TYPE_ITALIC);            /*  Printing examole information            */
	uartSendStr("Printing example: Alignment mode");
	print();
	fontTypeSet(FONT_TYPE_NORMAL);                                  /*  Reset the font to default setting       */ 

	horizontalAlign(ALIGN_LEFT);					/*  Set the alignment to left 		    */

	uartSendStr("Left");
	print();
	horizontalAlign(ALIGN_CENTER);					/*  Set the alignment to center		    */

	uartSendStr("Center");
	print();
	horizontalAlign(ALIGN_RIGHT);					/*  Set the alignment to right		    */

	uartSendStr("Right");
	print();
	horizontalAlign(ALIGN_LEFT);					/*  Set the alignment to left		    */	

	print();                                                        /*  print() can be used to print blank line */
	print();                                                        /*  print() can be used to print blank line */
                                                           
}

void CzyPrtLib_DemoDlg::OnBnClickedBtn10()
{
	// TODO: Add control notification handler codes here
	/*
     *  Printing example-Print one dimension bar code
     */
	fontTypeSet(FONT_TYPE_EMPHASIZE | FONT_TYPE_ITALIC);             /*  Print example information              */
    uartSendStr("Printing example: Print one-dimension code, part of models are supported³Ö");
    print();
    fontTypeSet(FONT_TYPE_NORMAL);                                       /*  Reset the font to default setting      */ 
	
    barcodeWidthSet(2);
    barcodeHeightSet(96);
    barcodeHriPosSet(2);
	barcodePrint(BARCODE_SYS_EAN13,(unsigned char *)"012345678900",12);

	print();                                                         /*  print() can be used to print blank line */
	print();
}

void CzyPrtLib_DemoDlg::OnBnClickedBtn11()
{
	// TODO: Add control notification handler codes here
	/*
	*  Printing example-Paper cut mode
	*/
	fontTypeSet(FONT_TYPE_EMPHASIZE | FONT_TYPE_ITALIC);              /*  Printing example information                */
	uartSendStr("Printing example: Paper cut mode; part of models are supported");
	print();
	fontTypeSet(FONT_TYPE_NORMAL);                                    /*  Reset the font to default setting           */ 

	uartSendStr("Half cut");
	print();
	printerCutPaper(50,CUT_TYPE_HALF);				  /*  Feed paper for 50 dots and cut paper */
	
	uartSendStr("Full cut");
	print();
	printerCutPaper(50,CUT_TYPE_ALL);				  /*  Feed paper for 50 dots and cut paper */

	                                                        
	print();                                                          /*  print() can be used to print blank line */
	print();                                                          /*  print() can be used to print blank line */

	
}


void CzyPrtLib_DemoDlg::OnBnClickedBtnSync()
{
    // TODO: Add control notification handler codes here
    if (!IsOpen())
        return;
	
	EndThread();

	O_.SafeClear(true);		

    // Clear the transmission buffer of seral port
    Purge(PURGE_TXABORT | PURGE_TXCLEAR);   

    //Turn off the flow control temporarily
    DCB DCB_BACK_;
    memcpy(&DCB_BACK_, &DCB_, sizeof(DCB));
    /* ÎŞ	*/
    DCB_.fOutX = FALSE;
    DCB_.fInX = FALSE;
    DCB_.fOutxCtsFlow = FALSE;		
    DCB_.fOutxDsrFlow = FALSE;
    DCB_.fRtsControl = DTR_CONTROL_DISABLE;
    DCB_.fDtrControl = DTR_CONTROL_DISABLE;
    SetState();     

	BeginThread();

    //Synchronize with printer state
    if (printerStateRT(NULL, 10) == false) {
    	MessageBox("Query is fail, printer has no response", "Prompt");
    }

	//Set printer states automatic back
    printerStateAutoUpload(PRINTER_STATE_OVERVOLATGE | PRINTER_STATE_AXOOPEN | PRINTER_STATE_OVERHEAT | PRINTER_STATE_OFPAPER  | PRINTER_STATE_CUTTERON);

	//Wait transmission completes
    Sleep(100);
	
    //Resume flow control
    memcpy(&DCB_, &DCB_BACK_, sizeof(DCB));
    SetState();  
}

//Over heat message handler
LRESULT CzyPrtLib_DemoDlg::OnOverHeat(WPARAM wParam, LPARAM lParam)
{
	m_btn_heat.SetIcon(IDI_ICON_ON);
	return 0;	
}

//Over heat resume message handler
LRESULT CzyPrtLib_DemoDlg::OnOverHeatResume(WPARAM wParam, LPARAM lParam)
{
	m_btn_heat.SetIcon(IDI_ICON_OFF);
	return 0;	
}

//Paper end massage handler
LRESULT CzyPrtLib_DemoDlg::OnOfPaper(WPARAM wParam, LPARAM lParam)
{
	m_btn_paper.SetIcon(IDI_ICON_ON);	
	return 0;	
}

//Paper end resume message handler
LRESULT CzyPrtLib_DemoDlg::OnOfPaperResume(WPARAM wParam, LPARAM lParam)
{
	m_btn_paper.SetIcon(IDI_ICON_OFF);
	return 0;	
}


//Platen open message handler
LRESULT CzyPrtLib_DemoDlg::OnAxoOpen(WPARAM wParam, LPARAM lParam)
{
	m_btn_axoopen.SetIcon(IDI_ICON_ON);	
	return 0;	
}

//Platen close message handler
LRESULT CzyPrtLib_DemoDlg::OnAxoClose(WPARAM wParam, LPARAM lParam)
{
	m_btn_axoopen.SetIcon(IDI_ICON_OFF);
	return 0;	
}


//Over voltage message handler
LRESULT CzyPrtLib_DemoDlg::OnOverVoltage(WPARAM wParam, LPARAM lParam)
{
	m_btn_overvoltage.SetIcon(IDI_ICON_ON);	
	return 0;	
}

//Over voltage resume message handler
LRESULT CzyPrtLib_DemoDlg::OnOverVoltageResume(WPARAM wParam, LPARAM lParam)
{
	m_btn_overvoltage.SetIcon(IDI_ICON_OFF);
	return 0;	
}


//Cutter down message handler
LRESULT CzyPrtLib_DemoDlg::OnCutterOn(WPARAM wParam, LPARAM lParam)
{
	m_btn_cutter.SetIcon(IDI_ICON_ON);
	return 0;	
}

//Cutter resume message handler
LRESULT CzyPrtLib_DemoDlg::OnCutterOff(WPARAM wParam, LPARAM lParam)
{
	m_btn_cutter.SetIcon(IDI_ICON_OFF);
	return 0;	
}