如何:使用 PageSetupDialog 组件确定页属性
PageSetupDialog 组件针对文档向用户呈现布局、纸张大小和其他页面布局选项。
需要指定 PrintDocument 类的实例 — 这是要打印的文档。 此外,用户必须在其计算机上安装打印机(本地或通过网络),因为 PageSetupDialog 组件在一定程度上会通过此方面确定向用户呈现的页面格式设置选项。
使用 PageSetupDialog 组件的一个重要方面是它如何与 PageSettings 类进行交互。 PageSettings 类用于指定修改页面打印方式的设置,如纸张方向、页面大小和边距。 这些设置中的每个设置都表示为 PageSettings 类的属性。 PageSetupDialog 类会为与文档关联(并表示为 PageSettings 属性)的给定 DefaultPageSettings 类实例修改这些属性值。
使用 PageSetupDialog 组件设置页面属性
使用 ShowDialog 方法可显示对话框,从而指定要使用的 PrintDocument 。
在以下示例中, Button 控件的 Click 事件处理程序会打开 PageSetupDialog 组件的实例。 一个现有文档在 Document 属性中进行指定,其 PageSettings.Color 属性设置为
false
。示例假设窗体具有一个 Button 控件、一个名为
myDocument
的 PrintDocument 组件和一个 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);