📄 misprinter.cs
字号:
using System;
using System.Drawing;
using System.Drawing.Printing;
namespace GoldPrinter
{
/// <summary>
/// 本程序为通用打印程序,单据、会计凭证、发票清单、报表、任意复杂表格、合并表格如工矿企业合同都可以由系统提供的几个默
/// 认打印对象组合打印。
/// DataGrid、DataTable、MSHFlexGrid等二维形式全部可以打印。
/// 部分对象如PrinterMargins、Sewing、GridLineFlag、GridMergeFlag等提供图例,以促进理解。
/// 后期版本将提供XML描述、SQL数据源的打印,并用管理器管理任意多个网格、文本对象、图象等,用户可以随意定义。
///
/// 程序提供:周方勇;Email:flygoldfish@sina.com。
/// 请 关 注:WebMIS.Net快速开发工具,不写一行程序开发B/S架构下MIS、OA、CRM、人事管理、档案管理等数据库网页系统。简单、实用、稳定、可靠。
/// 下 载:
/// 上海奥联:WWW.AlinkSoft.COM
/// 用友华表:WWW.CellSoft.CC
///★★★★★您可以免费使用此程序,但是请您保留此说明,以维护知识产权★★★★★
/// </summary>
public class MisPrinter:IDisposable
{
public Color BackColor = Color.White;//背景颜色
//绘图表面
private Graphics mGraphics;
//打印文档
private PrintDocument mPrintDocument;
//下一对象的起点坐标及宽
private float X,Y,Width;
//翻页用
private int mCurrentPageIndex; //当前页
private int mCurrentRowIndex; //主数据网格的当前行
private int mCurrentRowIndexForFooter; //Footer当前行
//字段
private int _rowsPerPage; //每页行数,小于等于0自适应,默认
private bool _isSewingLine; //是否打印装订线(默认无)
private bool _isPrinterMargins; //是否打印有效区域矩阵(默认无)
private bool _isSubTotalPerPage; //是否每页都要显示主数据网格当前页小计(默认否)
private string _subTotalCol; //每页小计要指定的列
private Sewing _sewing; //装订,对象的线长小于0则自动设置
private GridBorderFlag _gridBorder; //网格边框
#region 字段属性
/// <summary>
/// 每页行数,小于等于0自适应,默认
/// </summary>
public int RowsPerPage
{
get
{
return _rowsPerPage;
}
set
{
int mint = value;
if (mint < 0)
{
mint = -1;
}
_rowsPerPage = mint;
}
}
/// <summary>
/// 是否打印装订线,对象的线长小于0则自动设置
/// </summary>
public bool IsSewingLine
{
get
{
return _isSewingLine;
}
set
{
_isSewingLine = value;
}
}
/// <summary>
/// 是否打印有效区域矩阵
/// </summary>
public bool IsPrinterMargins
{
get
{
return _isPrinterMargins;
}
set
{
_isPrinterMargins = value;
}
}
/// <summary>
/// 是否每页都要显示当前页小计(默认否)
/// </summary>
public bool IsSubTotalPerPage
{
get
{
return _isSubTotalPerPage;
}
set
{
_isSubTotalPerPage = value;
}
}
/// <summary>
/// 用分号分隔的要每页小计列
/// </summary>
public string SubTotalCol
{
get
{
return _subTotalCol;
}
set
{
_subTotalCol = value;
}
}
/// <summary>
/// 装订对象,对象的线长小于0则自动设置
/// </summary>
public Sewing Sewing
{
get
{
return this._sewing;
}
set
{
if (value != null)
{
this._sewing = value;
}
else
{
this._sewing.Margin = 0; //宽度为0则不打印
}
}
}
/// <summary>
/// 网格边框
/// </summary>
public GridBorderFlag GridBorder
{
get
{
return this._gridBorder;
}
set
{
this._gridBorder = value;
}
}
#endregion
//********************打印对象********************
private Title _title; //主标题
private Caption _caption; //副标题
private Top _top; //简单的一行三列打印样式,第一列居左,第三列居右,中间列居中
private Header _header; //正文网格主体之上的几行几列的标注说明
private MultiHeader _multiHeader; //正文网格主体标题头可能需要多层合并表头说明
private Body _body; //*正文网格主体,必须,打印以此为基准
protected Footer _footer; //正文网格主体之下的几行几列的标注说明
private Bottom _bottom; //简单的一行三列打印样式,第一列居左,第三列居右,中间列居中
#region 打印对象字段属性
#region Title、Caption
/// <summary>
/// 获取或设置打印主标题,可以是文本,也可以是定义更多特性的Title对象
/// </summary>
public object Title
{
get
{
return this._title;
}
set
{
if (value != null)
{
if (value.GetType().ToString() == "System.String")
{
if (this._title == null)
{
this._title = new Title();
}
this._title.Text = (string)value;
}
else if(value.GetType().ToString() == "GoldPrinter.Title")
{
this._title = (GoldPrinter.Title)value;
}
}
}
}
/// <summary>
/// 获取或设置打印副标题,可以是文本,也可以是定义更多特性的Caption对象
/// </summary>
public object Caption
{
get
{
return this._caption;
}
set
{
if (value != null)
{
if (value.GetType().ToString() == "System.String")
{
if (this._caption == null)
{
this._caption = new Caption();
}
this._caption.Text = (string)value;
}
else if(value.GetType().ToString() == "GoldPrinter.Caption")
{
this._caption = (GoldPrinter.Caption)value;
}
}
}
}
#endregion
#region 或取或设置网格头、底,可以是以'|'分隔的字符串或或一维数组或具有更多特性的Top/Bottom对象
/// <summary>
/// 或取或设置网格头,可以是以'|'分隔的字符串或或一维数组或具有更多特性的Top对象
/// </summary>
public object Top
{
get
{
return this._top;
}
set
{
if (value != null)
{
if (value.GetType().ToString() == "System.String" || value.GetType().ToString() == "System.String[]")
{
if (this._top == null)
{
this._top = new Top();
}
this._top.DataSource = value;
}
else if(value.GetType().ToString() == "GoldPrinter.Top")
{
this._top = (GoldPrinter.Top)value;
}
}
}
}
/// <summary>
/// 或取或设置网格底,可以是以'|'分隔的字符串或或一维数组或具有更多特性的Bottom对象
/// </summary>
public object Bottom
{
get
{
return this._bottom;
}
set
{
if (value != null)
{
if (value.GetType().ToString() == "System.String" || value.GetType().ToString() == "System.String[]")
{
if (this._bottom == null)
{
this._bottom = new Bottom();
}
this._bottom.DataSource = (string)value;
}
else if(value.GetType().ToString() == "GoldPrinter.Bottom")
{
this._bottom = (GoldPrinter.Bottom)value;
}
}
}
}
#endregion
public object Header
{
get
{
return _header;
}
set
{
this._header = (GoldPrinter.Header)value;
}
}
public object MultiHeader
{
get
{
return _multiHeader;
}
set
{
this._multiHeader = (GoldPrinter.MultiHeader)value;
}
}
public object Body
{
get
{
return _body;
}
set
{
_body = (GoldPrinter.Body)value;
}
}
public object Footer
{
get
{
return this._footer;
}
set
{
this._footer = (GoldPrinter.Footer)value;
}
}
#endregion
//还可以将此程序稍微修改,用一个集体管理,动态加载打印对象,形成任意多个网格的组合体,打印任意复杂的网格
public MisPrinter()
{
mCurrentPageIndex = 1;
_rowsPerPage = 0;
mCurrentRowIndex = 0;
mCurrentRowIndexForFooter = 0;
_isSewingLine = false;
_isPrinterMargins = false;
_isSubTotalPerPage = false;
_subTotalCol = "";
_sewing = new Sewing(0,SewingDirectionFlag.Left);
mPrintDocument = new PrintDocument();
_body = new Body(); //主要对象,所以实例化
_gridBorder = GridBorderFlag.Double;
}
#region IDisposable 成员
public virtual void Dispose()
{
//...
}
#endregion
/// <summary>
/// 获取或设置数据主体网格的数据源
/// </summary>
public object DataSource
{
get
{
return this._body.DataSource;
}
set
{
this._body.DataSource = value;
}
}
/// <summary>
/// 页面设置对话框,如果需要,国庆期间继续开放,敬请关注
/// </summary>
public void PageSetup()
{
PrinterPageSetting printerPageSetting;
printerPageSetting = new PrinterPageSetting(mPrintDocument);
printerPageSetting.PrintPage += new PrintPageDelegate(this.PrintPageEventHandler);
printerPageSetting.ShowPageSetupDialog();
}
/// <summary>
/// 打印,如果需要,国庆期间继续开放,敬请关注
/// </summary>
public void Print()
{
PrinterPageSetting printerPageSetting;
printerPageSetting = new PrinterPageSetting(mPrintDocument);
printerPageSetting.PrintPage += new PrintPageDelegate(this.PrintPageEventHandler);
printerPageSetting.ShowPrintSetupDialog();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -