PageSetupDialog 组件向用户提供文档的布局、纸张大小和其他页面布局选项。
您需要指定 PrintDocument 类的一个实例——这是要打印的文档。 此外,用户必须在其计算机上安装打印机,无论是本地的还是通过网络连接的,因为这部分决定组件 PageSetupDialog 向用户呈现的页面格式选项。
使用 PageSetupDialog 组件的重要方面是它如何与 PageSettings 类交互。 该 PageSettings 类用于指定修改页面打印方式的设置,例如纸张方向、页面大小和页边距。 其中每个设置都表示为类的属性 PageSettings 。 该 PageSetupDialog 类修改与文档关联的类的给定实例的 PageSettings 这些属性值(并表示为 DefaultPageSettings 属性)。
使用 PageSetupDialog 组件设置页面属性
使用 ShowDialog 方法可显示对话框,从而指定要使用的 PrintDocument。
在下面的示例中,控件 Button 的 Click 事件处理程序将打开组件的实例 PageSetupDialog 。 通过 Document 属性指定了一个现有文档,并将其 PageSettings.Color 属性设置为
false
。该示例假定窗体有一个Button控件、一个名为PrintDocument
myDocument
组件和一个PageSetupDialog组件。Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click ' The print document 'myDocument' used below ' is merely for an example. 'You will have to specify your own print document. PageSetupDialog1.Document = myDocument ' Sets the print document's color setting to false, ' so that the page will not be printed in color. PageSetupDialog1.Document.DefaultPageSettings.Color = False PageSetupDialog1.ShowDialog() End Sub
private void button1_Click(object sender, System.EventArgs e) { // The print document 'myDocument' used below // is merely for an example. // You will have to specify your own print document. pageSetupDialog1.Document = myDocument; // Sets the print document's color setting to false, // so that the page will not be printed in color. pageSetupDialog1.Document.DefaultPageSettings.Color = false; pageSetupDialog1.ShowDialog(); }
private: System::Void button1_Click(System::Object ^ sender, System::EventArgs ^ e) { // The print document 'myDocument' used below // is merely for an example. // You will have to specify your own print document. pageSetupDialog1->Document = myDocument; // Sets the print document's color setting to false, // so that the page will not be printed in color. pageSetupDialog1->Document->DefaultPageSettings->Color = false; pageSetupDialog1->ShowDialog(); }
(Visual C# 和 Visual C++)将以下代码置于表单的构造函数中以注册事件处理程序。
this.button1.Click += new System.EventHandler(this.button1_Click);
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);