C#控件打印的使用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 微信购电记录打印
{
    public partial class Form1 : Form
    {

        private System.ComponentModel.Container components;
        private System.Windows.Forms.Button printButton;
        private Font printFont;
        private StreamReader streamToPrint;

        public Form1()
        {
            InitializeComponent();
            this.printDocument1.OriginAtMargins = true;//启用页边距
            this.pageSetupDialog1.EnableMetric = true; //以毫米为单位
        }

        SqlConnection myConn;//定义一个数据库连接对象  全局变量
        public string svName;//数据服务器名称
        public string idName;//数据库名称
        public string psName;//数据库密码
        public string sqlName;//要连接的数据库

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            svName = "PC-20141126MSWL\\MYSQL2012";
            idName = "sa";
            psName = "mingyu";
            sqlName = "Lw_DkSv10";
            string sql = "Data Source='" + svName + "';Initial Catalog='" + sqlName + "';User ID='" + idName + "';Password='" + psName + "';";
            myConn = new SqlConnection(sql);//创建数据库连接类的对象
            myConn.Open(); //将连接打开
            if (myConn.State == ConnectionState.Open)
            {
                //MessageBox.Show("数据库连接成功!" + sqlName, "提示:");
                label1.Text = "数据库已连接";
            }
            myConn.Close();

            string[] array = { "一月", "二月", "三月", "四月", "五月","六月","七月","八月","九月","十月","十一月","十二月","全部" };
            comboBox1.DataSource = array;

            //printPreviewControl1.Document = printDocument1;

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (this.printDialog1.ShowDialog() == DialogResult.OK)
            {
                this.printDocument1.Print();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //label1.Text = comboBox1.SelectedIndex.ToString();
            int num=0;
            num = comboBox1.SelectedIndex+1;
            if (num == 0 || num == 13)
            {
                myConn.Open();
                string SQL = "select 校区,收费金额,流水号,购电时间 From Lw_DkSv10.dbo.购电记录2018";
                SqlDataAdapter objDataAdpter = new SqlDataAdapter();
                objDataAdpter.SelectCommand = new SqlCommand(SQL, myConn);
                DataSet ds = new DataSet();
                objDataAdpter.Fill(ds, "购电记录2018");
                dataGridView1.DataSource = ds.Tables[0];
                myConn.Close();
            }
            else {
                myConn.Open();
                string SQL = "select 校区,收费金额,流水号,购电时间 From Lw_DkSv10.dbo.购电记录2018 where month(购电时间)=" + num.ToString();
                SqlDataAdapter objDataAdpter = new SqlDataAdapter();
                objDataAdpter.SelectCommand = new SqlCommand(SQL, myConn);
                DataSet ds = new DataSet();
                objDataAdpter.Fill(ds, "购电记录2018");
                dataGridView1.DataSource = ds.Tables[0];
                myConn.Close();
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            设置printDialog控件的Document属性,设置操作文档
            //printDialog1.Document = printDocument1;
            启用"打印到文件"复选框
            //printDialog1.AllowPrintToFile = true;
            显示“当前项”按钮
            //printDialog1.AllowCurrentPage = true;
            启用"选择按钮"
            //printDialog1.AllowSelection = true;
            启用"页"按钮
            //printDialog1.AllowSomePages = true;
            //printDialog1.ShowDialog();


            //this.pageSetupDialog1.ShowDialog();

            if (MessageBox.Show("是否要预览打印文档", "打印预览", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                //开启操作系统的防锯齿功能
                this.printPreviewDialog1.UseAntiAlias = true;
                //设置要预览的文档
                this.printPreviewDialog1.Document = this.printDocument1;
                //打开预览窗口
                printPreviewDialog1.ShowDialog();
            }
            else
            {
                //调用Print方法直接打印文档
                //this.printDocument1.Print();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.printPreviewDialog1.ShowDialog();
        }

        //private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        //{
        //    打印内容 为 整个Form
        //    //Image myFormImage;
        //    //myFormImage = new Bitmap(this.Width, this.Height);
        //    //Graphics g = Graphics.FromImage(myFormImage);
        //    //g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
        //    //e.Graphics.DrawImage(myFormImage, 0, 0);

        //    //打印内容 为 局部的 this.groupBox1
        //    Bitmap _NewBitmap = new Bitmap(groupBox1.Width, groupBox1.Height);
        //    groupBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
        //    e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);

        //    //打印内容 为 自定义文本内容 
        //    //Font font = new Font("宋体", 12);
        //    //Brush bru = Brushes.Blue;
        //    //for (int i = 1; i <= 5; i++)
        //    //{
        //    //    e.Graphics.DrawString("Hello world ", font, bru, i * 20, i * 20);
        //    //}
        //}

        //private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        //{
        //    //通过GDI+绘制打印文档
        //    e.Graphics.DrawString("蝶恋花", new Font("宋体", 15), Brushes.Black, 350, 80);
        //    e.Graphics.DrawLine(new Pen(Color.Black, (float)3.00), 100, 185, 720, 185);
        //    e.Graphics.DrawString("伫倚危楼风细细,望极春愁,黯黯生天际。", new Font("宋体", 12), Brushes.Black, 110, 195);
        //    e.Graphics.DrawString("草色烟光残照里,无言谁会凭阑意。", new Font("宋体", 12), Brushes.Black, 110, 220);
        //    e.Graphics.DrawString("拟把疏狂图一醉,对酒当歌,强乐还无味。", new Font("宋体", 12), Brushes.Black, 110, 245);
        //    e.Graphics.DrawString("衣带渐宽终不悔。为伊消得人憔悴。", new Font("宋体", 12), Brushes.Black, 110, 270);
        //    e.Graphics.DrawLine(new Pen(Color.Black, (float)3.00), 100, 300, 720, 300);
        //}
        private void PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            打印内容 为 整个Form
            //Image myFormImage;
            //myFormImage = new Bitmap(this.Width, this.Height);
            //Graphics g = Graphics.FromImage(myFormImage);
            //g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
            //e.Graphics.DrawImage(myFormImage, 0, 0);

            打印内容 为 局部的 this.groupBox1
            //Bitmap _NewBitmap = new Bitmap(groupBox1.Width, groupBox1.Height);
            //groupBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            //e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);

            //打印内容 为 自定义文本内容 
            //Font font = new Font("宋体", 12);
            //Brush bru = Brushes.Blue;
            //for (int i = 1; i <= 5; i++)
            //{
            //    e.Graphics.DrawString("Hello world ", font, bru, i * 20, i * 20);
            //}


            Graphics g = e.Graphics;   //先建立画布   
            int x = 80;
            int y = 60;
            g.DrawImage(this.BackgroundImage, 50, 50);
            foreach (Control item in this.Controls)
            {
                if (item is TextBox)
                {
                    Control tx = (item as Control);
                    g.DrawString(tx.Text, tx.Font, Brushes.Black, tx.Left + x, tx.Top + y);
                }
            }
        }
    }
}

今天太晚了  睡觉了 明天看看打印控件问题出在哪里了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值