www.pudn.com > CFPrinterClass_src.rar > SmartDevicePrintEngine.cs, change:2004-04-16,size:10269b


using System; 
using System.Windows.Forms; 
 
namespace CFPrinting 
{ 
	// ' *-----------------------------------------* 
	// * Mobile Print Engine (.NET CF)				* 
	// * Author : Orkun Gedik						* 
	// * e-mail : orkun.gedik@astron.yasar.com.tr	* 
	// ' *-----------------------------------------* 
	#region PrintEngine Class 
	public class SmartDevicePrintEngine 
	{ 
		// Local variables 
		private int iPageWidth; // Page width 
		private int iPageHeight; // Page Height 
		private int iBottomLine; // Bottom line 
		private int iPreviewPageIndex; // Page number 
		private string Row; // Single row on logical page 
		private char[,] PageBuffer; // Logical page 
		private CommDevice commDevice; // Device class 
		private System.Collections.ArrayList PageArray; // Array for print preview buffer 
		private System.Text.StringBuilder PreviewBuffer; // Preview buffer 
		private System.Windows.Forms.Form frmPrintPreview; // Print preview window 
		private System.Windows.Forms.TextBox txtPreview; // Print preview textbox 
		private System.Windows.Forms.Label lblPageNumber; // Print preview page nuber 
		private System.Windows.Forms.Button btnBack; // Create back button 
		private System.Windows.Forms.Button btnPrev; // Create previous button 
		private System.Windows.Forms.Button btnNext; // Create next button 
 
		// Constructor 
		public SmartDevicePrintEngine() 
		{ 
			// Create COM device 
			commDevice = new CommDevice(); 
 
			// Print preview arraylist 
			PageArray = new System.Collections.ArrayList(); 
		} 
		// Creates new page logical type 
		public bool CreatePage(int Width, int Height) 
		{ 
			try 
			{ 
				// Allocate page size on memory 
				PageBuffer = new char[Height, Width]; 
 
				// Set local variables 
				iPageWidth = Width; 
				iPageHeight = Height; 
				 
				// Set bottom line to 0 
				iBottomLine = 0; 
 
				return(true); 
			} 
			catch (Exception ex) 
			{ 
				// An exception occured 
				throw new MobilePrintEngineException("CreatePage() failed: " + ex.ToString()); 
			} 
		} 
 
		// Write to logical page 
		public void WriteToLogicalPage(int x, int y, string Value) 
		{ 
			int iLetterPos; // Position of word 
 
			// Set index base 1 
			x--; 
			y--; 
 
			// Page overflow check 
			if(((x + Value.Length)>iPageWidth || (x<0)) || 
				((y>iPageHeight) || (y<0))) 
				throw new MobilePrintEngineException("Page overflow"); 
 
			// Iterate letters in word 
			for(iLetterPos=0;iLetterPos!=Value.Length;iLetterPos++) 
			{ 
				// Get 1 byte(letter) and put into array (logical page) 
				PageBuffer[y, (x + iLetterPos)] = System.Convert.ToChar(Value.Substring(iLetterPos, 1)); 
			} 
 
			// Set bottom line 
			if(iBottomLine < y) 
				iBottomLine = y; 
		} 
		 
