Home

File-Based Applications:
College Park Auto-Shop

 

Sending a Document to the Printer

One of the ways users print consists of sending the document to the printer. To directly send a document to the printer, you need to make sure that the control, whose value needs to be printed, supports printing. To accommodate the users of your application, you can provide a menu item or a button they would click. An example of such a button would be . To print, the user can click this button. That is the case when using WordPad: its Standard toolbar is equipped with such a button. With this type of printing, when the user decides to print, the whole document would be printed "as is", in color if the document is colored and if the printer supports colors. If there is more than one printer, the computer would use what is known as the default printer.

Providing a Printer

If you want users to be able to configure or customize the printing process, Microsoft Windows provides a common dialog box called Print:

The Print dialog box allows a user to select a printer if more than one is available. The user can decide either to print the whole document, to print a range of pages, or to print a portion of the document that was previously selected. The user can also decide on the number of copies to print from the document, the range specified, or the selected portion. Furthermore, the user can access the particular characteristics of the selected printer and specify how the printer should perform the job. For example, if the selected printer can print in color and the document is in color but the user wants to print in black and white, he or she can specify this using the Properties button.

To provide the users with the ability to customize printing through the Print dialog box, you can add a PrintDialog object from the Toolbox to your form. The PrintDialog control is implemented through the PrintDialog class of the System::Windows::Forms namespace. To programmatically create a PrintDialog object, you can declare a pointer to PrinterDialog. Here is an example:

System::Void btnPrint_Click(System::Object *  sender, System::EventArgs *  e)
{
	 PrintDialog *dlgPrint = new PrintDialog;
}

To present the Print dialog box to the user, you can call its ShowDialog() method.

 

Practical LearningPractical Learning: Providing a Print Dialog Box

  • Display the form. From the Toolbox, click the PrintDialog button and click the form

The Document to Print

In order to print, the Print dialog box must be given a document to print. This means that you must first prepare a document prior to printing. To support this, the .NET Framework provides the PrintDocument class that is defined in the System::Drawing::Printing namespace. This class is represented in the Toolbox by the PrintDocument button. Based on this, to prepare a document for printing, you can either add a PrintDocument object to your project or declare a pointer to PrintDocument. Here is an example:

System::Void btnPrint_Click(System::Object *  sender, System::EventArgs *  e)
{
	 PrintDialog       *dlgPrint = new PrintDialog;
	 PrintDocument *docPrint = new PrintDocument;
}

After creating the document to print through a PrintDocument object, you can associate it with a PrintDialog. To support this, the PrintDialog class is equipped with the Document property. To specify the object that would carry the printing, you can assign the PrintDocument object to the PrintDialog::Document property. Here is an example:

System::Void btnPrint_Click(System::Object *  sender, System::EventArgs *  e)
{
	 PrintDialog       *dlgPrint = new PrintDialog;
	 PrintDocument *docPrint = new PrintDocument;

	 dlgPrint->Document = docPrint;
	 dlgPrint->ShowDialog();
}

A document to print can be made of only one or many pages. Each page has a number of characteristics. The characteristics of a page are controlled by the PrintDocument::PageSettings property which itself is based on the PageSettings class. The PageSettings class is defined in the System::Drawing::Printing namespace. This class holds the dimensions of the page, the values of the margins applied on the page, the tray that would supply the paper (since some printers have many trays), the printer resolution, whether the page would be printed in color or black and white, whether the page would be printed in Portrait or Landscape orientation, etc. If you don't want to specify these characteristics, you can set the PrintDocument::PageSettings property to DefaultPageSettings.

If you know the name of the document to be printed, you can assign it to the PrintDocument::DocumentName property. Here is an example:

System::Void btnPrint_Click(System::Object *  sender, System::EventArgs *  e)
{
	 PrintDialog   *dlgPrint = new PrintDialog;
	 PrintDocument *docPrint = new PrintDocument;

	 docPrint->DocumentName  = S"Exercise";
	 dlgPrint->Document      = docPrint;

	 dlgPrint->ShowDialog();
}

