实例介绍
【实例简介】
【实例截图】
【核心代码】
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace Print
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Drawing.Printing.PrintDocument printDocument1;
private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
private System.Windows.Forms.Button button4;
private System.Drawing.Printing.PrintDocument printDocument2;
private System.Windows.Forms.PrintPreviewDialog printPreviewDialog2;
private System.Windows.Forms.PrintDialog printDialog1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private PrinterClass PrintC;
private System.Windows.Forms.DataGrid dataGrid;
private OleDbConnection conn = null;
private string connectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\Northwind.mdb";
private string sql = null;
private DataSet ds = null;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
private void Form1_Load(object sender,System.EventArgs e)
{
FillDataGrid();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.dataGrid = new System.Windows.Forms.DataGrid();
this.printDocument1 = new System.Drawing.Printing.PrintDocument();
this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
this.button4 = new System.Windows.Forms.Button();
this.printDocument2 = new System.Drawing.Printing.PrintDocument();
this.printPreviewDialog2 = new System.Windows.Forms.PrintPreviewDialog();
this.printDialog1 = new System.Windows.Forms.PrintDialog();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid)).BeginInit();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// dataGrid
//
this.dataGrid.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192)));
this.dataGrid.BackgroundColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
this.dataGrid.DataMember = "";
this.dataGrid.Font = new System.Drawing.Font("Times New Roman", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(177)));
this.dataGrid.GridLineColor = System.Drawing.Color.Black;
this.dataGrid.HeaderBackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
this.dataGrid.HeaderFont = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(177)));
this.dataGrid.HeaderForeColor = System.Drawing.Color.Red;
this.dataGrid.Location = new System.Drawing.Point(0, 0);
this.dataGrid.Name = "dataGrid";
this.dataGrid.Size = new System.Drawing.Size(592, 200);
this.dataGrid.TabIndex = 0;
//
// printDocument1
//
this.printDocument1.PrintPage = new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
//
// printPreviewDialog1
//
this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.Enabled = true;
this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
this.printPreviewDialog1.Location = new System.Drawing.Point(88, 96);
this.printPreviewDialog1.MinimumSize = new System.Drawing.Size(375, 250);
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty;
this.printPreviewDialog1.Visible = false;
//
// button4
//
this.button4.Location = new System.Drawing.Point(384, 216);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(104, 48);
this.button4.TabIndex = 4;
this.button4.Text = "Print with header and footer";
this.button4.Click = new System.EventHandler(this.button4_Click);
//
// printDocument2
//
this.printDocument2.PrintPage = new System.Drawing.Printing.PrintPageEventHandler(this.printDocument2_PrintPage);
//
// printPreviewDialog2
//
this.printPreviewDialog2.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialog2.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.printPreviewDialog2.ClientSize = new System.Drawing.Size(400, 300);
this.printPreviewDialog2.Document = this.printDocument2;
this.printPreviewDialog2.Enabled = true;
this.printPreviewDialog2.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog2.Icon")));
this.printPreviewDialog2.Location = new System.Drawing.Point(279, 17);
this.printPreviewDialog2.MinimumSize = new System.Drawing.Size(375, 250);
this.printPreviewDialog2.Name = "printPreviewDialog2";
this.printPreviewDialog2.TransparencyKey = System.Drawing.Color.Empty;
this.printPreviewDialog2.Visible = false;
//
// printDialog1
//
this.printDialog1.AllowSelection = true;
this.printDialog1.AllowSomePages = true;
this.printDialog1.Document = this.printDocument1;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button3);
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Location = new System.Drawing.Point(56, 224);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(224, 80);
this.groupBox1.TabIndex = 6;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Print without header";
//
// button3
//
this.button3.Location = new System.Drawing.Point(8, 32);
this.button3.Name = "button3";
this.button3.TabIndex = 8;
this.button3.Text = "PrintDialog";
this.button3.Click = new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(96, 48);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(120, 23);
this.button2.TabIndex = 7;
this.button2.Text = "Preview";
this.button2.Click = new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 23);
this.button1.TabIndex = 6;
this.button1.Text = "Print";
this.button1.Click = new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(624, 331);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.button4);
this.Controls.Add(this.dataGrid);
this.Name = "Form1";
this.Text = "Form1";
this.Load = new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//print grid
bool morepages = PrintC.Print(e.Graphics);
if (morepages)
{
e.HasMorePages = true;
}
}
private void button1_Click(object sender, System.EventArgs e)
{
//create printerclass object
PrintC = new PrinterClass(printDocument1,dataGrid);
printDocument1.Print();
}
private void FillDataGrid()
{
try
{
sql = "SELECT CustomerID,CompanyName,ContactName,Address,City,Country,Phone,Fax FROM Customers";
conn = new OleDbConnection(connectionString);
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
ds = new DataSet("Customers");
adapter.Fill(ds, "Customers");
dataGrid.DataSource = ds.Tables[0].DefaultView;
// By default there is no DataGridTableStyle object.
// Add all DataSet table's style to the DataGrid
foreach(DataTable dTable in ds.Tables)
{
DataGridTableStyle dgStyle = new DataGridTableStyle();
dgStyle.MappingName = dTable.TableName;
dataGrid.TableStyles.Add(dgStyle);
}
// DataGrid settings
dataGrid.CaptionText = "DataGrid Printing";
dataGrid.HeaderFont = new Font("Verdana", 12);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void button2_Click(object sender, System.EventArgs e)
{
//create printerclass object
PrintC = new PrinterClass(printDocument1,dataGrid);
//preview
if (printPreviewDialog1.ShowDialog() == DialogResult.Cancel){}
}
private void printDocument2_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float CurrentX = (e.PageBounds.Width - e.MarginBounds.Left) / 2; //center
float CurrentY = e.MarginBounds.Top;
//header
string text = dataGrid.CaptionText;
Font font = dataGrid.CaptionFont;
RectangleF rect = new RectangleF(CurrentX,CurrentY,e.Graphics.MeasureString(text,font).Width,e.Graphics.MeasureString(text,font).Height);
e.Graphics.FillRectangle(new SolidBrush(dataGrid.CaptionBackColor),rect);
e.Graphics.DrawString(text,font,Brushes.Black,rect);
CurrentY = CurrentY rect.Height;
//print with given X and Y start
bool morepages = PrintC.Print(e.Graphics,ref CurrentX,ref CurrentY);
if (morepages)
{
e.HasMorePages = true;
}
//footer
CurrentX = (e.PageBounds.Width - e.MarginBounds.Left) / 2; //center
CurrentY = 20;
text = PrintC.PageCounter.ToString();
rect = new RectangleF(CurrentX,CurrentY,e.Graphics.MeasureString(text,font).Width,e.Graphics.MeasureString(text,font).Height);
e.Graphics.FillRectangle(new SolidBrush(dataGrid.CaptionBackColor),rect);
e.Graphics.DrawString(text,font,Brushes.Black,rect);
}
private void button4_Click(object sender, System.EventArgs e)
{
//create printerclass object
PrintC = new PrinterClass(printDocument2,dataGrid);
//preview with header and footer
printPreviewDialog2.ShowDialog();
}
private void button3_Click(object sender, System.EventArgs e)
{
printDialog1.ShowDialog();
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论