		// Write to logical page 
		public void WriteToLogicalPage(int x, int y, int Length, int Alignment, string Value) 
		{ 
			int iLetterPos; // Position of word 
 
			// Set index base 1 
			x--; 
			y--; 
 
			// Page overflow check 
			if(((x + Value.Length)>iPageWidth || (x<0)) || 
				((y>iPageHeight) || (y<0))) 
				throw new MobilePrintEngineException("Page overflow"); 
 
			// Restricted text overflow 
			if(Value.Length > Length)  
				throw new MobilePrintEngineException("Field overflow"); 
 
			switch(Alignment) 
			{ 
				case 0: 
					// LEFT 
					// Iterate letters in word 
					for(iLetterPos=0;iLetterPos!=Length;iLetterPos++) 
					{ 
						// Get 1 letter and put into array (logical page) 
						PageBuffer[y, (x + iLetterPos)] = Convert.ToChar(Value.Substring(iLetterPos, 1)); 
					} 
					break; 
				case 1: 
					// RIGHT 
					int iStartPos; // Start position 
 
					// Get start position 
					iStartPos = Length - Value.Length; 
 
					// Iterate letters in word 
					for(iLetterPos=0;iLetterPos!=iStartPos;iLetterPos++) 
					{ 
						// Put empty char 
						PageBuffer[y, (x + iLetterPos)] = ' '; 
					} 
 
					for(iLetterPos=iStartPos;iLetterPos!=Length;iLetterPos++) 
					{ 
						// Get 1 letter and put into array (logical page) 
						PageBuffer[y, (x + iLetterPos)] = Convert.ToChar(Value.Substring(iLetterPos - iStartPos, 1)); 
					} 
					break; 
				default: 
					throw new MobilePrintEngineException("Unexpected alignment"); 
			} 
			// Set bottom line 
			if(iBottomLine < y) 
				iBottomLine = y; 
		} 
		// Form feed 
		public void FormFeed() 
		{ 
			// Send form feed character 
			commDevice.Send(0x0C); 
		} 
		// Get logical row 
		private string ReadLogicalPageByRow(int RowNumber) 
		{ 
			int iCurrent; // Current row 
			 
			// Read into line for each char in array (array ~= a row in logical page) 
			for(iCurrent=0;iCurrent!=iPageWidth;iCurrent++) 
			{ 
				if('\0' == PageBuffer[RowNumber, iCurrent]) 
				{ 
					Row += " "; 
				} 
				else 
				{ 
					Row += Convert.ToString(PageBuffer[RowNumber, iCurrent]); 
				} 
			} 
			Row.Trim(); 
			return(Row.ToString()); 
		} 
		// Dispose the page 
		public void Dispose() 
		{ 
			PreviewBuffer = null; // Preview buffer area 
			PageBuffer = null; // Print buffer area 
			PageArray = null; // Page container for preview 
			Row = null; // Logical row string value 
			GC.Collect(); 
		} 
		// New Page 
		public void NewPage() 
		{ 
			// Clear current page 
			PageBuffer = null; 
			GC.Collect(); 
 
			// Create new page 
			CreatePage(iPageWidth, iPageHeight); 
		} 
		// Print the page 
		public void Print() 
		{ 
			// Init device 
			commDevice.Init(); 
 
			// Send data to device layer 
			commDevice.Send(PageBuffer); 
 
			// End of page 
			FormFeed(); 
 
			// Close COM port 
			commDevice.ClosePort(); 
		} 
		// Write to buffer for preview 
		public void AddToPreview() 
		{ 
			int iRow=0; // Row number 
			PreviewBuffer = new System.Text.StringBuilder(); // Buffer area 
			 
			for(iRow=0;iRow!=iBottomLine+1;iRow++) 
			{ 
				PreviewBuffer.Append(ReadLogicalPageByRow(iRow)); 
				PreviewBuffer.Append("\r\n"); 
				Row=""; 
			} 
			// Add to array 
			PageArray.Add(PreviewBuffer); 
		} 
		// Preview the page 
		public void Preview() 
		{	 
			// Create preview window dynamicaly 
 
			// Create form instance 
			frmPrintPreview = new Form(); 
			// Create Textbox 
			txtPreview = new TextBox(); 
			// Create page number indicator 
			lblPageNumber = new Label(); 
			// Create back button 
			btnBack = new Button(); 
			// Create previous button 
			btnPrev = new Button(); 
			// Create next button 
			btnNext = new Button(); 
 
			// Add controls to form object 
			frmPrintPreview.Controls.Add(txtPreview); 
			frmPrintPreview.Controls.Add(btnBack); 
			frmPrintPreview.Controls.Add(btnPrev); 
			frmPrintPreview.Controls.Add(btnNext); 
			frmPrintPreview.Controls.Add(lblPageNumber); 
 
			// Set a preview window controlbox 
			frmPrintPreview.ControlBox = false; 
 
			// txtPreview settings 
			txtPreview.Top = 0; 
			txtPreview.Left= 0; 
			txtPreview.Width = Screen.PrimaryScreen.Bounds.Width; 
			txtPreview.Height = Screen.PrimaryScreen.Bounds.Height - 70; 
			txtPreview.Multiline = true; 
			txtPreview.WordWrap = false; 
			txtPreview.ReadOnly = true; 
			txtPreview.ScrollBars = System.Windows.Forms.ScrollBars.Both; 
			txtPreview.Font= new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular); 
 
