.net打印控件基本用法

1、在winform上加如下控件

2、代码和用法如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Winform
{
    /*
     在 Windows 窗体中进行打印主要包括以下两个方面:使用 PrintDocument 组件(Windows 窗体)组件来使用户可以打印;使用 PrintPreviewDialog 控件(Windows 窗体)控件、 PrintDialog 组件(Windows 窗体)组件和 PageSetupDialog 组件(Windows 窗体)组件向了解 Windows 操作系统的用户提供熟悉的图形界面
     * printdocument使用方法:1、创建;2、在printpage事件中写每一页的打印逻辑;3、调用print方法
     * printPreviewControl用法:1、创建;2、将document属性和printdocument关联;3、在printdocument的printpage事件里写打印逻辑;4、运行程序后会自动调用printpage事件;5、如果要刷新就用InvalidatePreview函数
     */
    public partial class UsageOfControl6 : Form
    {
        string documentContents;//总打印字符
        string stringToPrint;///要打印的
        public UsageOfControl6()
        {
            InitializeComponent();
            ///考虑printPreviewControl组件在初始化时就调用了printdocument的printpage事件,在这里初始化打印数据
            using (FileStream stream = new FileStream(@"d:\redist.txt", FileMode.Open))
            using (StreamReader reader = new StreamReader(stream))
            {
                documentContents = reader.ReadToEnd();
            }
            stringToPrint = documentContents;
        }

        private void UsageOfControl6_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            printPreviewDialog1.Document = printDocument1;//打印预览控件
            printDialog1.Document = printDocument1;//打印设置控件
            pageSetupDialog1.Document=printDocument1;//页面设置控件
            printPreviewControl1.Document = printDocument1;
            

            using (FileStream stream = new FileStream(@"d:\redist.txt", FileMode.Open))
            using (StreamReader reader = new StreamReader(stream))
            {
                documentContents = reader.ReadToEnd();
            }
            stringToPrint = documentContents;

            if (printDialog1.ShowDialog()==DialogResult.OK)///通过printDialog1来设置printdocument1的属性:打印机,打印页数,打印方向 
            {
                pageSetupDialog1.ShowDialog();///打印纸设置,边距,页眉,纸大小,也可以在printDocument1_PrintPage事件中获取e.PageSettings来设置
                this.printPreviewDialog1.ShowDialog();///打印预览
                printDocument1.Print();
                printPreviewControl1.InvalidatePreview();////刷新,会再次调用printdocument的printpage事件
            }

        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int charactersOnPage = 0;
            int linesPerPage = 0;
            // Sets the value of charactersOnPage to the number of characters 
            // of stringToPrint that will fit within the bounds of the page.
            //算出stringToPrint铺满打印页时的打印行数和字符数
            e.Graphics.MeasureString(stringToPrint, this.Font,
                e.MarginBounds.Size, StringFormat.GenericTypographic,
                out charactersOnPage, out linesPerPage);

            // Draws the string within the bounds of the page.///打印本页文字
            ///除了打印文字还可以打印线条,图形,屏幕截图等,可以在批定的位置打
            e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
            e.MarginBounds, StringFormat.GenericTypographic);



            // Remove the portion of the string that has been printed.
            stringToPrint = stringToPrint.Substring(charactersOnPage);//下一页要打的

            // Check to see if more pages are to be printed.
            e.HasMorePages = (stringToPrint.Length > 0);///如果没有打印完就打印下一页,会自动调用这个事件来打印下一页

            // If there are no more pages, reset the string to be printed.
            if (!e.HasMorePages)
                stringToPrint = documentContents;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            printPreviewControl1.StartPage += 1;///下一页
        }

        private void button3_Click(object sender, EventArgs e)
        {
            printPreviewControl1.StartPage -= 1;///上一页
        }

        private void button4_Click(object sender, EventArgs e)
        {
            printPreviewControl1.Zoom += 0.1;///放大
        }

        private void button5_Click(object sender, EventArgs e)
        {
            printPreviewControl1.Zoom -= 0.1;//缩小
        }
    }
}
View Code

 

转载于:https://www.cnblogs.com/shengyu-kmust/p/4478810.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VB.NET是一种面向对象的编程语言,可用于开发Windows应用程序。如果要实现箱贴打印功能,可以使用VB.NET打印功能和Windows Forms控件。 首先,我们可以使用VB.NET打印功能来设置打印机和打印参数。可以使用PrintDocument类来创建一个新的打印文档对象,并设置打印参数,如纸张尺寸、边距、打印方向等。 然后,我们可以使用Windows Forms控件中的Label控件来设计箱贴的样式。可以调整Label控件的大小、字体、颜色等属性,以满足打印箱贴的需求。可以使用Label控件的Text属性来设置箱贴上的文本内容。 接下来,我们可以使用VB.NET中的打印事件来处理打印过程。可以使用PrintPage事件来绘制需要打印的内容。在PrintPage事件的处理程序中,可以使用Graphics对象来绘制Label控件或其他需要打印的内容,并使用PrintDocument类的Print方法将内容发送到打印机。 最后,可以使用VB.NET打印预览功能来查看打印效果。可以使用PrintPreviewDialog控件来创建一个打印预览对话框,并将PrintDocument对象与该对话框关联。可以使用PrintPreviewDialog控件的ShowDialog方法来显示打印预览对话框。 通过以上步骤,我们可以在VB.NET中实现箱贴打印功能。可以根据需要调整打印参数、设计打印样式,并使用打印事件来处理打印过程。最后,使用打印预览功能来查看打印效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值