![]() |
File-Based Applications: |
In the above example, we saw a somewhat simplistic way of making the Print dialog box available to the user. This dialog box offers many options defined as the Printer Settings. To support the various options of a Print dialog box, the PrintDialog class is equipped with a property called PrinterSettings, which itself is defined from the PrinterSettings class, which holds all possible characteristics of a printer. As opposed to directly printing a file, a user may want to perform some preliminary preparation on the document or the printer. Microsoft Windows provides another dialog box used to control printing. It is called Page Setup To provide a Page Setup to your application, you can use the PageSetupDialog button from the Toolbox. The PageSetupDialog control is based on the PageSetupDialog class which derives from the CommonDialog class. When using the Page Setup dialog box, the user must first select a printer. This is usually done already by the operating system that selects the default printer of the computer that called this dialog box. Otherwise, to select a printer or to change the printer, the user can click the Printer button and select or change it using the Name combo box. Microsoft Visual Studio .NET provides the ability to preview a document before printing it. To use it, you can use the appropriate control but you must "draw" what you are planning to print. To start, you can create a Windows Forms Application with a form designed as you wish. Here is an example: ![]() You can then add a PrintDocument and a PrintPreviewDialog controls to your form. When the user clicks the menu item or button you had provided for print preview, you can implement its event by drawing what will be printing, retrieving values from existing controls. Here is an example from the above form: |
#pragma once namespace WinForms4 { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// <summary> /// Summary for Form1 /// /// WARNING: If you change the name of this class, you will need to change the /// 'Resource File Name' property for the managed resource compiler tool /// associated with all .resx files this class depends on. Otherwise, /// the designers will not be able to interact properly with localized /// resources associated with this form. /// </summary> public __gc class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } protected: void Dispose(Boolean disposing) { if (disposing && components) { components->Dispose(); } __super::Dispose(disposing); } private: System::Windows::Forms::Label * label1; private: System::Windows::Forms::TextBox * txtNumber1; private: System::Windows::Forms::TextBox * txtNumber2; private: System::Windows::Forms::Label * label2; private: System::Windows::Forms::Button * btnAdd; private: System::Windows::Forms::Button * btnSubtract; private: System::Windows::Forms::Button * btnDivide; private: System::Windows::Forms::Button * btnMultiply; private: System::Windows::Forms::TextBox * txtResult; private: System::Windows::Forms::Label * label3; private: System::Windows::Forms::Button * btnPreview; private: System::Drawing::Printing::PrintDocument * printDocument1; private: System::Windows::Forms::PrintPreviewDialog * printPreviewDialog1; private: System::Windows::Forms::Button * btnClose; private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container * components; /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> void InitializeComponent(void) { System::Resources::ResourceManager * resources = new System::Resources::ResourceManager(__typeof(WinForms4::Form1)); this->label1 = new System::Windows::Forms::Label(); this->txtNumber1 = new System::Windows::Forms::TextBox(); this->txtNumber2 = new System::Windows::Forms::TextBox(); this->label2 = new System::Windows::Forms::Label(); this->btnAdd = new System::Windows::Forms::Button(); this->btnSubtract = new System::Windows::Forms::Button(); this->btnDivide = new System::Windows::Forms::Button(); this->btnMultiply = new System::Windows::Forms::Button(); this->txtResult = new System::Windows::Forms::TextBox(); this->label3 = new System::Windows::Forms::Label(); this->btnPreview = new System::Windows::Forms::Button(); this->printDocument1 = new System::Drawing::Printing::PrintDocument(); this->printPreviewDialog1 = new System::Windows::Forms::PrintPreviewDialog(); this->btnClose = new System::Windows::Forms::Button(); this->SuspendLayout(); // // label1 // this->label1->Location = System::Drawing::Point(16, 21); this->label1->Name = S"label1"; this->label1->Size = System::Drawing::Size(80, 16); this->label1->TabIndex = 0; this->label1->Text = S"First Number:"; // // txtNumber1 // this->txtNumber1->Location = System::Drawing::Point(112, 16); this->txtNumber1->Name = S"txtNumber1"; this->txtNumber1->Size = System::Drawing::Size(80, 20); this->txtNumber1->TabIndex = 1; this->txtNumber1->Text = S"125.58"; this->txtNumber1->TextAlign = System::Windows::Forms::HorizontalAlignment::Right; // // txtNumber2 // this->txtNumber2->Location = System::Drawing::Point(112, 48); this->txtNumber2->Name = S"txtNumber2"; this->txtNumber2->Size = System::Drawing::Size(80, 20); this->txtNumber2->TabIndex = 3; this->txtNumber2->Text = S"64.26"; this->txtNumber2->TextAlign = System::Windows::Forms::HorizontalAlignment::Right; // // label2 // this->label2->Location = System::Drawing::Point(16, 53); this->label2->Name = S"label2"; this->label2->Size = System::Drawing::Size(96, 16); this->label2->TabIndex = 2; this->label2->Text = S"Second Number:"; // // btnAdd // this->btnAdd->Location = System::Drawing::Point(8, 88); this->btnAdd->Name = S"btnAdd"; this->btnAdd->Size = System::Drawing::Size(56, 23); this->btnAdd->TabIndex = 4; this->btnAdd->Text = S"Add"; this->btnAdd->Click += new System::EventHandler(this, btnAdd_Click); // // btnSubtract // this->btnSubtract->Location = System::Drawing::Point(64, 88); this->btnSubtract->Name = S"btnSubtract"; this->btnSubtract->Size = System::Drawing::Size(56, 23); this->btnSubtract->TabIndex = 5; this->btnSubtract->Text = S"Subtract"; this->btnSubtract->Click += new System::EventHandler(this, btnSubtract_Click); // // btnDivide // this->btnDivide->Location = System::Drawing::Point(176, 88); this->btnDivide->Name = S"btnDivide"; this->btnDivide->Size = System::Drawing::Size(56, 23); this->btnDivide->TabIndex = 7; this->btnDivide->Text = S"Divide"; this->btnDivide->Click += new System::EventHandler(this, btnDivide_Click); // // btnMultiply // this->btnMultiply->Location = System::Drawing::Point(120, 88); this->btnMultiply->Name = S"btnMultiply"; this->btnMultiply->Size = System::Drawing::Size(56, 23); this->btnMultiply->TabIndex = 6; this->btnMultiply->Text = S"Multiply"; this->btnMultiply->Click += new System::EventHandler(this, btnMultiply_Click); // // txtResult // this->txtResult->Location = System::Drawing::Point(112, 128); this->txtResult->Name = S"txtResult"; this->txtResult->Size = System::Drawing::Size(88, 20); this->txtResult->TabIndex = 8; this->txtResult->Text = S"8069.7708"; this->txtResult->TextAlign = System::Windows::Forms::HorizontalAlignment::Right; // // label3 // this->label3->Location = System::Drawing::Point(24, 132); this->label3->Name = S"label3"; this->label3->Size = System::Drawing::Size(72, 16); this->label3->TabIndex = 9; this->label3->Text = S"Result:"; // // btnPreview // this->btnPreview->Location = System::Drawing::Point(8, 160); this->btnPreview->Name = S"btnPreview"; this->btnPreview->Size = System::Drawing::Size(88, 23); this->btnPreview->TabIndex = 10; this->btnPreview->Text = S"Preview"; this->btnPreview->Click += new System::EventHandler(this, btnPreview_Click); // // printDocument1 // this->printDocument1->PrintPage += new System::Drawing::Printing::PrintPageEventHandler(this, printDocument1_PrintPage); // // printPreviewDialog1 // this->printPreviewDialog1->AutoScrollMargin = System::Drawing::Size(0, 0); this->printPreviewDialog1->AutoScrollMinSize = System::Drawing::Size(0, 0); this->printPreviewDialog1->ClientSize = System::Drawing::Size(400, 300); this->printPreviewDialog1->Document = this->printDocument1; this->printPreviewDialog1->Enabled = true; this->printPreviewDialog1->Icon = (__try_cast<System::Drawing::Icon * >(resources->GetObject(S"printPreviewDialog1.Icon"))); this->printPreviewDialog1->Location = System::Drawing::Point(141, 19); this->printPreviewDialog1->MinimumSize = System::Drawing::Size(375, 250); this->printPreviewDialog1->Name = S"printPreviewDialog1"; this->printPreviewDialog1->TransparencyKey = System::Drawing::Color::Empty; this->printPreviewDialog1->Visible = false; // // btnClose // this->btnClose->Location = System::Drawing::Point(112, 160); this->btnClose->Name = S"btnClose"; this->btnClose->Size = System::Drawing::Size(88, 23); this->btnClose->TabIndex = 11; this->btnClose->Text = S"Close"; this->btnClose->Click += new System::EventHandler(this, btnClose_Click); // // Form1 // this->AutoScaleBaseSize = System::Drawing::Size(5, 13); this->ClientSize = System::Drawing::Size(240, 198); this->Controls->Add(this->btnClose); this->Controls->Add(this->btnPreview); this->Controls->Add(this->label3); this->Controls->Add(this->txtResult); this->Controls->Add(this->btnDivide); this->Controls->Add(this->btnMultiply); this->Controls->Add(this->btnSubtract); this->Controls->Add(this->btnAdd); this->Controls->Add(this->txtNumber2); this->Controls->Add(this->label2); this->Controls->Add(this->txtNumber1); this->Controls->Add(this->label1); this->MaximizeBox = false; this->Name = S"Form1"; this->StartPosition = System::Windows::Forms::FormStartPosition::CenterParent; this->Text = S"Calculations"; this->ResumeLayout(false); } private: System::Void btnAdd_Click(System::Object * sender, System::EventArgs * e) { Double Number1, Number2, Result; Number1 = txtNumber1->Text->ToDouble(0); Number2 = txtNumber2->Text->ToDouble(0); Result = Number1 + Number2; txtResult->Text = Result.ToString(); } private: System::Void btnSubtract_Click(System::Object * sender, System::EventArgs * e) { Double Number1, Number2, Result; Number1 = txtNumber1->Text->ToDouble(0); Number2 = txtNumber2->Text->ToDouble(0); Result = Number1 - Number2; txtResult->Text = Result.ToString(); } private: System::Void btnMultiply_Click(System::Object * sender, System::EventArgs * e) { Double Number1, Number2, Result; Number1 = txtNumber1->Text->ToDouble(0); Number2 = txtNumber2->Text->ToDouble(0); Result = Number1 * Number2; txtResult->Text = Result.ToString(); } private: System::Void btnDivide_Click(System::Object * sender, System::EventArgs * e) { Double Number1, Number2, Result; Number1 = txtNumber1->Text->ToDouble(0); Number2 = txtNumber2->Text->ToDouble(0); Result = Number1 / Number2; txtResult->Text = Result.ToString(); } private: System::Void btnPreview_Click(System::Object * sender, System::EventArgs * e) { this->printPreviewDialog1->ShowDialog(); } private: System::Void printDocument1_PrintPage(System::Object * sender, System::Drawing::Printing::PrintPageEventArgs * e) { System::Drawing::Font *ft = new System::Drawing::Font(S"Garamond", 24, FontStyle::Bold); int xpos = e->MarginBounds.Left; int ypos = e->MarginBounds.Top; int lineHeight = 50; e->Graphics->DrawString(S"Arithmetic Calculations", ft, Brushes::Red, static_cast<Single>(xpos), static_cast<Single>(ypos)); ypos += lineHeight; ft = new System::Drawing::Font(S"Times New Roman", 12, FontStyle::Regular); e->Graphics->DrawString(String::Concat(S"Number 1: ", this->txtNumber1->Text), ft, Brushes::Blue, static_cast<Single>(xpos), static_cast<Single>(ypos)); ypos += lineHeight; e->Graphics->DrawString(String::Concat(S"Number 2: ", this->txtNumber2->Text), ft, Brushes::Blue, static_cast<Single>(xpos), static_cast<Single>(ypos)); e->Graphics->DrawLine(new Pen(Color::Blue), Point(20, ypos+20), Point(340, ypos+20)); ypos += lineHeight; ft = new System::Drawing::Font(S"Times New Roman", 14, FontStyle::Regular); e->Graphics->DrawString(String::Concat(S"Result: ", this->txtResult->Text), ft, Brushes::Blue, static_cast<Single>(xpos), static_cast<Single>(ypos)); } private: System::Void btnClose_Click(System::Object * sender, System::EventArgs * e) { Close(); } }; }
|
System::Void btnPreview_Click_1(System::Object * sender, System::EventArgs * e) { this->printPreviewDialog1->ShowDialog(); } |
|
||
Previous | Copyright © 2005-2016, FunctionX | |
|