			// Back button 
			btnBack.Top = Screen.PrimaryScreen.Bounds.Height - 60; 
			btnBack.Width = 50; 
			btnBack.Height = 25; 
			btnBack.Left = 5; 
			btnBack.Text = "Back"; 
			btnBack.Click += new System.EventHandler(this.btnBack_Click); 
 
			// Previous button 
			btnPrev.Top = Screen.PrimaryScreen.Bounds.Height - 60; 
			btnPrev.Width = 50; 
			btnPrev.Height = 25; 
			btnPrev.Left = Screen.PrimaryScreen.Bounds.Width - 115; 
			btnPrev.Text = "Prev"; 
			btnPrev.Click += new System.EventHandler(this.btnPrev_Click); 
 
			// Next button 
			btnNext.Top = Screen.PrimaryScreen.Bounds.Height - 60; 
			btnNext.Width = 50; 
			btnNext.Height = 25; 
			btnNext.Left = Screen.PrimaryScreen.Bounds.Width - 55; 
			btnNext.Text = "Next"; 
			btnNext.Click += new System.EventHandler(this.btnNext_Click); 
 
			// Indicator label 
			lblPageNumber.Font= new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold); 
			lblPageNumber.Top = Screen.PrimaryScreen.Bounds.Height - 55; 
			lblPageNumber.Width = 90; 
			lblPageNumber.Height = 20; 
			lblPageNumber.Left = 70;			 
 
			// Set start page 
			iPreviewPageIndex = 0; 
			lblPageNumber.Text = "Page #"+(iPreviewPageIndex+1).ToString(); 
			txtPreview.Text = ((System.Text.StringBuilder)PageArray[iPreviewPageIndex]).ToString(); 
 
			// Show preview window 
			frmPrintPreview.Show(); 
		} 
		// Button event handlers 
		private void btnBack_Click(object sender, System.EventArgs e) 
		{ 
			// Dispose buffer area 
			PageArray.Clear(); 
			PreviewBuffer = null; 
			GC.Collect(); 
 
			// Dispose print preview window 
			lblPageNumber.Dispose(); 
			frmPrintPreview.Dispose(); 
			txtPreview.Dispose(); 
			btnPrev.Dispose(); 
			btnNext.Dispose(); 
		} 
		private void btnNext_Click(object sender, System.EventArgs e) 
		{ 
			// Set page preview into text box			 
			if(iPreviewPageIndex!=PageArray.Count-1) 
				iPreviewPageIndex++; // Go to next page 
			lblPageNumber.Text = "Page #"+(iPreviewPageIndex+1).ToString(); 
			txtPreview.Text = ((System.Text.StringBuilder)PageArray[iPreviewPageIndex]).ToString(); 
		} 
		private void btnPrev_Click(object sender, System.EventArgs e) 
		{ 
			// Set page preview into text box			 
			if(iPreviewPageIndex!=0) 
				iPreviewPageIndex--; // Go to previous page 
			lblPageNumber.Text = "Page #"+(iPreviewPageIndex+1).ToString(); 
			txtPreview.Text = ((System.Text.StringBuilder)PageArray[iPreviewPageIndex]).ToString();			 
		} 
		// Properties 
		public int PageWidth 
		{ 
			get 
			{ 
				return(iPageWidth); 
			} 
		} 
		public int PageHeight 
		{ 
			get 
			{ 
				return(iPageHeight); 
			} 
		} 
		public string Port 
		{ 
			set 
			{ 
				commDevice.Port=value; 
			} 
			get 
			{ 
				return(commDevice.Port); 
			} 
		} 
	} 
	#endregion 
 
	#region Mobile Print Engine Exception Class 
	public class MobilePrintEngineException : Exception 
	{ 
		public MobilePrintEngineException(string sException) 
		{ 
			MessageBox.Show("An error occured:"+sException, "MPE Exception"); 
		} 
	} 
	#endregion 
}