打印系统开发(2)——WinForm开发(18)——C# winform 打印 窗体 及 窗体控件内容

学习c# winform 打印,参照了网络上诸多资源。
效果示意:

操作步骤:
1、新建winform项目及创建窗体。

2、拖取 打印 相关控件
   PageSetupDialog 、 PrintDialog 、 PrintDocument 、PrintPreviewDialog

3、设置上述控件的Document属性为相应的PrintDocument

4、设置按钮等控件 及 添加相应按钮事件。

5、示意代码如下:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->public partial class Form3 : Form
{
    public Form3()
    {
        InitializeComponent();
        this.printDocument1.OriginAtMargins = true;//启用页边距
        this.pageSetupDialog1.EnableMetric = true; //以毫米为单位

    }

    //打印设置
    private void btnSetPrint_Click(object sender, EventArgs e)
    {
        this.pageSetupDialog1.ShowDialog(); 
    }

    //打印预览
    private void btnPrePrint_Click(object sender, EventArgs e)
    {
        this.printPreviewDialog1.ShowDialog(); 
    }

    //打印
    private void btnPrint_Click(object sender, EventArgs e)
    {
        if (this.printDialog1.ShowDialog() == DialogResult.OK)
        {
            this.printDocument1.Print();
        }
    }

    //打印内容的设置
    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {                        
        打印内容 为 整个Form
        //Image myFormImage;
        //myFormImage = new Bitmap(this.Width, this.Height);
        //Graphics g = Graphics.FromImage(myFormImage);
        //g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
        //e.Graphics.DrawImage(myFormImage, 0, 0);

        打印内容 为 局部的 this.groupBox1
        //Bitmap _NewBitmap = new Bitmap(groupBox1.Width, groupBox1.Height);
        //groupBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
        //e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height); 

        //打印内容 为 自定义文本内容 
        Font font = new Font("宋体", 12);
        Brush bru = Brushes.Blue; 
        for (int i = 1; i <= 5; i++)
        {
            e.Graphics.DrawString("Hello world ", font, bru, i*20, i*20);
        }
    }
}
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;


public class PrintingExample : System.Windows.Forms.Form
{
    private System.ComponentModel.Container components;
    private System.Windows.Forms.Button printButton;
    private Font printFont;
    private StreamReader streamToPrint;

    public PrintingExample() : base()
    {
        // The Windows Forms Designer requires the following call.
        InitializeComponent();
    }

    // The Click event is raised when the user clicks the Print button.
    private void printButton_Click(object sender, EventArgs e)
    {
        try
        {
            streamToPrint = new StreamReader
            ("C:\\My Documents\\MyFile.txt");
            try
            {
                printFont = new Font("Arial", 10);
                PrintDocument pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler
                (this.pd_PrintPage);
                pd.Print();
            }
            finally
            {
                streamToPrint.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    // The PrintPage event is raised for each page to be printed.
    private void pd_PrintPage(object sender, PrintPageEventArgs ev)
    {
        float linesPerPage = 0;
        float yPos = 0;
        int count = 0;
        float leftMargin = ev.MarginBounds.Left;
        float topMargin = ev.MarginBounds.Top;
        string line = null;

        // Calculate the number of lines per page.
        linesPerPage = ev.MarginBounds.Height /
        printFont.GetHeight(ev.Graphics);

        // Print each line of the file.
        while (count < linesPerPage &&
        ((line = streamToPrint.ReadLine()) != null))
        {
            yPos = topMargin + (count *
            printFont.GetHeight(ev.Graphics));
            ev.Graphics.DrawString(line, printFont, Brushes.Black,
            leftMargin, yPos, new StringFormat());
            count++;
        }

        // If more lines exist, print another page.
        if (line != null)
            ev.HasMorePages = true;
        else
            ev.HasMorePages = false;
    }


    // The Windows Forms Designer requires the following procedure.
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.printButton = new System.Windows.Forms.Button();

        this.ClientSize = new System.Drawing.Size(504, 381);
        this.Text = "Print Example";

        printButton.ImageAlign =
        System.Drawing.ContentAlignment.MiddleLeft;
        printButton.Location = new System.Drawing.Point(32, 110);
        printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        printButton.TabIndex = 0;
        printButton.Text = "Print the file.";
        printButton.Size = new System.Drawing.Size(136, 40);
        printButton.Click += new System.EventHandler(printButton_Click);

        this.Controls.Add(printButton);
    }

    // This is the main entry point for the application.
    public static void Main(string[] args)
    {
        Application.Run(new PrintingExample());
    }
}

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值