To actually print the document, you can call the PrintDocument::Print() method. Its syntax is:

public: void Print();

Practical LearningPractical Learning: Providing the Document to Print

  1. From the Toolbox, click the PrintDocument button and click the form
  2. On the bar under the form, click printDialog1
  3. In the Properties window, set its Document to printDocument1

Events Related to Printing

When the PrintDocument::Print() method is called, the printing process would start by firing the BeginPrint event but this event occurs before the first page is printed. The BeginPrint event is of type PrintEventArgs which doesn't hold any particular information, especially for the BeginPrint event. This event allows you to take some early actions, if necessary, before the printer receives the job.

Once the printer is ready, the application would then need to know what needs to be printed on the paper. At this time, the PrintPage event is fired. The PrintPage event is of type PrintPageEventArgs. The PrintPageEventArgs class allows you to fully customize the page of the document to be printed. For example, it is equipped with a Graphics property that allows you to "draw" anything you want on the paper. The PrintPageEventArgs class also allows you to get the location and dimensions of the area to be printed. This is represented by the PageBounds property, which produces a Rectangle object.

Once the PrintDocument object is ready, you must pass it to the Print dialog box. To do that, assign the name of the PrintDocument variable to the PrintDialog::Document property.

Practical LearningPractical Learning: Printing a Document

  1. On the bar under the form, double-click the printDocument1 button and implement its PrintPage event as follows:
     
    private: System::Void printDocument1_PrintPage(System::Object *  sender, 
    			System::Drawing::Printing::PrintPageEventArgs *  e)
    {
    	 e->Graphics->DrawLine(new Pen(Color::Black, 2), 60, 90, 680, 90);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 60, 93, 680, 93);
    
    	 String* strDisplay = S"College Park Auto-Shop";
    	 System::Drawing::Font *fntString = new 
    			        System::Drawing::Font(S"Times New Roman", 
    				28, FontStyle::Bold);
    	 e->Graphics->DrawString(strDisplay, fntString, 
    				Brushes::Black, 160, 100);
    			
    	 strDisplay = S"Customer Car Repair Order";
    	 fntString = new System::Drawing::Font(S"Times New Roman", 18, 
    						FontStyle::Bold);
    	 e->Graphics->DrawString(strDisplay, fntString, 
    						Brushes::Black, 220, 150);
    			
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					60, 184, 680, 184);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 2), 
    					60, 187, 680, 187);
    			 
    	 fntString = new System::Drawing::Font(S"Times New Roman", 12, 
    						FontStyle::Bold);
    	 e->Graphics->DrawString(S"::  Order Identification", fntString, 
    						Brushes::Black, 80, 200);
    
    	 e->Graphics->DrawLine(new Pen(Color::Black, 2), 
    					80, 220, 680, 220);
    
    	 e->Graphics->DrawString(S"Order #:", fntString, 
    						Brushes::Black, 500, 200);
    	 fntString = new System::Drawing::Font(S"Times New Roman", 12, 
    						FontStyle::Regular);
    	 e->Graphics->DrawString(this->txtOrderNumber->Text, fntString, 
    						Brushes::Black, 575, 200);
    	 
    	 fntString = new System::Drawing::Font(S"Times New Roman", 10, 
    						FontStyle::Bold);
    	 String *strDateTimeFormat[] = { L"D", L"T" };
    	 e->Graphics->DrawString(S"Order Date: ", fntString, 
    						Brushes::Black, 100, 230);
    
    	 fntString = new System::Drawing::Font(S"Times New Roman", 10, 
    						FontStyle::Regular);
    	 String *strDate = 
    		this->dtpOrderDate->Value.ToString(strDateTimeFormat[0], 
    					DateTimeFormatInfo::InvariantInfo);
    	 e->Graphics->DrawString(strDate, 
    			fntString, Brushes::Black, 200, 230);
    
    	 fntString = new System::Drawing::Font(S"Times New Roman", 10, 
    						FontStyle::Bold);
    	 e->Graphics->DrawString(S"Order Time:", fntString, 
    					Brushes::Black, 500, 230);
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    					10, FontStyle::Regular);
    	 String *strTime = 
    		this->dtpOrderTime->Value.ToString(strDateTimeFormat[1], 
    					DateTimeFormatInfo::InvariantInfo);
    	 e->Graphics->DrawString(strTime, fntString, 
    				Brushes::Black, 580, 230);
    
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    				100, 250, 640, 250);
    
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Bold);
    	 e->Graphics->DrawString(S"Customer Name:", fntString, 
    						Brushes::Black, 100, 260);
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Regular);
    	 e->Graphics->DrawString(this->txtCustomerName->Text, 
    				fntString, Brushes::Black, 260, 260);;
    	 
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					100, 280, 640, 280);
    
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Bold);
    	 e->Graphics->DrawString(S"Address:", fntString, 
    						Brushes::Black, 100, 290);
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Regular);
    	 e->Graphics->DrawString(this->txtAddress->Text, fntString, 
    						Brushes::Black, 260, 290);;
    	 
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					100, 310, 640, 310);
    
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Regular);
    	 String *strAddress = String::Concat(this->txtCity->Text, S", ", 
    					     this->txtState->Text, S" ", 
    					     this->txtZIPCode->Text);
    	 e->Graphics->DrawString(strAddress, fntString, 
    					     Brushes::Black, 260, 320);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					     100, 340, 640, 340);
    	 
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Bold);
    	 e->Graphics->DrawString(S"Car:", fntString, 
    				Brushes::Black, 100, 350);
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Regular);
    	 String *strCar = String::Concat(this->txtMake->Text, S", ", 
    					this->txtModel->Text, S", ", 
    					this->txtCarYear->Text);
    	 e->Graphics->DrawString(strCar, fntString, 
    				Brushes::Black, 260, 350);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    				100, 370, 640, 370);
    
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    					10, FontStyle::Bold);
    	 e->Graphics->DrawString(S"Problem Description:", fntString, 
    					Brushes::Black, 100, 380);
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    					10, FontStyle::Regular);
    	 e->Graphics->DrawString(this->txtProblem->Text, 
    				fntString, Brushes::Black, 
    				RectangleF(260, 380, 420, 380));
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					100, 400, 640, 400);
    			 
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						12, FontStyle::Bold);
    	 e->Graphics->DrawString(S"::  Parts Used", fntString, 
    						Brushes::Black, 80, 430);
    
    	 e->Graphics->DrawLine(new Pen(Color::Black, 2), 
    					80, 450, 680, 450);
    
    	 e->Graphics->DrawString(S"Parts Name", fntString, 
    				 Brushes::Black, 100, 460);
    	 e->Graphics->DrawString(S"Unit Price", fntString, 
    				 Brushes::Black, 420, 460);
    	 e->Graphics->DrawString(S"Qty", fntString, 
    				 Brushes::Black, 520, 460);
    	 e->Graphics->DrawString(S"Sub-Total", fntString, 
    				 Brushes::Black, 562, 460);
    
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					100, 480, 640, 480);
    	 
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Regular);
    	 StringFormat *fmtString = new StringFormat();
    	 fmtString->Alignment = StringAlignment::Far;
    
    	 e->Graphics->DrawString(this->txtPartName1->Text,  fntString, 
    					Brushes::Black,  100, 490);
    	 e->Graphics->DrawString(this->txtUnitPrice1->Text, fntString, 
    					Brushes::Black, 
    					480, 490, fmtString);
    	 e->Graphics->DrawString(this->txtQuantity1->Text,  fntString, 
    					Brushes::Black, 
    					540, 490, fmtString);
    	 e->Graphics->DrawString(this->txtSubTotal1->Text,  fntString, 
    					Brushes::Black, 
    					630, 490, fmtString);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					100, 510, 640, 510);
    
    	 e->Graphics->DrawString(this->txtPartName2->Text,  fntString, 
    					Brushes::Black,  100, 520);
    	 e->Graphics->DrawString(this->txtUnitPrice2->Text, fntString, 
    					Brushes::Black, 
    					480, 520, fmtString);
    	 e->Graphics->DrawString(this->txtQuantity2->Text,  fntString, 
    					Brushes::Black, 540, 
    					520, fmtString);
    	 e->Graphics->DrawString(this->txtSubTotal2->Text,  fntString, 
    					Brushes::Black, 
    					630, 520, fmtString);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					100, 540, 640, 540);
    
    	 e->Graphics->DrawString(this->txtPartName3->Text,  fntString, 
    					Brushes::Black,  100, 550);
    	 e->Graphics->DrawString(this->txtUnitPrice3->Text, fntString, 
    					Brushes::Black, 
    					480, 550, fmtString);
    	 e->Graphics->DrawString(this->txtQuantity3->Text,  fntString, 
    					Brushes::Black, 
    					540, 550, fmtString);
    	 e->Graphics->DrawString(this->txtSubTotal3->Text,  fntString, 
    					Brushes::Black, 
    					630, 550, fmtString);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					100, 570, 640, 570);
    
    	 e->Graphics->DrawString(this->txtPartName4->Text,  fntString, 
    					Brushes::Black,  100, 580);
    	 e->Graphics->DrawString(this->txtUnitPrice4->Text, fntString, 
    					Brushes::Black, 
    					480, 580, fmtString);
    	 e->Graphics->DrawString(this->txtQuantity4->Text,  fntString, 
    					Brushes::Black, 
    					540, 580, fmtString);
    	 e->Graphics->DrawString(this->txtSubTotal4->Text,  fntString, 
    					Brushes::Black, 
    					630, 580, fmtString);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					100, 600, 640, 600);
    
    	 e->Graphics->DrawString(this->txtPartName5->Text,  fntString, 
    					Brushes::Black,  100, 610);
    	 e->Graphics->DrawString(this->txtUnitPrice5->Text, fntString, 
    					Brushes::Black, 
    					480, 610, fmtString);
    	 e->Graphics->DrawString(this->txtQuantity5->Text,  fntString, 
    					Brushes::Black, 
    					540, 610, fmtString);
    	 e->Graphics->DrawString(this->txtSubTotal5->Text,  fntString, 
    					Brushes::Black, 
    					630, 610, fmtString);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					100, 630, 640, 630);
    
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						12, FontStyle::Bold);
    	 e->Graphics->DrawString(S"::  Jobs Performed", fntString, 
    					Brushes::Black, 80, 650);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 2), 
    					80, 670, 680, 670);
    	 
    	 e->Graphics->DrawString(S"Job Name", fntString, 
    				 Brushes::Black, 100, 680);
    	 e->Graphics->DrawString(S"Price", fntString, 
    				 Brushes::Black, 562, 680);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    				 100, 700, 640, 700);
    	 
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Regular);
    	 
    	 e->Graphics->DrawString(this->txtJobPerformed1->Text, fntString, 
    					Brushes::Black, 100, 710);
    	 e->Graphics->DrawString(this->txtJobPrice1->Text, fntString, 
    					Brushes::Black, 
    					600, 710, fmtString);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					100, 730, 640, 730);
    
    	 e->Graphics->DrawString(this->txtJobPerformed2->Text, fntString, 
    					Brushes::Black, 100, 740);
    	 e->Graphics->DrawString(this->txtJobPrice2->Text, fntString, 
    					Brushes::Black, 
    					600, 740, fmtString);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					100, 760, 640, 760);
    
    	 e->Graphics->DrawString(this->txtJobPerformed3->Text, fntString, 
    					Brushes::Black, 100, 770);
    	 e->Graphics->DrawString(this->txtJobPrice3->Text, fntString, 
    					Brushes::Black, 
    					600, 770, fmtString);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					100, 790, 640, 790);
    
    	 e->Graphics->DrawString(this->txtJobPerformed4->Text, fntString, 
    					Brushes::Black, 100, 800);
    	 e->Graphics->DrawString(this->txtJobPrice4->Text, fntString, 
    					Brushes::Black, 
    					600, 800, fmtString);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					100, 820, 640, 820);
    
    	 e->Graphics->DrawString(this->txtJobPerformed5->Text, fntString, 
    					Brushes::Black, 100, 830);
    	 e->Graphics->DrawString(this->txtJobPrice5->Text, fntString, 
    					Brushes::Black, 
    					600, 830, fmtString);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 1), 
    					100, 850, 640, 850);
    
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						12, FontStyle::Bold);
    	 e->Graphics->DrawString(S":: Order Summary", fntString, 
    					Brushes::Black, 80, 870);
    	 e->Graphics->DrawLine(new Pen(Color::Black, 2), 
    					80, 890, 680, 890);
    
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    					10, FontStyle::Bold);
    	 e->Graphics->DrawString(S"Total Parts:", fntString, 
    				 Brushes::Black, 500, 900);
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Regular);
    	 e->Graphics->DrawString(this->txtTotalParts->Text, fntString, 
    					Brushes::Black, 
    					640, 900, fmtString);
    	 
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Bold);
    	 e->Graphics->DrawString(S"Total Labor:", fntString, 
    					Brushes::Black, 500, 920);
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Regular);
    	 e->Graphics->DrawString(this->txtTotalLabor->Text, fntString, 
    					Brushes::Black, 
    					640, 920, fmtString);
    	 
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Bold);
    	 e->Graphics->DrawString(S"Tax Rate:", fntString, 
    				Brushes::Black, 500, 940);
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Regular);
    	 e->Graphics->DrawString(this->txtTaxRate->Text, fntString, 
    					Brushes::Black, 
    					640, 940, fmtString);
    	 
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Bold);
    	 e->Graphics->DrawString(S"Tax Amount:", fntString, 
    				 Brushes::Black, 500, 960);
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Regular);
    	 e->Graphics->DrawString(this->txtTaxAmount->Text , fntString, 
    					Brushes::Black, 
    					640, 960, fmtString);
    	 
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Bold);
    	 e->Graphics->DrawString(S"Repair Total:", fntString, 
    				Brushes::Black, 500, 980);
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Regular);
    	 e->Graphics->DrawString(this->txtRepairTotal->Text, fntString, 
    					Brushes::Black, 
    					640, 980, fmtString);
    	 
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Bold);
    	 e->Graphics->DrawString(S"Recommendations:", fntString, 
    						Brushes::Black, 100, 900);
    	 fntString = new System::Drawing::Font(S"Times New Roman", 
    						10, FontStyle::Regular);
    	 e->Graphics->DrawString(this->txtRecommendations->Text, 
    				 fntString, Brushes::Black, 
    				 RectangleF(100, 920, 350, 280));
    }
  2. On the form, double-click the Print Repair Order button
  3. Implement its Click event as follows:
     
    private: System::Void mnuFilePrint_Click(System::Object *  sender, 
    			System::EventArgs *  e)
    {		
    	 if( this->printDialog1->ShowDialog() == DialogResult::OK )
    	 {
    		 this->printDocument1->Print();
    	 }
    }
  4. Execute the application and create an order then click File -> Print
  5. After printing the order, save it
  6. After using it, close the form and return to your programming environment
 

Previous Copyright © 2005-2016, FunctionX, Inc. Next