我想要一个具有打印对话框,打印机设置对话框和打印预览完整的样例,谢谢!

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Drawing.Drawing2D;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Drawing.Printing;
    using System.IO;namespace Test
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.TextBox textBox4;
    private System.Windows.Forms.TextBox textBox5;
    private PrintDocument pd = new PrintDocument();
    private PageSettings storePageSetting = new PageSettings();
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.Button button4;
    private System.Windows.Forms.TextBox textBox6;
    private System.Windows.Forms.Button button5;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    this.pd.DocumentName = "HAHAHA";
    this.pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.pd_PrintPage);
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.textBox3 = new System.Windows.Forms.TextBox();
    this.textBox4 = new System.Windows.Forms.TextBox();
    this.textBox5 = new System.Windows.Forms.TextBox();
    this.button2 = new System.Windows.Forms.Button();
    this.button3 = new System.Windows.Forms.Button();
    this.button4 = new System.Windows.Forms.Button();
    this.textBox6 = new System.Windows.Forms.TextBox();
    this.button5 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(40, 16);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(224, 21);
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "textBox1";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(8, 208);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "打印预览";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // textBox2
    // 
    this.textBox2.Location = new System.Drawing.Point(40, 48);
    this.textBox2.Name = "textBox2";
    this.textBox2.TabIndex = 0;
    this.textBox2.Text = "textBox1";
    // 
    // textBox3
    // 
    this.textBox3.Location = new System.Drawing.Point(40, 88);
    this.textBox3.Name = "textBox3";
    this.textBox3.TabIndex = 0;
    this.textBox3.Text = "textBox1";
    // 
    // textBox4
    // 
    this.textBox4.Location = new System.Drawing.Point(160, 48);
    this.textBox4.Name = "textBox4";
    this.textBox4.TabIndex = 0;
    this.textBox4.Text = "textBox1";
    // 
    // textBox5
    // 
    this.textBox5.Location = new System.Drawing.Point(16, 128);
    this.textBox5.Name = "textBox5";
    this.textBox5.Size = new System.Drawing.Size(264, 21);
    this.textBox5.TabIndex = 0;
    this.textBox5.Text = "textBox1";
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(96, 208);
    this.button2.Name = "button2";
    this.button2.TabIndex = 1;
    this.button2.Text = "页面设置";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // button3
    // 
    this.button3.Location = new System.Drawing.Point(192, 208);
    this.button3.Name = "button3";
    this.button3.TabIndex = 1;
    this.button3.Text = "打印";
    this.button3.Click += new System.EventHandler(this.button3_Click);
    // 
    // button4
    // 
    this.button4.Location = new System.Drawing.Point(192, 208);
    this.button4.Name = "button4";
    this.button4.TabIndex = 1;
    this.button4.Text = "打印";
    // 
    // textBox6
    // 
    this.textBox6.Location = new System.Drawing.Point(40, 88);
    this.textBox6.Name = "textBox6";
    this.textBox6.TabIndex = 0;
    this.textBox6.Text = "textBox1";
    // 
    // button5
    // 
    this.button5.Location = new System.Drawing.Point(192, 88);
    this.button5.Name = "button5";
    this.button5.TabIndex = 1;
    this.button5.Text = "重设";
    this.button5.Click += new System.EventHandler(this.button5_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.button1,
      this.textBox1,
      this.textBox2,
      this.textBox3,
      this.textBox4,
      this.textBox5,
      this.button2,
      this.button3,
      this.button4,
      this.textBox6,
      this.button5});
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    string paperName = "B5";
    foreach(PaperSize ps in this.pd.PrinterSettings.PaperSizes)
    if(paperName ==  ps.PaperName)
    {
    //ps.Width = 800;
    //ps.Height = 800;
    //this.pd.DefaultPageSettings.PaperSize = ps;
    this.storePageSetting.PaperSize = ps;
    }

    // this.storePageSetting = this.pd.DefaultPageSettings;
    this.storePageSetting.PaperSize = new PaperSize("Custom",800,400);
    this.storePageSetting.Margins = new Margins(40,40,40,40);
    this.WriteTextBox(); } private void button1_Click(object sender, System.EventArgs e)
    {
    pd.DefaultPageSettings = this.storePageSetting;

    PrintPreviewDialog ppDlg = new PrintPreviewDialog();
    ppDlg.Document = pd;
    ppDlg.ShowDialog();
    }
      

  2.   

    private void WriteTextBox()
    {
    this.textBox1.Text = "纸张种类:"+this.storePageSetting.PaperSize.Kind.ToString();
    this.textBox2.Text = "纸张名称:"+this.storePageSetting.PaperSize.PaperName.ToString();
    this.textBox3.Text = "纸张宽度:"+this.storePageSetting.PaperSize.Width.ToString();
    this.textBox4.Text = "纸张高度:"+this.storePageSetting.PaperSize.Height.ToString();
    this.textBox5.Text = "左:"+this.storePageSetting.Margins.Left.ToString()
    +"右"+this.storePageSetting.Margins.Right.ToString()
    +"上"+this.storePageSetting.Margins.Top.ToString()
    +"下"+this.storePageSetting.Margins.Bottom.ToString();
    }
    private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    e.Graphics.FillRectangle(Brushes.Blue,new Rectangle(e.PageSettings.Margins.Left+(e.MarginBounds.Width-200)/2,
    e.PageSettings.Margins.Top+(e.MarginBounds.Height-200)/2,200,200));
    } private void button2_Click(object sender, System.EventArgs e)
    {
    PageSetupDialog psDlg = new PageSetupDialog();
    psDlg.PageSettings = this.storePageSetting;
    if(psDlg.ShowDialog()==DialogResult.OK)
    this.WriteTextBox();
    } private void button3_Click(object sender, System.EventArgs e)
    {
    this.pd.DefaultPageSettings = this.storePageSetting;
    PrintDialog pDlg = new PrintDialog();
    pDlg.Document = pd;
    if(pDlg.ShowDialog()==DialogResult.OK)
    pd.Print();
    } private void button5_Click(object sender, System.EventArgs e)
    {
    if(this.storePageSetting.PaperSize.Kind == PaperKind.Custom)
    {
    try
    {
    this.storePageSetting.PaperSize.PaperName = this.textBox2.Text.Trim();
    this.storePageSetting.PaperSize.Width = int.Parse(this.textBox3.Text.Trim());
    this.storePageSetting.PaperSize.Height = int.Parse(this.textBox4.Text.Trim());
    this.WriteTextBox();
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }
    }
    else
    MessageBox.Show("不能改变纸张");
    }
    }
    }
      

  3.   

    楼主注意了
    textBox3  and textBox4应该输入数字而不是“纸张宽度:800",如果装有多台打印机的话,也要注意默认打印机的设置问题!上面两段是同一程序,不要拆开!至于打印对话框,.NET 2003有一个自带的控件printDialog,也可以完成楼主所有的要求!
      

  4.   

    请问有没有Web下的具有打印对话框,打印机设置对话框和打印预览的例子?我是刚开始学c#的,希望大家给些帮助,非常感谢