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

namespace LKCSharpSampleBM
{
	/// <summary>
	/// Form1에 대한 요약 설명입니다.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.Button btnPrintSample;
		private System.Windows.Forms.Button btnPrintBarCode;
		private System.Windows.Forms.Button btnPrintBitmap;
		private System.Windows.Forms.Button btnPrintText;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.Button btnOpen;
		private System.Windows.Forms.ComboBox cboPort;
		private System.Windows.Forms.Label label1;
		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;
		}
		/// <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.btnExit = new System.Windows.Forms.Button();
			this.btnPrintSample = new System.Windows.Forms.Button();
			this.btnPrintBarCode = new System.Windows.Forms.Button();
			this.btnPrintBitmap = new System.Windows.Forms.Button();
			this.btnPrintText = new System.Windows.Forms.Button();
			this.btnClose = new System.Windows.Forms.Button();
			this.btnOpen = new System.Windows.Forms.Button();
			this.cboPort = new System.Windows.Forms.ComboBox();
			this.label1 = new System.Windows.Forms.Label();
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(24, 223);
			this.btnExit.Size = new System.Drawing.Size(192, 32);
			this.btnExit.Text = "Exit";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// btnPrintSample
			// 
			this.btnPrintSample.Location = new System.Drawing.Point(24, 191);
			this.btnPrintSample.Size = new System.Drawing.Size(192, 24);
			this.btnPrintSample.Text = "Print TTF";
			this.btnPrintSample.Click += new System.EventHandler(this.btnPrintSample_Click);
			// 
			// btnPrintBarCode
			// 
			this.btnPrintBarCode.Location = new System.Drawing.Point(24, 159);
			this.btnPrintBarCode.Size = new System.Drawing.Size(192, 24);
			this.btnPrintBarCode.Text = "Print BarCode";
			this.btnPrintBarCode.Click += new System.EventHandler(this.btnPrintBarCode_Click);
			// 
			// btnPrintBitmap
			// 
			this.btnPrintBitmap.Location = new System.Drawing.Point(25, 127);
			this.btnPrintBitmap.Size = new System.Drawing.Size(192, 24);
			this.btnPrintBitmap.Text = "Print Bitmap";
			this.btnPrintBitmap.Click += new System.EventHandler(this.btnPrintBitmap_Click);
			// 
			// btnPrintText
			// 
			this.btnPrintText.Location = new System.Drawing.Point(24, 95);
			this.btnPrintText.Size = new System.Drawing.Size(192, 24);
			this.btnPrintText.Text = "Print Text";
			this.btnPrintText.Click += new System.EventHandler(this.btnPrintText_Click);
			// 
			// btnClose
			// 
			this.btnClose.Location = new System.Drawing.Point(144, 55);
			this.btnClose.Size = new System.Drawing.Size(72, 24);
			this.btnClose.Text = "Close";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// btnOpen
			// 
			this.btnOpen.Location = new System.Drawing.Point(144, 23);
			this.btnOpen.Size = new System.Drawing.Size(72, 24);
			this.btnOpen.Text = "Open";
			this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
			// 
			// cboPort
			// 
			this.cboPort.Location = new System.Drawing.Point(24, 31);
			this.cboPort.Size = new System.Drawing.Size(104, 20);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 15);
			this.label1.Size = new System.Drawing.Size(56, 16);
			this.label1.Text = "Port : ";
			// 
			// Form1
			// 
			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;

		}

		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;

		}

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

			lResult = LKPrint.PrinterCheck();

			switch(lResult)
			{
				case -1: // Bluetooth Write Fail. Please Check Connection.
					MessageBox.Show("Write Error"); 
					return;
				case -2: 
					MessageBox.Show("Fail to Read Printer Status");
					return;
			}
			lResult = LKPrint.status();
			if( lResult != LKPrint.LK_STS_NORMAL)
			{
				ErrMsg = string.Format("Printer Status get Error ErrorCode = %d", lResult);
				MessageBox.Show(ErrMsg);
				return;
			}

			LKPrint.BlackMarkON(280);
			LKPrint.PrintNormal("\x1b|rATEL (123)-456-7890\n\n\n");
			LKPrint.PrintNormal("\x1b|cAThank you for coming to our shop!\n");
			LKPrint.PrintNormal("\x1b|cADate\n\n");
			LKPrint.PrintNormal("Chicken                                   $10.00\n");
			LKPrint.PrintNormal("Hamburger                                 $20.00\n");
			LKPrint.PrintNormal("Pizza                                     $30.00\n");
			LKPrint.PrintNormal("Lemons                                    $40.00\n");
			LKPrint.PrintNormal("Drink                                     $50.00\n");
			LKPrint.PrintNormal("Excluded tax                             $150.00\n");
			LKPrint.PrintNormal("\x1b|uCTax(5%)                                    $7.50\n");
			LKPrint.PrintNormal("\x1b|bC\x1b|2CTotal            $157.50\n\n");
			LKPrint.PrintNormal("Payment                                  $200.00\n");
			LKPrint.PrintNormal("Change                                    $42.50\n\n");
			LKPrint.BlackMarkSearch();
		}

		private void btnPrintBitmap_Click(object sender, System.EventArgs e)
		{
			long lResult;
			string	ErrMsg;
			string strFileName = null;

			lResult = LKPrint.PrinterCheck();

			switch(lResult)
			{
				case -1: // Bluetooth Write Fail. Please Check Connection.
					MessageBox.Show("Write Error"); 
					return;
				case -2: 
					MessageBox.Show("Fail to Read Printer Status");
					return;
			}
			lResult = LKPrint.status();
			if( lResult != LKPrint.LK_STS_NORMAL)
			{
				ErrMsg = string.Format("Printer Status get Error ErrorCode = %d", lResult);
				MessageBox.Show(ErrMsg);
				return;
			}

			
			OpenFileDialog openFileDialog1 = new OpenFileDialog();

			openFileDialog1.InitialDirectory = ".\\" ;
			openFileDialog1.Filter = "Bitmap files (*.bmp)|*.bmp|All files (*.*)|*.*" ;
			openFileDialog1.FilterIndex = 1 ;

			if(openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				try
				{
					if ((strFileName = openFileDialog1.FileName) != null)
					{
						LKPrint.BlackMarkON(280);
						LKPrint.PrintBitmap(strFileName, 1, 0);
						LKPrint.BlackMarkSearch();
					}
				}
				catch (Exception ex)
				{
					MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
				}
			}

		}

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

			lResult = LKPrint.PrinterCheck();

			switch(lResult)
			{
				case -1: // Bluetooth Write Fail. Please Check Connection.
					MessageBox.Show("Write Error"); 
					return;
				case -2: 
					MessageBox.Show("Fail to Read Printer Status");
					return;
			}
			lResult = LKPrint.status();
			if( lResult != LKPrint.LK_STS_NORMAL)
			{
				ErrMsg = string.Format("Printer Status get Error ErrorCode = %d", lResult);
				MessageBox.Show(ErrMsg);
				return;
			}

			LKPrint.BlackMarkON(280);
			LKPrint.PrintNormal("-----start of barcode-----\n");
			LKPrint.PrintNormal("3 of 9 barcode\n");
			LKPrint.PrintBarCode("1234567890", LKPrint.LK_BCS_Code39, 40, 576, 1, 2);
			LKPrint.PrintNormal("Code 93 barcode\n");
			LKPrint.PrintBarCode("0123498765", LKPrint.LK_BCS_Code93, 40, 576, 1, 2);
			LKPrint.PrintNormal("Interleaved 2 of 5 barcode\n");
			LKPrint.PrintBarCode("0987654321", LKPrint.LK_BCS_ITF, 40, 576, 1, 2);
			LKPrint.PrintNormal("Code 128 A barcode\n");
			LKPrint.PrintBarCode("{ACODE 128", LKPrint.LK_BCS_Code128, 40, 576, 1, 2);
			LKPrint.PrintNormal("Code 128 B barcode\n");
			LKPrint.PrintBarCode("{BCode 128", LKPrint.LK_BCS_Code128, 40, 576, 1, 2);
			LKPrint.PrintNormal("Code 128 C barcode\n");
			LKPrint.PrintBarCode("{C12345", LKPrint.LK_BCS_Code128, 40, 576, 1, 2);
			LKPrint.PrintNormal("Codabar barcode\n");
			LKPrint.PrintBarCode("A1029384756A", LKPrint.LK_BCS_Codabar, 40, 576, 1, 2);
			LKPrint.PrintNormal("\n-----end of barcode-----\n\n");
			LKPrint.BlackMarkSearch();
		}

		private void btnPrintSample_Click(object sender, System.EventArgs e)
		{
			long lResult, PrintBitmap = 0;
			string	ErrMsg;
			string strFileName = null;

			lResult = LKPrint.PrinterCheck();

			switch(lResult)
			{
				case -1: // Bluetooth Write Fail. Please Check Connection.
					MessageBox.Show("Write Error"); 
					return;
				case -2: 
					MessageBox.Show("Fail to Read Printer Status");
					return;
			}
			lResult = LKPrint.status();
			if( lResult != LKPrint.LK_STS_NORMAL)
			{
				ErrMsg = string.Format("Printer Status get Error ErrorCode = %d", lResult);
				MessageBox.Show(ErrMsg);
				return;
			}

			
			OpenFileDialog openFileDialog1 = new OpenFileDialog();

			openFileDialog1.InitialDirectory = ".\\" ;
			openFileDialog1.Filter = "Bitmap files (*.bmp)|*.bmp|All files (*.*)|*.*" ;
			openFileDialog1.FilterIndex = 1 ;

			if(openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				try
				{
					if ((strFileName = openFileDialog1.FileName) != null)
					{
						PrintBitmap = 1;
					}
				}
				catch (Exception ex)
				{
					MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
				}
			}

			LKPrint.BlackMarkON(280);
			LKPrint.PrintNormal("----True-Type Font Printing.----\n");
			LKPrint.PrintNormal("Print Normal(24x24)\n");
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x00, 24, 2);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x05, 24, 2);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x06, 24, 2);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x08, 24, 2);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x88, 24, 2);
			LKPrint.PrintNormal("Print Italic(24x24)\n");
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x10, 24, 2);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x15, 24, 2);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x16, 24, 2);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x18, 24, 2);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x98, 24, 2);
			LKPrint.PrintNormal("Print Underline(24x24)\n");
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x20, 24, 2);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x25, 24, 2);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x26, 24, 2);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x28, 24, 2);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0xA8, 24, 2);

			LKPrint.PrintNormal("Print Normal(28x28)\n");
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x00, 28, 1);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x05, 28, 1);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x06, 28, 1);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x08, 28, 1);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x88, 28, 1);
			LKPrint.PrintNormal("Print Italic(28x28)\n");
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x10, 28, 1);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x15, 28, 1);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x16, 28, 1);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x18, 28, 1);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x98, 28, 1);
			LKPrint.PrintNormal("Print Underline(28x28)\n");
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x20, 28, 1);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x25, 28, 1);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x26, 28, 1);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x28, 28, 1);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0xA8, 28, 1);

			LKPrint.PrintNormal("Print Normal(32x32)\n");
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x00, 32, 0);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x05, 32, 0);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x06, 32, 0);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x08, 32, 0);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x88, 32, 0);
			LKPrint.PrintNormal("Print Italic(32x32)\n");
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x10, 32, 0);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x15, 32, 0);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x16, 32, 0);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x18, 32, 0);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x98, 32, 0);
			LKPrint.PrintNormal("Print Underline(32x32)\n");
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x20, 28, 0);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x25, 28, 0);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x26, 28, 0);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0x28, 28, 0);
			LKPrint.PrintText2Image("Tahoma Font", "Tahoma", 0xA8, 28, 0);

			LKPrint.PrintNormal("--True-Type Font Printing End.--\n\n");

			if(PrintBitmap == 1) LKPrint.PrintBitmap(strFileName, 1, 0);

			LKPrint.BlackMarkSearch();
		}

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

		}
	}
}
