using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace LKCSharpSamplePrint
{
	/// <summary>
	/// Form1에 대한 요약 설명입니다.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ComboBox cboPort;
		private System.Windows.Forms.Button btnOpen;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.Button btnPrintText;
		private System.Windows.Forms.Button btnPrintBitmap;
		private System.Windows.Forms.Button btnPrintBarCode;
		private System.Windows.Forms.Button btnPrintSample;
		private System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.CheckBox checkBox1;
		private System.Windows.Forms.CheckBox checkBox2;
		private System.Windows.Forms.Button btnPrint2DBarcode;
		private System.Windows.Forms.MainMenu mainMenu1;

		public Form1()
		{
			//
			// Windows Form 디자이너 지원에 필요합니다.
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent를 호출한 다음 생성자 코드를 추가합니다.
			//
			cboPort.Items.Add("COM3:");
			cboPort.Items.Add("COM4:");
			cboPort.Items.Add("COM5:");
			cboPort.Items.Add("COM6:");
			cboPort.Items.Add("COM7:");
			cboPort.Items.Add("COM8:");
			cboPort.Items.Add("COM9:");
			cboPort.SelectedIndex = 6;

			btnClose.Enabled = false;
			btnPrintText.Enabled = false;
			btnPrintBitmap.Enabled = false;
			btnPrintBarCode.Enabled = false;
			btnPrintSample.Enabled = false;
			btnPrint2DBarcode.Enabled = false;

		}
		/// <summary>
		/// 사용 중인 모든 리소스를 정리합니다.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}
		#region Windows Form 디자이너에서 생성한 코드
		/// <summary>
		/// 디자이너 지원에 필요한 메서드입니다.
		/// 이 메서드의 내용을 코드 편집기로 수정하지 마십시오.
		/// </summary>
		private void InitializeComponent()
		{
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.label1 = new System.Windows.Forms.Label();
			this.cboPort = new System.Windows.Forms.ComboBox();
			this.btnOpen = new System.Windows.Forms.Button();
			this.btnClose = new System.Windows.Forms.Button();
			this.btnPrintText = new System.Windows.Forms.Button();
			this.btnPrintBitmap = new System.Windows.Forms.Button();
			this.btnPrintBarCode = new System.Windows.Forms.Button();
			this.btnPrintSample = new System.Windows.Forms.Button();
			this.btnExit = new System.Windows.Forms.Button();
			this.checkBox1 = new System.Windows.Forms.CheckBox();
			this.checkBox2 = new System.Windows.Forms.CheckBox();
			this.btnPrint2DBarcode = new System.Windows.Forms.Button();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 18);
			this.label1.Size = new System.Drawing.Size(40, 16);
			this.label1.Text = "Port : ";
			// 
			// cboPort
			// 
			this.cboPort.Location = new System.Drawing.Point(56, 16);
			this.cboPort.Size = new System.Drawing.Size(80, 20);
			// 
			// btnOpen
			// 
			this.btnOpen.Location = new System.Drawing.Point(136, 16);
			this.btnOpen.Size = new System.Drawing.Size(72, 24);
			this.btnOpen.Text = "Open";
			this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
			// 
			// btnClose
			// 
			this.btnClose.Location = new System.Drawing.Point(136, 48);
			this.btnClose.Size = new System.Drawing.Size(72, 24);
			this.btnClose.Text = "Close";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// btnPrintText
			// 
			this.btnPrintText.Location = new System.Drawing.Point(16, 88);
			this.btnPrintText.Size = new System.Drawing.Size(192, 24);
			this.btnPrintText.Text = "Print Text";
			this.btnPrintText.Click += new System.EventHandler(this.btnPrintText_Click);
			// 
			// btnPrintBitmap
			// 
			this.btnPrintBitmap.Location = new System.Drawing.Point(17, 120);
			this.btnPrintBitmap.Size = new System.Drawing.Size(192, 24);
			this.btnPrintBitmap.Text = "Print Bitmap";
			this.btnPrintBitmap.Click += new System.EventHandler(this.btnPrintBitmap_Click);
			// 
			// btnPrintBarCode
			// 
			this.btnPrintBarCode.Location = new System.Drawing.Point(16, 152);
			this.btnPrintBarCode.Size = new System.Drawing.Size(104, 24);
			this.btnPrintBarCode.Text = "Print BarCode";
			this.btnPrintBarCode.Click += new System.EventHandler(this.btnPrintBarCode_Click);
			// 
			// btnPrintSample
			// 
			this.btnPrintSample.Location = new System.Drawing.Point(128, 152);
			this.btnPrintSample.Size = new System.Drawing.Size(80, 24);
			this.btnPrintSample.Text = "Print TTF";
			this.btnPrintSample.Click += new System.EventHandler(this.btnPrintSample_Click);
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(16, 216);
			this.btnExit.Size = new System.Drawing.Size(192, 32);
			this.btnExit.Text = "Exit";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// checkBox1
			// 
			this.checkBox1.Location = new System.Drawing.Point(16, 40);
			this.checkBox1.Size = new System.Drawing.Size(104, 18);
			this.checkBox1.Text = "Printer Error";
			// 
			// checkBox2
			// 
			this.checkBox2.Location = new System.Drawing.Point(16, 64);
			this.checkBox2.Size = new System.Drawing.Size(104, 18);
			this.checkBox2.Text = "Battery Low";
			// 
			// btnPrint2DBarcode
			// 
			this.btnPrint2DBarcode.Location = new System.Drawing.Point(16, 184);
			this.btnPrint2DBarcode.Size = new System.Drawing.Size(192, 24);
			this.btnPrint2DBarcode.Text = "Print 2D Barcode";
			this.btnPrint2DBarcode.Click += new System.EventHandler(this.btnPrint2DBarcode_Click);
			// 
			// Form1
			// 
			this.ClientSize = new System.Drawing.Size(226, 270);
			this.Controls.Add(this.btnPrint2DBarcode);
			this.Controls.Add(this.checkBox2);
			this.Controls.Add(this.checkBox1);
			this.Controls.Add(this.btnExit);
			this.Controls.Add(this.btnPrintSample);
			this.Controls.Add(this.btnPrintBarCode);
			this.Controls.Add(this.btnPrintBitmap);
			this.Controls.Add(this.btnPrintText);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.btnOpen);
			this.Controls.Add(this.cboPort);
			this.Controls.Add(this.label1);
			this.Menu = this.mainMenu1;
			this.Text = "Form1";

		}
		#endregion

		/// <summary>
		/// 해당 응용 프로그램의 주 진입점입니다.
		/// </summary>

		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void btnOpen_Click(object sender, System.EventArgs e)
		{
			long lResult = LKPrint.OpenPort(cboPort.Text, 38400);
			if (lResult != 0)
			{
				MessageBox.Show("Open Port Failed");
				return;
			}

			btnOpen.Enabled = false;
			btnClose.Enabled = true;
			btnPrintText.Enabled = true;
			btnPrintBitmap.Enabled = true;
			btnPrintBarCode.Enabled = true;
			btnPrintSample.Enabled = true;
			btnPrint2DBarcode.Enabled = true;

		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			long lResult = LKPrint.ClosePort();
			if (lResult != 0)
			{
				MessageBox.Show("Close Port Failed!!!");
				return;
			}

			btnOpen.Enabled = true;
			btnClose.Enabled = false;
			btnPrintText.Enabled = false;
			btnPrintBitmap.Enabled = false;
			btnPrintBarCode.Enabled = false;
			btnPrintSample.Enabled = false;
			btnPrint2DBarcode.Enabled = false;

		}

		private void btnPrintText_Click(object sender, System.EventArgs e)
		{
			long lResult;
			int lState;

			lResult = LKPrint.PrinterCheck();
			switch(lResult)
			{
			case -1: // Bluetooth Write Fail. Please Check Connection.
				MessageBox.Show("Write Error"); 
				break;
			case -2: 
				MessageBox.Show("Fail to Read Printer Status");
				break;
			}
			lResult = LKPrint.status();

			checkBox1.Checked = false;
			checkBox2.Checked = false;

			if( (lResult & LKPrint.LK_STS_BUSY)	== LKPrint.LK_STS_BUSY)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_PAPER_EMPTY)	== LKPrint.LK_STS_PAPER_EMPTY)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_COVER_OPEN)	== LKPrint.LK_STS_COVER_OPEN)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_BATTERY_LOW)	== LKPrint.LK_STS_BATTERY_LOW)
			{
				checkBox2.Checked = true;
			}

			if((lResult & 0x0f) != 0x00) 
			{
				checkBox1.Checked = true;
				return;
			}


			LKPrint.SetForm("0", 200, 200, "406", 1);
			LKPrint.SetFormMode();

			LKPrint.CPCLPrintText(0, 0, 0, "1", "1",   "FONT-0-0", 0);
			LKPrint.CPCLPrintText(0, 0, 1, "1", "50",  "FONT-0-1", 0);
			LKPrint.CPCLPrintText(0, 4, 0, "1", "100", "FONT-4-0", 0);
			LKPrint.CPCLPrintText(0, 4, 1, "1", "150",  "FONT-4-1", 0);
			LKPrint.CPCLPrintText(0, 4, 2, "1", "260",  "4-2", 0);

			LKPrint.PrintForm();
		}

		private void btnPrintBitmap_Click(object sender, System.EventArgs e)
		{
			long lResult;
			int lState;

			lResult = LKPrint.PrinterCheck();
			switch(lResult)
			{
			case -1: // Bluetooth Write Fail. Please Check Connection.
				MessageBox.Show("Write Error"); 
				break;
			case -2: 
				MessageBox.Show("Fail to Read Printer Status");
				break;
			}
			lResult = LKPrint.status();

			checkBox1.Checked = false;
			checkBox2.Checked = false;

			if( (lResult & LKPrint.LK_STS_BUSY)	== LKPrint.LK_STS_BUSY)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_PAPER_EMPTY)	== LKPrint.LK_STS_PAPER_EMPTY)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_COVER_OPEN)	== LKPrint.LK_STS_COVER_OPEN)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_BATTERY_LOW)	== LKPrint.LK_STS_BATTERY_LOW)
			{
				checkBox2.Checked = true;
			}

			if((lResult & 0x0f) != 0x00) 
			{
				checkBox1.Checked = true;
				return;
			}


			LKPrint.SetForm("0", 200, 200, "406", 1);
			LKPrint.SetFormMode();
			// Printing the saved in flash memory.
			LKPrint.CPCLPrintImage("SAMPLE02.PCX", "12", "150");
			LKPrint.PrintForm();
		}

		private void btnPrintBarCode_Click(object sender, System.EventArgs e)
		{
			long lResult;
			int lState;

			lResult = LKPrint.PrinterCheck();
			switch(lResult)
			{
			case -1: // Bluetooth Write Fail. Please Check Connection.
				MessageBox.Show("Write Error"); 
				break;
			case -2: 
				MessageBox.Show("Fail to Read Printer Status");
				break;
			}
			lResult = LKPrint.status();

			checkBox1.Checked = false;
			checkBox2.Checked = false;

			if( (lResult & LKPrint.LK_STS_BUSY)	== LKPrint.LK_STS_BUSY)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_PAPER_EMPTY)	== LKPrint.LK_STS_PAPER_EMPTY)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_COVER_OPEN)	== LKPrint.LK_STS_COVER_OPEN)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_BATTERY_LOW)	== LKPrint.LK_STS_BATTERY_LOW)
			{
				checkBox2.Checked = true;
			}

			if((lResult & 0x0f) != 0x00) {
				checkBox1.Checked = true;
				return;
			}

			LKPrint.SetForm("0", 200, 200, "406", 1);
			LKPrint.SetFormMode();
			// CODABAR
			LKPrint.CPCLSetBarCode(0, 0, "0");
			LKPrint.CPCLPrintBarCode(0, 7, 2, 1, 30, "19", "45", "A37859B", 0);
			LKPrint.CPCLPrintText(0, 7, 0, "19", "18", "CODABAR", 0);
			// Code 39
			LKPrint.CPCLSetBarCode(0, 0, "0");
			LKPrint.CPCLPrintBarCode(0, 0, 2, 1, 30, "19", "130", "0123456", 10);
			LKPrint.CPCLPrintText(0, 7, 0, "21", "103", "CODE 39", 0);
			// PDF-417
			LKPrint.CPCL2DPrintBarCode(0, 0, "162", "234", 2, 10, 2, 1, "SEWOO TECH\r\nLK-P30");
			LKPrint.PrintForm();
		}

		private void btnPrintSample_Click(object sender, System.EventArgs e)
		{
			long lResult;
			int lState;

			lResult = LKPrint.PrinterCheck();
			switch(lResult)
			{
				case -1: // Bluetooth Write Fail. Please Check Connection.
					MessageBox.Show("Write Error");
					break;
				case -2: 
					MessageBox.Show("Fail to Read Printer Status");
					break;
			}
			lResult = LKPrint.status();

			checkBox1.Checked = false;
			checkBox2.Checked = false;

			if( (lResult & LKPrint.LK_STS_BUSY)	== LKPrint.LK_STS_BUSY)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_PAPER_EMPTY)	== LKPrint.LK_STS_PAPER_EMPTY)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_COVER_OPEN)	== LKPrint.LK_STS_COVER_OPEN)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_BATTERY_LOW)	== LKPrint.LK_STS_BATTERY_LOW)
			{
				checkBox2.Checked = true;
			}

			if((lResult & 0x0f) != 0x00) 
			{
				checkBox1.Checked = true;
				return;
			}


			LKPrint.SetForm("0", 200, 200, "406", 1);
			LKPrint.SetFormMode();
			LKPrint.CPCLPrintText2Image(0, "0", "0", "Tahoma", 0x00, 24, "Tahoma Font", 0);
			LKPrint.CPCLPrintText2Image(0, "0", "30", "Tahoma", 0x05, 24, "Tahoma Font", 0);
			LKPrint.CPCLPrintText2Image(0, "0", "60", "Tahoma", 0x06, 24, "Tahoma Font", 0);
			LKPrint.CPCLPrintText2Image(0, "0", "90", "Tahoma", 0x08, 24, "Tahoma Font", 0);
			LKPrint.CPCLPrintText2Image(0, "0", "120", "Tahoma", 0x08, 24, "Tahoma Font", 1);
			LKPrint.PrintForm();
		}

		private void btnExit_Click(object sender, System.EventArgs e)
		{
			this.Dispose(true);
		}

		private void btnPrint2DBarcode_Click(object sender, System.EventArgs e)
		{
			long lResult;
			int lState;

			lResult = LKPrint.PrinterCheck();
			switch(lResult)
			{
				case -1: // Bluetooth Write Fail. Please Check Connection.
					MessageBox.Show("Write Error");
					break;
				case -2: 
					MessageBox.Show("Fail to Read Printer Status");
					break;
			}
			lResult = LKPrint.status();

			checkBox1.Checked = false;
			checkBox2.Checked = false;

			if( (lResult & LKPrint.LK_STS_BUSY)	== LKPrint.LK_STS_BUSY)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_PAPER_EMPTY)	== LKPrint.LK_STS_PAPER_EMPTY)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_COVER_OPEN)	== LKPrint.LK_STS_COVER_OPEN)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_BATTERY_LOW)	== LKPrint.LK_STS_BATTERY_LOW)
			{
				checkBox2.Checked = true;
			}

			if((lResult & 0x0f) != 0x00) 
			{
				checkBox1.Checked = true;
				return;
			}


			// PDF-417
			LKPrint.SetForm("0", 200, 200, "406", 1);
			LKPrint.SetFormMode();
			LKPrint.CPCLPrintText(0, 7, 0, "0", "0",   "PDF417 TEST", 0);
			LKPrint.CPCL2DPrintBarCode(0, 0, "0", "36", 2, 10, 2, 1, "SEWOO TECH\r\nLK-P30");
			LKPrint.PrintForm();

			// QRCode
			LKPrint.SetForm("0", 200, 200, "406", 1);
			LKPrint.SetFormMode();
			LKPrint.CPCLPrintText(0, 7, 0, "0", "0",   "QRCODE TEST", 0);
			LKPrint.CPCL2DPrintBarCode(0, 1, "0", "36", 4, 0, 1, 0, "SEWOO TECH\r\nLK-P30");
			LKPrint.PrintForm();


			// DataMatrix
			LKPrint.SetForm("0", 200, 200, "406", 1);
			LKPrint.SetFormMode();
			LKPrint.CPCLPrintText(0, 7, 0, "0", "0",   "DataMatrix TEST", 0);
			LKPrint.CPCL2DPrintBarCode(0, 2, "0", "36", 6, 0, 100, 0, "SEWOO TECH\r\nLK-P30");
			LKPrint.PrintForm();
		}
	}
}
