// VC_SampleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VC_Sample.h"
#include "VC_SampleDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CVC_SampleDlg dialog

CVC_SampleDlg::CVC_SampleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVC_SampleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVC_SampleDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_hMyDevice=NULL;
}

void CVC_SampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVC_SampleDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVC_SampleDlg, CDialog)
	//{{AFX_MSG_MAP(CVC_SampleDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_TEST_PRINT, OnTestPrint)
	ON_WM_CLOSE()
	ON_BN_CLICKED(ID_TEST_LABELPRINT, OnTestLabelprint)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVC_SampleDlg message handlers

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

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// Open using Virtual LPT port(LPT2)
	m_hMyDevice = CreateFile("LPT2", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

	if (m_hMyDevice == INVALID_HANDLE_VALUE) // failed port open?
	{
		CancelIo(m_hMyDevice);
		m_hMyDevice = NULL;
		return FALSE;
	}

	COMMTIMEOUTS	cto;
	cto.ReadIntervalTimeout			= 10;
	cto.ReadTotalTimeoutConstant		= 10;
	cto.ReadTotalTimeoutMultiplier	= 10;
	cto.WriteTotalTimeoutConstant		= 0; //Timeout 
	cto.WriteTotalTimeoutMultiplier	= 0;

	if(!SetCommTimeouts(m_hMyDevice, &cto))
		AfxMessageBox("SetCommTimeouts Fail");
	
	// flush the port
	if(!PurgeComm(m_hMyDevice, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT))
		AfxMessageBox("PurgeComm Fail");

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CVC_SampleDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client 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 the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CVC_SampleDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

// for POS Printer
void CVC_SampleDlg::OnTestPrint() 
{
	// TODO: Add your control notification handler code here
	
	DWORD dwWritten = 0;
	
	//Feed and Cutting
	unsigned char command[]="\x1d\x56\x41\x00";
	
	dwWritten=Write((unsigned char*)"===================================\x0d\x0a",38);
	dwWritten=Write((unsigned char*)"====Virtual LPT for USB Driver=====\x0d\x0a",38);
	dwWritten=Write((unsigned char*)"\x0d\x0a",2);
	dwWritten=Write((unsigned char*)"======Visual C++ Sample Code=======\x0d\x0a",38);
	dwWritten=Write((unsigned char*)"===================================\x0d\x0a",38);
	dwWritten=Write(command, 4);

}

// for Label Printer
void CVC_SampleDlg::OnTestLabelprint() 
{
	DWORD dwWritten = 0;

	char szText1[] = "T50,100,3,1,1,0,0,N,N,'==================================='\r\n";
	char szText2[] = "T50,150,3,1,1,0,0,N,N,'====Virtual LPT for USB Driver====='\r\n";
	char szText3[] = "T50,200,3,1,1,0,0,N,N,'======Visual C++ Sample Code======='\r\n";
	char szText4[] = "T50,250,3,1,1,0,0,N,N,'==================================='\r\n";

	dwWritten=Write((unsigned char*)szText1, strlen(szText1));
	dwWritten=Write((unsigned char*)szText2, strlen(szText2));
	dwWritten=Write((unsigned char*)szText3, strlen(szText3));
	dwWritten=Write((unsigned char*)szText4, strlen(szText4));
	dwWritten=Write((unsigned char*)"P1\r\n", 4); // Prints...
}

// Write File Function
DWORD CVC_SampleDlg::Write(BYTE *pByte, DWORD dwWritten)
{
	DWORD dwWrittenResult=0;
	DWORD dwTotalWritten = 0;

	if(WriteFile(m_hMyDevice, pByte, dwWritten, &dwWrittenResult, NULL)==0)
	{
		DWORD nResult = GetLastError();

		CString str;
		str.Format("Write Failed...\nLastErrorCode : %d", nResult);

		AfxMessageBox(str);
		return 0;

	}
	
	return dwWrittenResult;
}



void CVC_SampleDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	
	// Close Hande
	if(m_hMyDevice!=NULL)
	{
		CloseHandle(m_hMyDevice);
	}
	

	CDialog::OnClose();
}


// ReadFile function
DWORD CVC_SampleDlg::Read(BYTE *pByte, DWORD dwRead)
{
	if(ReadFile(m_hMyDevice, (LPVOID)pByte, dwRead, &dwRead, NULL)==0)
	{
		AfxMessageBox("Read Failed...");
		return 0;

	}
	
	return dwRead;
}

