Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)qqpublished in(发表于) 2014/7/9 1:39:39 C#中的PrintDialog组件的应用实例
PrintDialog组件的应用下面通过一个例子来说明PrintDialog组件的用法。程序开发步骤如下所示。(1)打开上一节的项目,继续在Windows窗体上添加一个PrintDialog组件。(2)双击窗体中的按钮,在双击事件中添加如下代码:private void button1_Click(object sender, EventArgs e){pageSetupDialog1.Document = printDocument1;pageSetupDialog1.ShowDialog();printDialog1.Document = printDocument1;printDialog1.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{/// /// 必需的设计器变量。/// private System.ComponentModel.IContainer components = null;/// /// 清理所有正在使用的资源。/// /// 如果应释放托管资源,为 true;否则为 false。protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// /// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。/// 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);}#endregionprivate 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{/// /// 应用程序的主入口点。/// [STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Form1());}}}图1 PrintDialog对话框
赞