C#-Winform-打印控件

 

控件设计:

 

设置上述出去Button和Form控件的剩余控件(PrintDialog1等)的Document属性为相应的PrintDocument。

 

 

 

 

 

代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace yundayin
12 {
13     public partial class Form1 : Form
14     {
15         public Form1()
16         {
17             InitializeComponent();
18 
19             this.printDocument1.OriginAtMargins = true;//启用页边距
20             this.pageSetupDialog1.EnableMetric = true; //以毫米为单位
21         }
22 
23         /// <summary>
24         /// 打印
25         /// </summary>
26         private void button1_Click(object sender, EventArgs e)
27         {
28             if (this.printDialog1.ShowDialog() == DialogResult.OK)
29             {
30                 this.printDocument1.Print();
31             }
32         }
33 
34         /// <summary>
35         /// 打印设置
36         /// </summary>
37         private void button2_Click(object sender, EventArgs e)
38         {
39             this.pageSetupDialog1.ShowDialog(); 
40         }
41 
42         /// <summary>
43         /// 打印预览
44         /// </summary>
45         private void button3_Click(object sender, EventArgs e)
46         {
47             this.printPreviewDialog1.ShowDialog(); 
48         }
49 
50         private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
51         {
52             ////----------------打印内容 为 整个Form----------------
53             //Image myFormImage;
54             //myFormImage = new Bitmap(this.Width, this.Height);
55             //Graphics g = Graphics.FromImage(myFormImage);
56             //g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
57             //e.Graphics.DrawImage(myFormImage, 0, 0);
58 
59             ////----------------打印内容 为 局部的 this.groupBox1----
60             //Bitmap _NewBitmap = new Bitmap(groupBox1.Width, groupBox1.Height);
61             //groupBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
62             //e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height); 
63 
64             //------------------打印内容 为 自定义文本内容------------
65             Font font = new Font("宋体", 12);
66             Brush bru = Brushes.Blue;
67             //for (int i = 1; i <= 5; i++)
68             //{
69                 
70             //}
71             e.Graphics.DrawString("Hello world ", font, bru, 10, 10);
72         }
73     }
74 }

 

 

完成。

 

转载于:https://www.cnblogs.com/KTblog/p/4471460.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值