VS 2005 打印
来源:互联网 发布:java web书籍推荐2016 编辑:程序博客网 时间:2024/05/16 18:33
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[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;
}
}
}
}
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[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;
}
}
}
}
- VS 2005 打印
- VS打印思想
- VS 打印调试
- VS和linux中变参宏,打印
- vs 打印信息到Output
- VS打印printf等传出的打印信息
- 在vs中后台打印rdlc报表
- windows VS 下打印堆栈日志
- VS调试时看程序打印信息
- 如何vs在cocos2dx项目中打印中文
- VS.NET打印页面设置(PageSetupDialog)页边距总是改变
- 分享一个VS写的PDF打印控件
- 在VS下使用QDebug打印调试信息
- vs 2005/vs 2008 打包程序
- VS.NET打印思想与2003/5DataGrid、DataGridView及二维数据如ListView等终极打印实现
- 开源:VS.NET打印思想与2003/5DataGrid、DataGridView及二维数据如ListView等终极打印实现
- 开源:VS.NET打印思想与2003/5DataGrid、DataGridView及二维数据如ListView等终极打印实现
- VS.NET打印思想与2003/5DataGrid、DataGridView及二维数据如ListView等终极打印实现
- flash websites
- 操作Excel文档-基础篇
- 关于离职感想
- 栅格运算
- “我的电脑”打开缓慢,左下角显示“寻找项目”
- VS 2005 打印
- [ASP]GetRows的用法详解!
- Tomcat 简介,Tomcat 与Apache 的关系
- POI操作Excel文档-中级篇
- poi遍歷Excel文件
- 桌面图标下面有阴影
- [股林秘籍]涨停板战法秘籍大全
- 网络故障多种解决方案(1)
- BigDecimal介绍