首页 新闻 会员 周边 捐助

VS2005 窗体打印解决方案 急 求热心的高手指点

0
悬赏园豆:100 [已关闭问题]

小弟要实现在VS2005下 窗体打印

知道2008版本有一个printform组件  ,在2005下 该怎么做  求高手指点 解决方案

文刀的主页 文刀 | 初学一级 | 园豆:100
提问于:2008-10-30 16:34
< >
分享
其他回答(1)
0

http://zamuj.blog.hexun.com/15140514_d.html

Astar | 园豆:40805 (高人七级) | 2008-10-30 21:56
0

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Drawing.Printing;
namespace ExcelPrj
{
    public partial class Test : Form
    {
        /// <summary>
        /// .NET 框架提供了相关的类实现文档的打印功能,这些类在 System.Drawing.Printing 命名空间内.为了实现打印功能,通常首先需要建立PringDocument 类的实例化对象,PrintDocument 类用于设置打印文档的属性和方法
        /// 调用PringDocument类的 Print 方法后,会间接调用打印控制器类的方法,这些方法包括 OnStartPrint, OnEndPring,OnStartPage,OnEndPage,由这些方法来完成把文档内容输出到打印机的工作
        /// 在打印之前,还可以选择用于输出的打印机.在.NET 框架中,PringDialog 类可以实现打印机设置对话框,在此对话框中可以选择输出的打印机,打印方法,和文档分数
        /// 与 PrintPreviewDialog 类类似, PrintDialog 也是间接调用打印控制器的相关方法实现打印输出的阿功能
        /// .NET 框架还提供了  PringPreviewDialog 类实现打印预览的功能.PringviewDialog 在屏幕上显示通用的打印预览窗口,窗口中 的内容是通过 Pringdocument 类间接调用打印控制器的方法,显示在屏幕上.
        /// </summary>
        public Test()
        {
            InitializeComponent();
        }
        private void Test_Load(object sender, EventArgs e)
        {
            DataSet ds = Bind();
            this.dataGridView1.DataSource = ds.Tables[0];
        }
        private DataSet Bind()
        {
            SqlConnection conn = new SqlConnection("Server=.;Database=testManage;Integrated Security=SSPI");
            SqlDataAdapter da = new SqlDataAdapter("select FNumber,FExamNum,FName,FSex,FJobAdd,FCardID,FBirDate from stuInfo", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }
        /// <summary>
        /// [1] 编写打印逻辑步骤
        /// 将 PringPreviewDialog1 的Document属性设置为 PringDocument1.当PringPreviewDialog1对象执行ShowDialog方法之后,会触发PringDocument1对象的 PringPage 事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            this.pageSetupDialog1.Document = this.printDocument1;

            Font objFont = new Font("Tahoma", 11, FontStyle.Regular);
            Brush objBrush = Brushes.Black;
            Pen objPen = new Pen(objBrush);

            objPen.Width = 2;

            //象打印预览窗口添加内容
            int nLeft = this.pageSetupDialog1.PageSettings.Margins.Left;
            int nTop = this.pageSetupDialog1.PageSettings.Margins.Top;
            int nWidth=this.pageSetupDialog1.PageSettings.PaperSize.Width-(this.pageSetupDialog1.PageSettings.Margins.Left+this.pageSetupDialog1.PageSettings.Margins.Right);
            int nHeight=this.pageSetupDialog1.PageSettings.PaperSize.Height-(this.pageSetupDialog1.PageSettings.Margins.Top+this.pageSetupDialog1.PageSettings.Margins.Bottom);

            //画出页面的有效区域
            e.Graphics.DrawLine(objPen, nLeft, nTop, nLeft + nWidth, nTop);
            e.Graphics.DrawLine(objPen, nLeft, nTop + nHeight, nLeft + nWidth,nTop+nHeight);
            e.Graphics.DrawLine(objPen, nLeft, nTop, nLeft, nTop+nHeight);
            e.Graphics.DrawLine(objPen, nLeft + nWidth, nTop, nLeft + nWidth, nTop + nHeight);

            // 打印表头
            e.Graphics.DrawString("学号", new Font("Garamond", 20, FontStyle.Regular), Brushes.Blue, nLeft + 30, nTop + 10);
            e.Graphics.DrawString("姓名", new Font("Garamond", 20, FontStyle.Regular), Brushes.Blue, nLeft + 150, nTop + 10);
            e.Graphics.DrawString("年龄", new Font("Garamond", 20, FontStyle.Regular), Brushes.Blue, nLeft + 270, nTop + 10);
            e.Graphics.DrawString("出生的", new Font("Garamond", 20, FontStyle.Regular), Brushes.Blue, nLeft + 390, nTop + 10);

            DataSet ds = Bind();
            for(int i=0;i<ds.Tables[0].Rows.Count;i++)
            {
                for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                {
                   e.Graphics.DrawString(ds.Tables[0].Rows[i][j].ToString(),objFont,objBrush,nLeft + 50+nLeft*j+20,nTop + 10+nTop*i);

                }
            }
            //if (e.HasMorePages)
            //{
            //    // 有其他的页未打印,
            //}
        }
        /// <summary>
        /// [2] 选择打印机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
           
        }
        /// <summary>
        /// [3]允许用户选择页面设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            pageSetupDialog1.ShowDialog();
        }
        /// <summary>
        /// [4]显示预览窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //PrintPreviewDialog 是.NET 类库中的打印预览窗体类,每个窗体类的对象都包含  PrintPreviewControl 控件
            //PrintPreviewControl 控件用于显示打印预览的内容,如果需要定制预览窗体,可把 PrintPreviewControl控件置于定制预览窗体上面
            this.printPreviewDialog1.Document = this.printDocument1;

            this.printPreviewDialog1.ShowDialog();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {

                DialogResult pDialogResult = printDialog1.ShowDialog();
 
                //判断打印对话框的返回值是否是OK,通常按下OK  就会返回此值
                if (pDialogResult == DialogResult.OK)

                     printDocument1.Print();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
     
    }
}

Geeks | 园豆:560 (小虾三级) | 2008-10-31 13:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册