winfrom里面打印类似小票

时间:2024-10-06 18:36:38

首先在窗体上拖一个printDocument1控件和一个Button按钮,然后双击该控件的PrintPage事件,在事件里面复制下面代码:

Pen blackPen = new Pen(Color.Black, 3);
            // Create array of rectangles.
            Rectangle[] rects =
             {
                 new Rectangle( 140,70,560,330), //参数说明:左边距,上边距,右边距,底边距
                 new Rectangle(145,75,550,320),
             };

// Draw rectangles to screen.
            e.Graphics.DrawRectangles(blackPen, rects);

e.Graphics.DrawString("自助导诊系统", new Font("Segoe UI", 15, FontStyle.Bold), Brushes.Black, new Point(250, 30));//其中10为左边距,30为上边距

//e.Graphics.DrawString("序号:"+SufferGlobalInfo.XM, new Font("Segoe UI", 15, FontStyle.Bold), Brushes.Black, new Point(150, 80));//画标签名,LName就是标签的名字
            e.Graphics.DrawString("姓名:"+SufferGlobalInfo.XM, new Font("Segoe UI", 15, FontStyle.Bold), Brushes.Black, new Point(150, 80));//画标签名,LName就是标签的名字
            e.Graphics.DrawString("性别:"+SufferGlobalInfo.SEX, new Font("Segoe UI", 15, FontStyle.Bold), Brushes.Black, new Point(150, 120));//画从库中取出的数据,

e.Graphics.DrawString("身份证号:"+SufferGlobalInfo.SFZHM, new Font("Segoe UI", 15, FontStyle.Bold), Brushes.Black, new Point(150, 160));//画标签名
            e.Graphics.DrawString("年龄:"+SufferGlobalInfo.AGE, new Font("Segoe UI", 15, FontStyle.Bold), Brushes.Black, new Point(150, 200));//画从库中取出的数据,

e.Graphics.DrawString("地址:"+SufferGlobalInfo.ADDRESS, new Font("Segoe UI", 15, FontStyle.Bold), Brushes.Black, new Point(150, 240));//画标签名,

注意:SufferGlobalInfo是我的一个静态页面,你们不用管。

然后在Button事件里面直接调用打印的方法即可.(打印方法调用代码:  printDocument1.Print();)