C#打印GroupBox控件区域

本文介绍了一种使用C#实现打印功能的方法,包括打印页面设置、打印预览及实际打印过程。通过三个主要控件:printDocument用于定义打印内容,printPreviewDialog提供预览功能,pageSetupDialog用于设置打印参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 //用到的三个控件分别是 printDocument(区域) 、printPreviewDialog(预览)、(pageSetupDialog设置)
        protected void Init()
        {            
            this.printDocument1.OriginAtMargins = true; //启用页边距

            this.printPreviewDialog1.Document = this.printDocument1;

            this.pageSetupDialog1.Document = this.printDocument1;

            this.labPrintTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
 /// <summary>
        /// 设置打印区域  核心
        /// </summary>
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //打印区域为GroupBox中的内容

            int width = this.groupBox1.Width;
            int height = this.groupBox1.Height;

            Bitmap bitmap = new Bitmap(width, height);
            this.groupBox1.DrawToBitmap(bitmap, new Rectangle(0, 0, width, height));
            e.Graphics.DrawImage(bitmap, 0, 0, width, height);
        }

        /// <summary>
        /// 打印预览
        /// </summary>
        private void btnPrintView_Click(object sender, EventArgs e)
        {
            DialogResult dr = this.printPreviewDialog1.ShowDialog();

            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                this.printDocument1.Print();
            }
        }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值