PageSetupDialog组件的应用
下面通过一个例子来说明PageSetupDialog组件的用法。
程序开发步骤如下所示。
(1)在VS 2005中新建一个项目,选择Windows窗体模板,并命名为PageSetupDialog。给窗体添加一个PageSetupDialog组件,然后再添加一个PrintDocument组件。
(2)在窗体中继续添加一个Button按钮,并将按钮的Text属性值设置为“打印”,然后在click事件中添加如下代码:
private void button1_Click(object sender, EventArgs e)
{
      pageSetupDialog1.Document = printDocument1;
      pageSetupDialog1.ShowDialog();
}
示例运行结果如图1所示。
完整程序代码如下:
★   ★★★★Form1.cs窗体代码文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace PageSetupDialog
{
      public partial class Form1 : Form
      {
            public Form1()
            {
                  InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                  pageSetupDialog1.Document = printDocument1;
                  pageSetupDialog1.ShowDialog();
                  printDialog1.Document = printDocument1;
                  printDialog1.ShowDialog();
            }
      }
}
★   ★★★★Form1.designer.cs窗体设计文件完整程序代码★★★★★
namespace PageSetupDialog
{
      partial class Form1
      {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                  if (disposing && (components != null))
                  {
                        components.Dispose();
                  }
                  base.Dispose(disposing);
            }
            #region Windows 窗体设计器生成的代码
            /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
            this.pageSetupDialog1 = new System.Windows.Forms.PageSetupDialog();
            this.printDocument1 = new System.Drawing.Printing.PrintDocument();
            this.button1 = new System.Windows.Forms.Button();
            this.printDialog1 = new System.Windows.Forms.PrintDialog();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(185, 258);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
this.button1.Text = "打印";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// printDialog1
//
this.printDialog1.UseEXDialog = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(463, 373);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PageSetupDialog pageSetupDialog1;
private System.Drawing.Printing.PrintDocument printDocument1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.PrintDialog printDialog1;
}
}
★   ★★★★Program.cs主程序文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace PageSetupDialog
{
      static class Program
      {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.EnableVisualStyles();
                  Application.SetCompatibleTextRenderingDefault(false);
                  Application.Run(new Form1());
            }
      }
}
 --转自 

该贴由koei123转至本版2015-2-6 5:07:10