| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 880 人关注过本帖
标题:[转载]一个打印类
只看楼主 加入收藏
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
结帖率:66.67%
收藏
 问题点数:0 回复次数:5 
[转载]一个打印类

using System;
using System.IO;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;

namespace PrintDataGrid
{
public class MyPrintDoc : PrintDocument
{

private DataGrid _dataGrid;
private int _currentPage = 0;
private int _currentRow = 0;
private int _rowCount = 0;
private int _colCount = 0;
private int _linesPerPage = 0;
private RectangleF _headerR;
private RectangleF _bodyR;
private RectangleF _footerR;
private string _headerText = "This is a test";

// these will be created in BeginPrint and
// destroyed in EndPrint.
private Font _headerFont;
private Font _bodyFont;

private float _headerHeight = 0;
private float _bodyFontHeight = 0;
private float _footerHeight = 0;

private Brush _defaultBrush = Brushes.Black;
private Pen _defaultPen = new Pen(Brushes.Black, .25f);

public string HeaderText
{
get { return _headerText;}
set { _headerText = value; }
}

public MyPrintDoc(DataGrid dataGrid) : base()
{
_dataGrid = dataGrid;
_rowCount = ((DataSet)(dataGrid.DataSource)).Tables[0].Rows.Count;
_colCount = dataGrid.TableStyles[0].GridColumnStyles.Count;
}


protected override void OnBeginPrint(PrintEventArgs ev)
{
base.OnBeginPrint(ev) ;
// make sure we reset this before printing
_currentPage = 0;
_currentRow = 0;
_headerFont = new Font("Arial", 24, GraphicsUnit.World);
_bodyFont = new Font("Arial", 16, GraphicsUnit.World);
}

protected override void OnEndPrint(PrintEventArgs ev)
{
base.OnEndPrint(ev);
_headerFont.Dispose();
_bodyFont.Dispose();

}

protected override void OnQueryPageSettings(QueryPageSettingsEventArgs ev)
{
base.OnQueryPageSettings(ev);
}
protected int DrawDataGridRow(Graphics g, int currentLine)
{
float y = getCurrentY(currentLine, _bodyR.Top, _bodyFontHeight);
float x = _bodyR.Left;
int colGap = 5;
string cellValue;
RectangleF cellR = new RectangleF(x, y, _bodyR.Right, y);
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;

for(int j = 0; j < _colCount; j++)
{
if(_dataGrid.TableStyles[0].GridColumnStyles[j].Width > 0)
{
cellValue = _dataGrid[_currentRow, j].ToString();
cellR.X = x;
cellR.Y = y;
cellR.Width = _dataGrid.TableStyles[0].GridColumnStyles[j].Width;
cellR.Height = _bodyFontHeight;
g.DrawString(cellValue, _bodyFont, _defaultBrush, cellR, drawFormat);
x += _dataGrid.TableStyles[0].GridColumnStyles[j].Width + colGap;
}
}
return 0;
}

protected int DrawDataGridHeader(Graphics g)
{
string cellValue;
float y = getCurrentY(2, _bodyR.Top, _bodyFontHeight);
float x = _bodyR.Left;
RectangleF cellR = new RectangleF(x, y, _bodyR.Right, y);
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;

for(int j = 0; j < _colCount; j++)
{
if(_dataGrid.TableStyles[0].GridColumnStyles[j].Width > 0)
{
cellValue = _dataGrid.TableStyles[0].GridColumnStyles[j].HeaderText;
cellR.X = x;
cellR.Y = y;
cellR.Width = _dataGrid.TableStyles[0].GridColumnStyles[j].Width;
cellR.Height = _bodyFontHeight;
g.DrawString(cellValue, _bodyFont, _defaultBrush, cellR, drawFormat);
x += _dataGrid.TableStyles[0].GridColumnStyles[j].Width + 5;
}
}
y = getCurrentY(3, _bodyR.Top, _bodyFontHeight) + _bodyFontHeight / 2;
g.DrawLine(_defaultPen, _bodyR.Left, y, cellR.Right, y);

return 0;
}

protected override void OnPrintPage(PrintPageEventArgs ev)
{
base.OnPrintPage(ev);
if ( _currentPage == 0 )
{
marginInfo mi;

float leftMargin = 0f;
float rightMargin = 0f;
float topMargin = 0f;
float bottomMargin = 0f;
float pageHeight = 0f;
float pageWidth = 0f;

// retrieve the hard printer margins
IntPtr hDc = ev.Graphics.GetHdc();
try
{
mi = new marginInfo(hDc.ToInt32());
}
finally
{
ev.Graphics.ReleaseHdc(hDc);
}

ev.Graphics.PageUnit = GraphicsUnit.Inch;
ev.Graphics.PageScale = .01f;
ev.Graphics.TranslateTransform(-mi.Left, -mi.Top);

// retrieve the margins
leftMargin = ev.MarginBounds.Left - mi.Left;
rightMargin = ev.MarginBounds.Right;
topMargin = ev.MarginBounds.Top - mi.Top;
bottomMargin = ev.MarginBounds.Bottom;
pageHeight = bottomMargin - topMargin;
pageWidth = rightMargin - leftMargin;

// used to define the sections of the document
_headerHeight = _headerFont.GetHeight(ev.Graphics);
_footerHeight = _bodyFont.GetHeight(ev.Graphics);

// report header
_headerR = new RectangleF(leftMargin, topMargin, pageWidth, _headerHeight);
// report body
_bodyR = new RectangleF(leftMargin, _headerR.Bottom, pageWidth, pageHeight - _headerR.Height - _footerHeight);
// report footer
_footerR = new RectangleF(leftMargin, _bodyR.Bottom, pageWidth, _footerHeight * 2);

// how many lines can we fit on a page?
_bodyFontHeight = _footerHeight;
_linesPerPage = Convert.ToInt32(_bodyR.Height / _bodyFontHeight) - 1;

}
_currentPage++;


// uncomment these lines to draw borders around the rectangles
/*
drawRect(ev.Graphics, _defaultPen, leftMargin, topMargin, pageWidth, headerHeight);
drawRect(ev.Graphics, _defaultPen, leftMargin, ReportheaderR.Bottom, pageWidth, pageHeight - ReportheaderR.Height - footerHeight);
drawRect(ev.Graphics, _defaultPen, leftMargin, bodyR.Bottom, pageWidth, ReportfooterR.Height);

centerText(ev.Graphics, "Header section", _headerFont, _defaultBrush, ReportheaderR);
centerText(ev.Graphics, "Body section", _headerFont, _defaultBrush, bodyR);
centerText(ev.Graphics, "Footer section", _headerFont, _defaultBrush, ReportfooterR);
return;
*/

// print the header once per page
centerText(ev.Graphics, _headerText, _headerFont, _defaultBrush, _headerR);
DrawDataGridHeader(ev.Graphics);

// the header = 4 lines
int currentLine = 4;

while(currentLine + 1 < _linesPerPage && _currentRow < _rowCount )
{
DrawDataGridRow(ev.Graphics, currentLine);
currentLine ++;
_currentRow ++;
}

// page number
centerText(ev.Graphics, "Page " + _currentPage.ToString(), _bodyFont, _defaultBrush, _footerR);

// Do we have more pages to print?
if (_currentRow != _rowCount)
{
ev.HasMorePages = true;
}
else
{
ev.HasMorePages = false;
}
}

private float getCurrentY(int currentLine, float topMargin, float fontHeight)
{
return topMargin + (currentLine * fontHeight);
}

// my wrapper for DrawRectangle
private void drawRect(Graphics g, Pen p, float left, float top, float width, float height)
{
g.DrawRectangle(_defaultPen, left, top, width, height);
}

// the StringFormat class has a lot of cool features
private void centerText(Graphics g, string t, Font f, Brush b, RectangleF rect)
{
StringFormat sf = new StringFormat();
// center horizontally
sf.Alignment = StringAlignment.Center;
// center vertically
sf.LineAlignment = StringAlignment.Center;
g.DrawString(t, f, b, rect, sf);
}

// used for debugging
private void dumpRectF(RectangleF rect)
{
System.Diagnostics.Debug.WriteLine(rect.ToString());
}

public void PrintDataGrid()
{
PrintPreviewDialog previewDialog = new PrintPreviewDialog();
/*
// display a pagesetup dialog
PageSetupDialog pageSetup = new PageSetupDialog();
pageSetup.Document = mydoc;
pageSetup.MinMargins.Left = 20;
pageSetup.MinMargins.Top = 20;
pageSetup.MinMargins.Bottom = 20;
DialogResult Rc = pageSetup.ShowDialog();
if (Rc == DialogResult.Cancel)
{
return;
}
*/
// display the preview dialog
previewDialog.Document = this;
previewDialog.PrintPreviewControl.Zoom = 1.0;
previewDialog.WindowState = FormWindowState.Maximized;
previewDialog.ShowInTaskbar = true;
previewDialog.ShowDialog();

}
}

public class marginInfo
{
[DllImport("gdi32.dll")]
private static extern Int16 GetDeviceCaps([In] [MarshalAs (UnmanagedType.U4)] int hDc, [In] [MarshalAs (UnmanagedType.U2)] Int16 funct);

private float _leftMargin = 0;
private float _topMargin = 0;
private float _rightMargin = 0;
private float _bottomMargin = 0;

const short HORZSIZE = 4; /* Horizontal size in millimeters */
const short VERTSIZE = 6; /* Vertical size in millimeters */
const short HORZRES = 8; /* Horizontal width in pixels */
const short VERTRES = 10; /* Vertical height in pixels*/
const short PHYSICALOFFSETX = 112; /* Physical Printable Area x margin */
const short PHYSICALOFFSETY = 113; /* Physical Printable Area y margin */

// Modified from code originally written by Ron Allen (Ron@us-src.com).
public marginInfo(int deviceHandle)
{
// Non printable margin in pixels
float offsetX = Convert.ToSingle(GetDeviceCaps(deviceHandle, PHYSICALOFFSETX));
float offsetY = Convert.ToSingle(GetDeviceCaps(deviceHandle, PHYSICALOFFSETY));
// printable page size in pixels
float resolutionX = Convert.ToSingle(GetDeviceCaps(deviceHandle, HORZRES));
float resolutionY = Convert.ToSingle(GetDeviceCaps(deviceHandle, VERTRES));
// printable page size in current unit (in this case, converted to inches)
float horizontalSize = Convert.ToSingle(GetDeviceCaps(deviceHandle, HORZSIZE))/25.4f;
float verticalSize = Convert.ToSingle(GetDeviceCaps(deviceHandle, VERTSIZE))/25.4f;

float pixelsPerInchX = resolutionX/horizontalSize;
float pixelsPerInchY = resolutionY/verticalSize;

_leftMargin = (offsetX/pixelsPerInchX)* 100.0f;
_topMargin = (offsetY/pixelsPerInchX) * 100.0f;
_bottomMargin = _topMargin + (verticalSize * 100.0f);
_rightMargin = _leftMargin + (horizontalSize * 100.0f);
}

public float Left
{
get
{
return _leftMargin;
}
}

public float Right
{
get
{
return _rightMargin;
}
}

public float Top
{
get
{
return _topMargin;
}
}

public float Bottom
{
get
{
return _bottomMargin;
}
}

public override string ToString()
{
return "left=" + _leftMargin.ToString() + ", top=" + _topMargin.ToString() + ", right=" + _rightMargin.ToString() + ", bottom=" + _bottomMargin.ToString();
}

}
}

搜索更多相关主题的帖子: 打印 
2006-12-02 14:19
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 
好长哦,头都看晕了,先收下,呵呵.

飘过~~
2006-12-02 20:22
YSKING
Rank: 5Rank: 5
来 自:中国绿城
等 级:贵宾
威 望:16
帖 子:1380
专家分:25
注 册:2006-11-11
收藏
得分:0 

建议发个程序包,呵呵~~


仍然自由自我,永远高唱我歌,走遍千里...
2006-12-02 22:04
lusi116
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2006-8-7
收藏
得分:0 

同意思楼上!

2006-12-03 14:36
aiwei1000
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2006-7-20
收藏
得分:0 
是啊,要能直接用的最好

2006-12-18 17:36
chenxkfox
Rank: 1
等 级:新手上路
威 望:1
帖 子:123
专家分:0
注 册:2005-8-18
收藏
得分:0 

好啊,顶!转载同样支持!


,SQL SERVER 群号:17280478
2006-12-18 22:02
快速回复:[转载]一个打印类
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027222 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved