c# winform页面信息的打印

对于打印页面来说,网页打印有很大的优势,因为有函数直接打印window.Print()就可以了很方便,但是对于winform除了使用一些第三方的类库之外就需要自己写代码了。

PrintDocument printDocument1 = new PrintDocument();
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
PrintDocument printDialog1 = new PrintDocument();

private void button1_Click(object sender, EventArgs e)
        {
            // printDocument1 为 打印控件
            //设置打印用的纸张 当设置为Custom的时候,可以自定义纸张的大小,还可以选择A4,A5等常用纸型
            this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 500, 300);
            this.printDocument1.PrintPage += new PrintPageEventHandler(this.MyPrintDocument_PrintPage);
            //将写好的格式给打印预览控件以便预览
            printPreviewDialog1.Document = printDocument1;
            //显示打印预览
            DialogResult result = printPreviewDialog1.ShowDialog();
            //if (result == DialogResult.OK)
            //this.MyPrintDocument.Print();
        }

        private void MyPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            /*如果需要改变自己 可以在new Font(new FontFamily("黑体"),11)中的“黑体”改成自己要的字体就行了,黑体 后面的数字代表字体的大小
             System.Drawing.Brushes.Blue , 170, 10 中的 System.Drawing.Brushes.Blue 为颜色,后面的为输出的位置 */
            e.Graphics.DrawString("新乡市三月软件公司入库单", new Font(new FontFamily("黑体"), 11), System.Drawing.Brushes.Black, 170, 10);
            e.Graphics.DrawString("供货商:河南科技学院", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Blue, 10, 12);
            //信息的名称
            e.Graphics.DrawLine(Pens.Black, 8, 30, 480, 30);
            e.Graphics.DrawString("入库单编号", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 9, 35);
            e.Graphics.DrawString("商品名称", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 160, 35);
            e.Graphics.DrawString("数量", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 260, 35);
            e.Graphics.DrawString("单价", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 330, 35);
            e.Graphics.DrawString("总金额", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 400, 35);
            e.Graphics.DrawLine(Pens.Black, 8, 50, 480, 50);
            //产品信息
            e.Graphics.DrawString("R2011-01-2016:06:35", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 9, 55);
            e.Graphics.DrawString("联想A460", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 160, 55);
            e.Graphics.DrawString("100", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 260, 55);
            e.Graphics.DrawString("200.00", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 330, 55);
            e.Graphics.DrawString("20000.00", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 400, 55);


            e.Graphics.DrawLine(Pens.Black, 8, 200, 480, 200);
            e.Graphics.DrawString("地址:新乡市河南科技学院信息工程学院", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 9, 210);
            e.Graphics.DrawString("经办人:任忌", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 220, 210);
            e.Graphics.DrawString("服务热线:15083128577", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 320, 210);
            e.Graphics.DrawString("入库时间:" + DateTime.Now.ToString(), new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 9, 230);
        }
以上就是自己拼接的打印界面,这是参考网络的一个例子很不错。

还有另外的一种方式就是将界面保存成为图片,然后打印图片。

//private void btnPrint_Click(object sender, EventArgs e)
        //{

        //    if (this.printDialog1.ShowDialog() == DialogResult.OK)

        //        CaptureScreen();

        //    this.printDocument1.Print();

        //}



        打印预览

        //private void btnView_Click(object sender, EventArgs e)
        //{

        //    CaptureScreen();

        //    this.printPreviewDialog1.ShowDialog();



        //}

        打印设置

        //private void btnSet_Click(object sender, EventArgs e)
        //{

        //    this.pageSetupDialog1.ShowDialog();

        //}



        如果要打印整个窗体

        //[System.Runtime.InteropServices.DllImport("gdi32.dll ")]

        //public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);



        //private void CaptureScreen()
        //{

        //    Graphics mygraphics = this.panel1.CreateGraphics();

        //    Size s = this.panel1.Size; //如果要打印整个窗体Size s = this. Size;

        //    memoryImage = new Bitmap(s.Width, s.Height, mygraphics);

        //    Graphics memoryGraphics = Graphics.FromImage(memoryImage);

        //    IntPtr dc1 = mygraphics.GetHdc();

        //    IntPtr dc2 = memoryGraphics.GetHdc();

        //    BitBlt(dc2, 0, 0, this.Width, this.Height, dc1, 0, 0, 13369376);

        //    mygraphics.ReleaseHdc(dc1);

        //    memoryGraphics.ReleaseHdc(dc2);

        //}

这也是一种方法,比那个要简单但是,自由度比较低。

转载于:https://www.cnblogs.com/yueshen-blog/archive/2013/05/17/3182773.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值