PrintDataGridDoc.cs
上传用户:fgkjxx
上传日期:2009-12-18
资源大小:3k
文件大小:11k
- 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();
- }
- }
- }