打印

下面,来进行简单的winform打印功能的使用:

页面布局如下:

打印

 

代码实现:

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

namespace WindowsForms代码
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Btn_Print_Click(object sender, EventArgs e)
        {
            // 定义打印模板(paper纸张)的大小;Custum表示自定义纸张大小
            this.PD_document.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Custum", 1000, 520);
            this.PD_document.PrintPage += PD_document_PrintPage; // 添加打印事件(快捷键:Tab)
            this.PPD_Dialog.Document = this.PD_document;  // 预览
            if(this.PPD_Dialog.ShowDialog()==DialogResult.OK)
            {
                this.PD_document.Print();  // 打印
            }
        }

        // 进行画图
        private void PD_document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            // 绘制字符串
            e.Graphics.DrawString("PrintString", new Font(new FontFamily("宋体"), 24), System.Drawing.Brushes.Yellow, 20, 20);
        }
    }
}

 

上一篇:(四十九)c#Winform自定义控件-下拉框(表格)


下一篇:在画布上绘制一个松鼠形状(Android)