业余编程写办公程序很多年了,大部分时间是做插件,完整的程序,手上也只有一个,不敢说专业,但对于一些简单的需求,还是想说一说。或许对各位没用,或许可以为初学者引路,或许只是自己写给自己看,或许是因为所有代码当初也是从网络上各位大神处抄来的,再晒回网上也只是转载。
办公软件的开发,总是离不开报表的打印。之前我也是使用水晶报表、微软报表来做打印。直到1年前,有个客户说公司使用的标签格式变了,要打印的内容没变,但位置要重新调整一下,字体大小也要调整一下。一开始是让客户把标签样板给我,我重新设计报表发过去更新,本来以为问题解决了。但是没过多久又遇到类似的问题。于是我开始考虑,简单的报表格式调整,是否可以由客户自己完成。毕竟每次都由我去做服务的话,总有些不现实。于是我问遍CSDN,找遍网络各处,结果是水晶报表和微软报表无法在客户端进行修改,除非安装相应的编辑软件,例如安装VS。这个要求不要说客户会不会同意,就算是同意,我也不敢做呀。有些客户的机器已经是七八年前买的了,能正常跑就不错了,为了一个报表修改安装一个不必要的软件总是多余,还影响电脑运行速度。当然,也关注到 FastReport 是可以在客户端进行报表修改的。这也让我有过使用FastReport 的想法,但当我看到价格的时候,我还是放弃了。自己做个小程序小插件的,没必要多花钱呀,不然也挣不了钱了。
转念又想,即然FastReport可以做到我想要的,为什么不可以自己写一个报表编辑器呢?说干就干。经过分析自己制做的各类报表,发现我用得到的报表其实只有两大类,一类是需要打印表格的,一类是没有表格的。那么也就是说,只要我写的报表编辑器可以完成简单的标签打印和表格打印就可以了,其它的各种强大功能,其实也只是神的存在,与我无关的。于是,一个报表编辑器的初步设计就出来了。
一、编辑器需求
1、标签打印。标签可调整字体、位置,可编辑固定值,也可以接受外部传入的值
2、表格打印。表格可以增加减少列,调整字体,列宽,可以绑定外部传入的 Datatable。本人未做过一页上打印两个表格的报表,所以不考虑一页多个表格的情况。
二、概要设计
1、报表页面设计
1)报表页面基本参数可设置
2)报表页面分为三大部分,页头、数据区、页脚
3)标签只出现在页头、页脚区域,数据区只出现表格
2、标签设计。
1)普通文本标签。可编辑字体,位置,及是否接受指定的值传入
2)直线标签。用于报表美观,可编辑长度、位置
3)图形标签。用于打印图标,如LOGO
其它标签根据需要增加
三、详细设计
我不知道你是不是这样,反正是个人开发,我从来没有过详细设计,都是直接代码了 🙂
四、代码编写
1、文本标签类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WaterReport
{ public partial class Cls_rptLabel : Label{[Serializable]public class MyProperties{Label parentLabel;[Category("自定义打印属性"), Browsable(true), Description("显示位置,单位1/100英寸\r\n打印机可能有5mm的硬边距")]public Point Location{get{return parentLabel.Location;}set{parentLabel.Location = value;}}[Category("自定义打印属性"), Browsable(true), Description("大小")]public Size Size{get{return parentLabel.Size;}set{parentLabel.Size = value;}}[Category("自定义打印属性"), Browsable(true), Description("字体")]public Font Font{get{return parentLabel.Font;}set{parentLabel.Font = value;}}[Category("自定义打印属性"), Browsable(true), Description("文本")]public string Text{get{return parentLabel.Text;}set{parentLabel.Text = value;}}[Category("自定义打印属性"), Browsable(true), Description("名称")]public string Name{get{return parentLabel.Name;}set{parentLabel.Name = value;}}public MyProperties(Label label){parentLabel = label; }}public Cls_rptLabel(){CreateBase();}/// <summary>/// 构建控件的基础/// </summary>private void CreateBase(){InitializeComponent();this.BackColor = System.Drawing.SystemColors.ActiveCaption;this.Location = new Point(10, 20);this.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));MyProperties mp = new MyProperties(this);this.Tag = mp;toolStripMenuItem1.Click += new EventHandler(toolStripMenuItem1_Click);}/// <summary>/// 根据传入的字符串构建控件;/// </summary>/// <param name="newString">包含构建信息的字符串</param>/// <param name="frm">报表主窗口</param>public Cls_rptLabel(string newString, frmWaterReportEdit frm){CreateBase();string[] rptItem = newString.Split('\t');string[] item;FontConverter fc = new FontConverter();for (int i = 2; i < rptItem.Length; i++){item = rptItem[i].Split('\a');if (item[0] == "Location"){string[] sLocation = item[1].Split(',');this.Location = new Point(Convert.ToInt32(sLocation[0]), Convert.ToInt32(sLocation[1]));}else if (item[0] == "Size"){string[] sSize = item[1].Split(',');this.Size = new Size(Convert.ToInt32(sSize[0]), Convert.ToInt32(sSize[1]));}else if (item[0] == "Font"){this.Font = (Font)fc.ConvertFromString(item[1]);}else if (item[0] == "Text"){this.Text = item[1];}else if (item[0] == "Name"){this.Name = item[1];}}if (rptItem[1] == "Header"){frm.AddEditControl(this);frm.pnlHeader.Controls.Add(this);}else if (rptItem[1] == "Footer"){frm.AddEditControl(this);frm.pnlFooter.Controls.Add(this);}}/// <summary>/// 获取保存字符串/// </summary>/// <param name="ToWhere">是属于哪一部份</param>/// <returns></returns>public string GetSaveString(string ToWhere){FontConverter fc = new FontConverter();return "\rCls_rptLabel"+ "\t" + ToWhere+ "\tLocation\a"+ this.Location.X.ToString()+ ","+ this.Location.Y.ToString()+ "\tSize\a"+ this.Size.Width.ToString()+ ","+ this.Size.Height.ToString()+ "\tFont\a"+ fc.ConvertToInvariantString(this.Font)+ "\tText\a"+ this.Text+ "\tName\a"+ this.Name;}/// <summary>/// 把控件画出来/// </summary>/// <param name="g">画布信息</param>/// <param name="LocationOffset">控件绘画的偏移量</param>public void DrawMe(Graphics g, Point LocationOffset){SizeF mSizef = new SizeF(this.Width, this.Height);Point pLocation = new Point(this.Location.X + LocationOffset.X, this.Location.Y + LocationOffset.Y);RectangleF rf = new RectangleF(pLocation, mSizef);g.DrawString(this.Text, this.Font, Brushes.Black, rf);}void toolStripMenuItem1_Click(object sender, EventArgs e){this.Dispose();} }
}
2、直线标签类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WaterReport
{/// <summary>/// 自定义报表横线,纵线控件/// </summary>public partial class Cls_rptDrawLineHV : UserControl{[Serializable]public class MyProperties{Cls_rptDrawLineHV parentUC;[Category("自定义属性"), Browsable(true), Description("直线的 X 坐标")]public Point X{get{return parentUC.Location;}set{parentUC.Location = value;}} [Category("自定义属性"), Browsable(true), Description("直线长度")]public int Long{get{return parentUC.LineLong;}set{parentUC.LineLong = value;if (BannerLine == true){parentUC.Width = parentUC.LineLong;}else{parentUC.Height = parentUC.LineLong;}parentUC.Refresh();}}[Category("自定义属性"), Browsable(true), Description("True 为横线,False 为竖线")]public bool BannerLine{get{return parentUC.bolBannerLine;}set{parentUC.bolBannerLine = value;if (value == true){parentUC.Width = Long;}else{parentUC.Height = Long;}parentUC.Refresh();}}public MyProperties(Cls_rptDrawLineHV cwdl){parentUC = cwdl;}}/// <summary>/// 直线的 X 坐标/// </summary>public Point PointX; /// <summary>/// 直线长度/// </summary>public int PointWidth;/// <summary>/// 标记是否是画横线/// </summary>public bool bolBannerLine;/// <summary>/// 直线长度/// </summary>public int LineLong;/// <summary>/// 构造函数/// </summary>public Cls_rptDrawLineHV(){CreateBase();}/// <summary>/// 构建控件的基础/// </summary>private void CreateBase(){InitializeComponent();bolBannerLine = true;PointX = new Point(0, 0);LineLong = 100;PointWidth = 1;MyProperties mp = new MyProperties(this);this.Tag = mp;toolStripMenuItem1.Click += new EventHandler(toolStripMenuItem1_Click);}/// <summary>/// 根据传入的字符串构建控件;/// </summary>/// <param name="newString">包含构建信息的字符串</param>/// <param name="frm">报表主窗口</param>public Cls_rptDrawLineHV(string newString, frmWaterReportEdit frm){CreateBase();string[] rptItem = newString.Split('\t');string[] item;//这里的添加判断只能在for之前,否则添加后控件属性会被修改为默认值if (rptItem[1] == "Header"){frm.AddEditControl(this);frm.pnlHeader.Controls.Add(this);}else if (rptItem[1] == "Footer"){frm.AddEditControl(this);frm.pnlFooter.Controls.Add(this);}for (int i = 2; i < rptItem.Length; i++){item = rptItem[i].Split('\a');if (item[0] == "BannerLine"){this.bolBannerLine = item[1] == "TRUE" ? true : false;}else if (item[0] == "PointX"){string[] sLocation = item[1].Split(',');this.Location = new Point(Convert.ToInt32(sLocation[0]), Convert.ToInt32(sLocation[1]));}else if (item[0] == "LineLong"){this.LineLong = Convert.ToInt32(item[1]);if (this.bolBannerLine == true){this.Width = this.LineLong;}else{this.Height = this.LineLong;} }}}/// <summary>/// 获取保存字符串/// </summary>/// <param name="ToWhere">是属于哪一部份</param>/// <returns></returns>public string GetSaveString(string ToWhere){return "\rCls_rptDrawLineHV"+ "\t" + ToWhere+ "\tBannerLine\a" + (this.bolBannerLine == true ? "TRUE" : "FALSE")+ "\tPointX\a" + this.Location.X.ToString() + "," + this.Location.Y.ToString()+ "\tLineLong\a" + this.LineLong.ToString();}/// <summary>/// 把控件画出来/// </summary>/// <param name="g">画布信息</param>/// <param name="LocationOffset">控件绘画的偏移量</param>public void DrawMe(Graphics g, Point LocationOffset){//Cls_rptDrawLineHV cline = (Cls_rptDrawLineHV)(c);Pen p = new Pen(Color.Black, this.PointWidth);Point drawLocation = new Point(this.Location.X + LocationOffset.X, this.Location.Y + LocationOffset.Y);if (this.bolBannerLine == true){//画横线g.DrawLine(p, new Point(drawLocation.X, drawLocation.Y), new Point(drawLocation.X + this.LineLong, drawLocation.Y));}else{g.DrawLine(p, new Point(drawLocation.X, drawLocation.Y), new Point(drawLocation.X, drawLocation.Y + this.LineLong));}}void toolStripMenuItem1_Click(object sender, EventArgs e){this.Dispose();} private void Cls_WaterDrawLine_Paint(object sender, PaintEventArgs e){e.Graphics.Clear(this.Parent.BackColor);Graphics g = e.Graphics;Pen p = new Pen(Color.Black, PointWidth);if (bolBannerLine == true){//画横线g.DrawLine(p, new Point(0, 0), new Point(this.LineLong, 0));}else{g.DrawLine(p, new Point(0, 0), new Point(0, this.LineLong));}}protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified){//base.SetBoundsCore(x, y, width, height, specified); if (bolBannerLine == true){//在上一句的基础上修改,意思是设置控件固定高度为10base.SetBoundsCore(x, y, width, 20, specified);}else{base.SetBoundsCore(x, y, 20, height, specified);}}private void Cls_rptWaterDrawLineHV_Resize(object sender, EventArgs e){if (bolBannerLine == true){LineLong = this.Width;}else{LineLong = this.Height;}}}
}
3、图形标签类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Compression;namespace WaterReport
{public partial class Cls_rptImage : PictureBox{[Serializable]public class MyProperties{PictureBox parentPicturebox;[Category("自定义打印属性"), Browsable(true), Description("显示位置,单位1/100英寸\r\n打印机可能有5mm的硬边距")]public Point Location{get{return parentPicturebox.Location;}set{parentPicturebox.Location = value;}}[Category("自定义打印属性"), Browsable(true), Description("大小")]public Size Size{get{return parentPicturebox.Size;}set{parentPicturebox.Size = value;}}[Category("自定义打印属性"), Browsable(true), Description("名称")]public string Name{get{return parentPicturebox.Name;}set{parentPicturebox.Name = value;}}[Category("自定义打印属性"), Browsable(true), Description("名称")]public Image Picture{get{return parentPicturebox.Image;}set{parentPicturebox.Image = value;}}public MyProperties(PictureBox pbox){parentPicturebox = pbox;}}public Cls_rptImage(){ CreateBase();}/// <summary>/// 构建控件的基础/// </summary>private void CreateBase(){InitializeComponent();this.BackColor = System.Drawing.SystemColors.ActiveCaption;this.Location = new Point(10, 20);this.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));MyProperties mp = new MyProperties(this);this.Tag = mp; this.Image= global::WaterReport.Properties.Resources.LJB_bmp;toolStripMenuItem1.Click += new EventHandler(toolStripMenuItem1_Click);}void toolStripMenuItem1_Click(object sender, EventArgs e){this.Dispose();}#region Image 与 string 互转,默认进行字符串压缩#region 压缩与解压字符串,网络下载代码/// <summary>/// 字符串压缩/// </summary>/// <param name="strSource"></param>/// <returns></returns>public static byte[] Compress(byte[] data){try{MemoryStream ms = new MemoryStream();GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);zip.Write(data, 0, data.Length);zip.Close();byte[] buffer = new byte[ms.Length];ms.Position = 0;ms.Read(buffer, 0, buffer.Length);ms.Close();return buffer;}catch (Exception e){throw new Exception(e.Message);}}/// <summary>/// 字符串解压缩/// </summary>/// <param name="strSource"></param>/// <returns></returns>public static byte[] Decompress(byte[] data){try{MemoryStream ms = new MemoryStream(data);GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true);MemoryStream msreader = new MemoryStream();byte[] buffer = new byte[0x1000];while (true){int reader = zip.Read(buffer, 0, buffer.Length);if (reader <= 0){break;}msreader.Write(buffer, 0, reader);}zip.Close();ms.Close();msreader.Position = 0;buffer = msreader.ToArray();msreader.Close();return buffer;}catch (Exception e){throw new Exception(e.Message);}}/// <summary>/// 压缩字符串/// </summary>/// <param name="str"></param>/// <returns></returns>public static string CompressString(string str){string compressString = "";byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str);byte[] compressAfterByte = Compress(compressBeforeByte);//compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte); compressString = Convert.ToBase64String(compressAfterByte);return compressString;}/// <summary>/// 解压字符串/// </summary>/// <param name="str"></param>/// <returns></returns>public static string DecompressString(string str){string compressString = "";//byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str); byte[] compressBeforeByte = Convert.FromBase64String(str);byte[] compressAfterByte = Decompress(compressBeforeByte);compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte);return compressString;}#endregion/// <summary>/// 字符串转为 Image/// </summary>/// <param name="imageString">字符串</param>/// <returns></returns>private Image StringToImage(string imageString){imageString = DecompressString(imageString);//将字符串按64位计转为 byte[]byte[] igeBytes = Convert.FromBase64String(imageString);MemoryStream mStream = new MemoryStream(igeBytes, 0, igeBytes.Length);mStream.Write(igeBytes, 0, igeBytes.Length);return Image.FromStream(mStream);}/// <summary>/// Image 转为字符串/// </summary>/// <param name="ige">等转 Image</param>/// <returns></returns>public string ImageToString(Image ige){Bitmap bmp = new Bitmap(ige);MemoryStream mStream = new MemoryStream();bmp.Save(mStream, System.Drawing.Imaging.ImageFormat.Bmp);byte[] bt = new byte[mStream.Length];mStream.Position = 0;//设置流的初始位置mStream.Read(bt, 0, Convert.ToInt32(bt.Length));//return Convert.ToBase64String(bt);string resultStr= Convert.ToBase64String(bt);return CompressString(resultStr);}#endregion/// <summary>/// 根据传入的字符串构建控件;/// </summary>/// <param name="newString">包含构建信息的字符串</param>/// <param name="frm">报表主窗口</param>public Cls_rptImage(string newString, frmWaterReportEdit frm){CreateBase();string[] rptItem = newString.Split('\t');string[] item;//FontConverter fc = new FontConverter();for (int i = 2; i < rptItem.Length; i++){item = rptItem[i].Split('\a');if (item[0] == "Location"){string[] sLocation = item[1].Split(',');this.Location = new Point(Convert.ToInt32(sLocation[0]), Convert.ToInt32(sLocation[1]));}else if (item[0] == "Size"){string[] sSize = item[1].Split(',');this.Size = new Size(Convert.ToInt32(sSize[0]), Convert.ToInt32(sSize[1]));} else if (item[0] == "Name"){this.Name = item[1];}else if (item[0] == "Image"){this.Image = StringToImage(item[1]);}}if (rptItem[1] == "Header"){frm.AddEditControl(this);frm.pnlHeader.Controls.Add(this);}else if (rptItem[1] == "Footer"){frm.AddEditControl(this);frm.pnlFooter.Controls.Add(this);}}/// <summary>/// 获取保存字符串/// </summary>/// <param name="ToWhere">是属于哪一部份</param>/// <returns></returns>public string GetSaveString(string ToWhere){return "\rCls_rptImage"+ "\t" + ToWhere+ "\tLocation\a"+ this.Location.X.ToString()+ ","+ this.Location.Y.ToString()+ "\tSize\a"+ this.Size.Width.ToString()+ ","+ this.Size.Height.ToString()+ "\tName\a"+ this.Name+ "\tImage\a"+ ImageToString(this.Image);}/// <summary>/// 把控件画出来/// </summary>/// <param name="g">画布信息</param>/// <param name="LocationOffset">控件绘画的偏移量</param>public void DrawMe(Graphics g, Point LocationOffset){SizeF mSizef = new SizeF(this.Width, this.Height);Point pLocation = new Point(this.Location.X + LocationOffset.X, this.Location.Y + LocationOffset.Y);RectangleF rf = new RectangleF(pLocation, mSizef);g.DrawImage(this.Image, rf);}}
}
4、表格标签类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;namespace WaterReport
{public partial class Cls_rptTable : DataGridView{[Serializable]public class MyProperties{public Cls_rptTable parentdgv; [Category("自定义打印属性"), Browsable(true), Description("字体")]public Font Font{get{return parentdgv.Font;}set{parentdgv.Font = value;}}[Category("自定义打印属性"), Browsable(true), Description("位置")]public Point Location{get{return parentdgv.Location;}set{parentdgv.Location = value;}}[Category("自定义打印属性"), Browsable(true), Description("行高")]public int RowsHeight{get{return parentdgv.ColumnHeadersHeight;}set{parentdgv.ColumnHeadersHeight = value;}}[Category("自定义打印属性"), Browsable(false), Description("各列宽")]public PrintColumn[] PrintColumns{get{PrintColumn[] pc = new PrintColumn[parentdgv.Columns.Count];for (int i = 0; i < parentdgv.Columns.Count; i++){pc[i] = (PrintColumn)parentdgv.Columns[i];}return pc;}}[Category("自定义打印属性"), Browsable(true), Description("值<=0时为打印默认行数")]public int OnePageRowCount{get{return parentdgv.RegularPagePrintCount;}set{parentdgv.RegularPagePrintCount = value;}}public MyProperties(Cls_rptTable d){parentdgv = d;}}public Cls_rptTable(){CreateBase();}/// <summary>/// 构建控件的基础/// </summary>private void CreateBase(){InitializeComponent();#region 处理右键事件toolStripMenuItem1.Click += new EventHandler(toolStripMenuItem1_Click);toolStripMenuItem2.Click += new EventHandler(toolStripMenuItem2_Click);toolStripMenuItem3.Click += new EventHandler(toolStripMenuItem3_Click);#endregionthis.RowHeadersVisible = false;MyProperties mp = new MyProperties(this);this.Tag = mp;}/// <summary>/// 根据传入的字符串构建控件;/// </summary>/// <param name="newString">包含构建信息的字符串</param>/// <param name="frm">报表主窗口</param>public Cls_rptTable(string newString, frmWaterReportEdit frm){CreateBase();string[] rptItem = newString.Split('\t');string[] item; this.Columns.Clear();this.MouseUp += new MouseEventHandler(frm.MyMouseUp);frm.pnlData.Controls.Add(this);string[] tbItem;for (int i = 1; i < rptItem.Length; i++){tbItem = rptItem[i].Split('\'');if (tbItem[0] == "Table"){#region 表格信息for (int k = 1; k < tbItem.Length; k++){item = tbItem[k].Split('\a');if (item[0] == "Location"){string[] sLocation = item[1].Split(',');this.Location = new Point(Convert.ToInt32(sLocation[0]), Convert.ToInt32(sLocation[1]));}else if (item[0] == "RowsHeight"){this.ColumnHeadersHeight = Convert.ToInt32(item[1]);}else if (item[0] == "Font"){FontConverter fc = new FontConverter();this.Font = (Font)fc.ConvertFromString(item[1]);}else if (item[0] == "OnePageRowCount"){this.RegularPagePrintCount = Convert.ToInt32(item[1]);}}#endregion}else if (tbItem[0] == "Column"){#region 列信息Cls_rptTable.PrintColumn cpc = new Cls_rptTable.PrintColumn();cpc.SortMode = DataGridViewColumnSortMode.NotSortable;for (int k = 1; k < tbItem.Length; k++){item = tbItem[k].Split('\a');if (item[0] == "Name"){cpc.Name = item[1];}else if (item[0] == "Width"){cpc.Width = Convert.ToInt32(item[1]);}else if (item[0] == "HeaderText"){cpc.HeaderText = item[1];}else if (item[0] == "Alignment"){if (item[1] == "Left"){cpc.myAlignment = Cls_rptTable.PrintColumn.Alignment.Left;}else if (item[1] == "Middle"){cpc.myAlignment = Cls_rptTable.PrintColumn.Alignment.Middle;}else if (item[1] == "Right"){cpc.myAlignment = Cls_rptTable.PrintColumn.Alignment.Right;}}else if (item[0] == "AutoFont"){cpc.bolAutoFont = item[1] == "TRUE" ? true : false;}else if (item[0] == "SumRowSet"){cpc.ToolTipText = item[1];}}this.Columns.Add(cpc);this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;this.ReSizeMe();#endregion}}this.Rows.Add();}#region 获取当前页打印的表格行范围/// <summary>/// 当前页打印数据的范围绕类/// </summary>public class cls_CurrentPrintDataArea{/// <summary>/// 开始打印的行/// </summary>public int StartLocation;/// <summary>/// 打印的行数/// </summary>public int RowLong;}public cls_CurrentPrintDataArea CurrentPrintDataArea;#endregion/// <summary>/// 已打印行数/// </summary>public int PrintedCount = 0;/// <summary>/// 每页固定打印行数/// </summary>public int RegularPagePrintCount; /// <summary>/// 打印的总页数/// </summary>public int AllPageNumber;/// <summary>/// 把控件画出来/// </summary>/// <param name="e">打印事件数据</param>/// <param name="dt">将打印的数据表</param>/// <param name="HeaderHeight">固定页头高度</param>/// <param name="FooterHeight">固定页脚高度</param>/// <param name="OnceRowCount">批定一页打印的行数,/// -1时自动计算,/// 如果是批定行数大于能打印行数时,/// 自动设置值为自动计算行数</param>public void DrawMe(PrintPageEventArgs e, DataTable dt,int HeaderHeight,int FooterHeight,int OnceRowCount){CurrentPrintDataArea = new cls_CurrentPrintDataArea();if (dt == null) { return; } //取得对应的Graphics对象 Graphics g = e.Graphics;//表格线样式Pen pen = new Pen(Brushes.Black, 1);#region 获得相关页面X坐标、Y坐标、打印区域宽度、长度int x = e.PageSettings.Margins.Left + this.Location.X;int y = e.PageSettings.Margins.Top + this.Location.Y + HeaderHeight;int width = e.PageSettings.PaperSize.Width- e.PageSettings.Margins.Left- e.PageSettings.Margins.Right- this.Location.X;int height = e.PageSettings.PaperSize.Height- y- e.PageSettings.Margins.Bottom- FooterHeight;//减去页脚高 if (x < 0 || y < 0){MessageBox.Show("页面设置错误");return;}#endregion#region 打印表格主体前设置及打印//定义打印字体Font font = this.Font;//使打印的字体比设置的小一号//Font font = new Font(this.Font.FontFamily, (this.Font.Size - (float)0.1), this.Font.Style);//设置字号缩小1号//NotPrintCount是除去打印过的行数后剩下的行数int NotPrintCount = dt.Rows.Count - PrintedCount;//maxPageRow是当前设置下该页面可以打印的最大行数int maxPageRow;if (RegularPagePrintCount > 0&& height / (int)font.GetHeight() >= RegularPagePrintCount){//只有当设置打印的行数小于或等于可打印的最大行数时,才启用固定行打印,否则只能打印可打的最大行数maxPageRow = RegularPagePrintCount;}else{maxPageRow = height / (int)font.GetHeight();}#region 计算总页if (AllPageNumber == 0 && dt!=null){AllPageNumber = dt.Rows.Count % maxPageRow == 0 ? dt.Rows.Count / maxPageRow : dt.Rows.Count / maxPageRow + 1;}#endregion// 表格横线的长度int rowLineLength = 0;for (int i = 0; i < this.Columns.Count; i++){rowLineLength += this.Columns[i].Width;}//这里的 Pen 可以自定义,但为了快速开发,暂不做//因为是表格,先画一条水平直线g.DrawLine(pen, new Point(x, y), new Point(x + rowLineLength, y));//这里要根据各列的宽来画,即要指定列宽 #region 画出表格各列的列标题int tmpX = 0;for (int i = 0; i < this.Columns.Count; i++){string head = this.Columns[i].HeaderText;g.DrawString(head, font, Brushes.Black, x + tmpX, y);tmpX += this.Columns[i].Width;}#endregion//画完标题,再画一条直线g.DrawLine(pen, new Point(x, y + (int)font.GetHeight()), new Point(x + rowLineLength, y + (int)font.GetHeight())); SizeF mSizeF; //用于计算文本大小Point pLocation;//文本打印位置RectangleF rf; //文本打印面积 #endregion#region 打印表格内容#region 计算本页要打印的行数int thisPagePrintRowCount;//本页要打印的行数int YLong = 0;//本面竖线的长度 if (NotPrintCount >= maxPageRow){thisPagePrintRowCount = PrintedCount + maxPageRow;YLong = maxPageRow;}else{thisPagePrintRowCount = dt.Rows.Count;YLong = dt.Rows.Count - PrintedCount; }#endregion//设置当前页打印的数据范围,以便于外部进行统计CurrentPrintDataArea.StartLocation = PrintedCount;CurrentPrintDataArea.RowLong = YLong; //thisPagePrintRowCount;for (int i = PrintedCount; i < thisPagePrintRowCount; i++){//以上循环为逐行循环 tmpX = 0;//单元格内容打印的 X 偏移量for (int col = 0; col < this.Columns.Count; col++){//打印每个单元格内的数据 if (dt.Columns.IndexOf(this.Columns[col].Name) < 0){//没有传入相关列时tmpX += this.Columns[col].Width;continue;}string temp = dt.Rows[PrintedCount][this.Columns[col].Name].ToString(); mSizeF = new SizeF(this.Columns[col].Width, (int)font.GetHeight());pLocation = new Point(x + tmpX, y + (PrintedCount % maxPageRow) * (int)font.GetHeight() + (int)font.GetHeight());rf = new RectangleF(pLocation, mSizeF);//计算当前列可打印的高和宽#region 处理自动缩小打印if (((PrintColumn)this.Columns[col]).bolAutoFont == true){//如果设置了自动缩小打印font = new Font(font.FontFamily, (font.Size - (float)1), font.Style);//设置字号缩小1号SizeF tmpSizeF = g.MeasureString(temp, font);//获取当前字体打印字符所需宽度while (font.Size > 8 && tmpSizeF.Width > rf.Width){//格子不够打印,缩小字体 font = new Font(font.FontFamily, (font.Size - (float)0.1), font.Style);//设置字号缩小1号tmpSizeF = g.MeasureString(temp, font);//获取当前字体打印字符所需宽度}}#endregionStringFormat sf = new StringFormat();//sf.LineAlignment =StringAlignment.Far;if (((PrintColumn)this.Columns[col]).myAlignment == PrintColumn.Alignment.Left){sf.Alignment = StringAlignment.Near;}else if (((PrintColumn)this.Columns[col]).myAlignment == PrintColumn.Alignment.Right){sf.Alignment = StringAlignment.Far;}else if (((PrintColumn)this.Columns[col]).myAlignment == PrintColumn.Alignment.Middle){sf.Alignment = StringAlignment.Center;}g.DrawString(temp, font, Brushes.Black, rf,sf);font = this.Font;//打印完后恢复设置的字体大小(如果未设置自动缩小字体则无意义)tmpX += this.Columns[col].Width;} //打印完一行后,继续打印一条直线g.DrawLine(pen, new Point(x, y + (PrintedCount % maxPageRow) * (int)font.GetHeight() + 2 * (int)font.GetHeight()), new Point(x + rowLineLength, y + (PrintedCount % maxPageRow) * (int)font.GetHeight() + 2 * (int)font.GetHeight()));PrintedCount++;}#region 打印剩余的空白行if (RegularPagePrintCount > 0){//当设置了打印固定行时,最后一页要用空行补齐for (int i = (PrintedCount % maxPageRow); i < maxPageRow; i++){//打印完一行后,继续打印一条直线g.DrawLine(pen, new Point(x, y + i * (int)font.GetHeight() + 2 * (int)font.GetHeight()), new Point(x + rowLineLength, y + i * (int)font.GetHeight() + 2 * (int)font.GetHeight()));}}#endregion#region 打印垂直直线tmpX = 0; for (int i = 0; i <= this.Columns.Count; i++){if (i > 0){tmpX += this.Columns[i - 1].Width;}if (RegularPagePrintCount > 0){g.DrawLine(pen, new Point(x + tmpX, y), new Point(x + tmpX, y + maxPageRow * (int)font.GetHeight() + (int)font.GetHeight()));}else{g.DrawLine(pen, new Point(x + tmpX, y), new Point(x + tmpX, y + YLong * (int)font.GetHeight() + (int)font.GetHeight()));}}#endregion #endregion#region 指定HasMorePages值,如果页面最大行数小于剩下的行数,则返回true(还有),否则返回falseif (maxPageRow < NotPrintCount){e.HasMorePages = true;}else{e.HasMorePages = false;PrintedCount = 0;}#endregion}void toolStripMenuItem3_Click(object sender, EventArgs e){//删除表this.Dispose();}void toolStripMenuItem2_Click(object sender, EventArgs e){//删除列if (this.CurrentCell!=null){this.Columns.RemoveAt(this.CurrentCell.ColumnIndex);}ReSizeMe();}void toolStripMenuItem1_Click(object sender, EventArgs e){//新添列 PrintColumn pcol = new PrintColumn();pcol.SortMode = DataGridViewColumnSortMode.NotSortable;pcol.HeaderText = " ";if (this.CurrentCell == null){this.Columns.Add(pcol);}else{this.Columns.Insert(this.CurrentCell.ColumnIndex, pcol);}ReSizeMe();this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; }/// <summary>/// 改变大小/// </summary>public void ReSizeMe(){if (this.Columns.Count == 0){this.Size = new Size(100, this.ColumnHeadersHeight * 2);}else{int len = 0;for (int i = 0; i < this.Columns.Count; i++){len += this.Columns[i].Width;}this.Size = new Size(len+50, this.ColumnHeadersHeight * 2);} }private void Cls_rptTable_ColumnAdded(object sender, DataGridViewColumnEventArgs e){ReSizeMe();}private void Cls_rptTable_ColumnRemoved(object sender, DataGridViewColumnEventArgs e){ReSizeMe();}private void Cls_rptTable_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e){ReSizeMe();}private void Cls_rptTable_ColumnHeadersHeightChanged(object sender, EventArgs e){ReSizeMe();}/ <summary>/ 设置统计方式/ </summary>//public enum SumRowStyle//{// /// <summary>// /// 正常,不统计// /// </summary>// Normal,// /// <summary>// /// 设置参数值// /// </summary>// Parameter,// /// <summary>// /// 合计本列// /// </summary>// Sum,// /// <summary>// /// 统计本列行数// /// </summary>// Count,// /// <summary>// /// 本格与前一格合并// /// 即不打印// /// </summary>// NoPrint//} /// <summary>/// 自定义打印列类/// </summary>public class PrintColumn : DataGridViewTextBoxColumn{/// <summary>/// 自动设置字体大小/// </summary>public bool bolAutoFont = false;public enum Alignment{Left, Middle, Right}public Alignment myAlignment;public class MyProperties{public PrintColumn dgvtbc;public MyProperties(PrintColumn d){dgvtbc = d;}[Category("自定义打印属性"), Browsable(true), Description("宽度")]public int pWidth{get{return dgvtbc.Width;}set{dgvtbc.Width = value;}}[Category("自定义打印属性"), Browsable(true), Description("列名")]public string pName{get{return dgvtbc.Name;}set{dgvtbc.Name = value;}}[Category("自定义打印属性"), Browsable(true), Description("列文本")]public string pHeaderText{get{return dgvtbc.HeaderText;}set{dgvtbc.HeaderText = value;}}[Category("自定义打印属性"), Browsable(true), Description("文本显示方式")]public Alignment pAlignment{get{ //return (Alignment)dgvtbc.DefaultCellStyle.Tag;return dgvtbc.myAlignment;}set{//dgvtbc.DefaultCellStyle.Tag = value;dgvtbc.myAlignment = value;}}/// <summary>/// 当当前设置字体不能全部打印时/// 是否自动缩小字体打印/// </summary>[Category("自定义打印属性"), Browsable(true), Description("当当前设置字体不能全部打印时,是否自动缩小字体打印")]public bool AutoFont{///此处使用 Frozen 做为是否自缩小字体的标记///因Frozen暂时不会对打印设置有影响get{ return dgvtbc.bolAutoFont;}set{ dgvtbc.bolAutoFont = value;}} }}private void Cls_rptTable_CellClick(object sender, DataGridViewCellEventArgs e){if (e.RowIndex == -1 || e.ColumnIndex == -1){PrintColumn.MyProperties pcmp = new PrintColumn.MyProperties((PrintColumn)this.Columns[this.CurrentCell.ColumnIndex]);this.Tag = pcmp;}else{MyProperties mp = new MyProperties(this);this.Tag = mp;}}}
}
当然,实际的报表中还可能遇到很多不同的标签,如页码、合计等。这里毕竟只能讲个大概,以上几个典型的标签做出来之后,其它的根据自己需要,我相信也是很简单的了。至少我在编写时是这样的,就上面这几个是难点。
有了编辑器的控件,那接下来就是编写编辑器了。编辑器主要有文件功能(新建、打开、保存报表)、控件选择增加、控件显示、控件属性显示及编辑几个部分组成,我做的是以下样式。
当然,光看这个很多东西是无法看清楚的,下面是窗体代码,有兴趣的自己弄到VS中看吧,编写时关键的位置我也注释了,没注释的基本也是一看就明白的。
窗体布局代码:
namespace WaterReport
{partial class frmWaterReportEdit{/// <summary>/// Required designer variable./// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// Clean up any resources being used./// </summary>/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>protected override void Dispose(bool disposing){if (disposing && (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.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmWaterReportEdit));this.toolStrip1 = new System.Windows.Forms.ToolStrip();this.toolStripButton1 = new System.Windows.Forms.ToolStripDropDownButton();this.加入页头ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();this.加入页脚ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();this.toolStripButton7 = new System.Windows.Forms.ToolStripButton();this.toolStripLabel1 = new System.Windows.Forms.ToolStripDropDownButton();this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();this.页肢直线ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();this.toolStripSplitButton3 = new System.Windows.Forms.ToolStripDropDownButton();this.插入页对ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();this.插入页脚ToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();this.toolStripDropDownButton1 = new System.Windows.Forms.ToolStripDropDownButton();this.插入页头ToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();this.插入页脚ToolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();this.toolStripSplitButton1 = new System.Windows.Forms.ToolStripDropDownButton();this.页脚ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();this.页头ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();this.toolStripSplitButton2 = new System.Windows.Forms.ToolStripDropDownButton();this.插入页脚ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();this.插入页头ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();this.pptGrid = new System.Windows.Forms.PropertyGrid();this.splitter3 = new System.Windows.Forms.Splitter();this.toolStrip2 = new System.Windows.Forms.ToolStrip();this.toolStripButton4 = new System.Windows.Forms.ToolStripButton();this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();this.toolStripButton3 = new System.Windows.Forms.ToolStripButton();this.toolStripButton5 = new System.Windows.Forms.ToolStripButton();this.toolStripButton6 = new System.Windows.Forms.ToolStripButton();this.printDocument = new System.Drawing.Printing.PrintDocument();this.pnlPage = new System.Windows.Forms.Panel();this.panel1 = new System.Windows.Forms.Panel();this.grbData = new System.Windows.Forms.GroupBox();this.pnlData = new System.Windows.Forms.Panel();this.splitter4 = new System.Windows.Forms.Splitter();this.splitter2 = new System.Windows.Forms.Splitter();this.grbFooter = new System.Windows.Forms.GroupBox();this.pnlFooter = new System.Windows.Forms.Panel();this.grbHeader = new System.Windows.Forms.GroupBox();this.pnlHeader = new System.Windows.Forms.Panel();this.button1 = new System.Windows.Forms.Button();this.splitter1 = new System.Windows.Forms.Splitter();this.printPreviewDialog = new System.Windows.Forms.PrintPreviewDialog();this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();this.openFileDialog = new System.Windows.Forms.OpenFileDialog();this.printDialog1 = new System.Windows.Forms.PrintDialog();this.panel2 = new System.Windows.Forms.Panel();this.groupBox2 = new System.Windows.Forms.GroupBox();this.lstFooter = new System.Windows.Forms.ListBox();this.splitter5 = new System.Windows.Forms.Splitter();this.groupBox1 = new System.Windows.Forms.GroupBox();this.lstHeader = new System.Windows.Forms.ListBox();this.splitter6 = new System.Windows.Forms.Splitter();this.toolStripSplitButton4 = new System.Windows.Forms.ToolStripDropDownButton();this.页脚ToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();this.页头ToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();this.toolStrip1.SuspendLayout();this.toolStrip2.SuspendLayout();this.pnlPage.SuspendLayout();this.panel1.SuspendLayout();this.grbData.SuspendLayout();this.grbFooter.SuspendLayout();this.grbHeader.SuspendLayout();this.panel2.SuspendLayout();this.groupBox2.SuspendLayout();this.groupBox1.SuspendLayout();this.SuspendLayout();// // toolStrip1// this.toolStrip1.Dock = System.Windows.Forms.DockStyle.Left;this.toolStrip1.Font = new System.Drawing.Font("微软雅黑", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.toolStrip1.ImageScalingSize = new System.Drawing.Size(30, 30);this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {this.toolStripButton1,this.toolStripButton7,this.toolStripLabel1,this.toolStripSplitButton3,this.toolStripDropDownButton1,this.toolStripSeparator1,this.toolStripSplitButton1,this.toolStripSplitButton2,this.toolStripSplitButton4});this.toolStrip1.Location = new System.Drawing.Point(0, 47);this.toolStrip1.Name = "toolStrip1";this.toolStrip1.Size = new System.Drawing.Size(115, 484);this.toolStrip1.TabIndex = 0;this.toolStrip1.Text = "toolStrip1";// // toolStripButton1// this.toolStripButton1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {this.加入页头ToolStripMenuItem,this.加入页脚ToolStripMenuItem});this.toolStripButton1.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton1.Image")));this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta;this.toolStripButton1.Name = "toolStripButton1";this.toolStripButton1.Size = new System.Drawing.Size(112, 34);this.toolStripButton1.Text = "文本框";// // 加入页头ToolStripMenuItem// this.加入页头ToolStripMenuItem.Name = "加入页头ToolStripMenuItem";this.加入页头ToolStripMenuItem.Size = new System.Drawing.Size(160, 30);this.加入页头ToolStripMenuItem.Text = "加入页头";this.加入页头ToolStripMenuItem.Click += new System.EventHandler(this.加入页头ToolStripMenuItem_Click_1);// // 加入页脚ToolStripMenuItem// this.加入页脚ToolStripMenuItem.Name = "加入页脚ToolStripMenuItem";this.加入页脚ToolStripMenuItem.Size = new System.Drawing.Size(160, 30);this.加入页脚ToolStripMenuItem.Text = "加入页脚";this.加入页脚ToolStripMenuItem.Click += new System.EventHandler(this.加入页脚ToolStripMenuItem_Click);// // toolStripButton7// this.toolStripButton7.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton7.Image")));this.toolStripButton7.ImageTransparentColor = System.Drawing.Color.Magenta;this.toolStripButton7.Name = "toolStripButton7";this.toolStripButton7.Size = new System.Drawing.Size(112, 34);this.toolStripButton7.Text = "表格 ";this.toolStripButton7.Click += new System.EventHandler(this.toolStripButton7_Click);// // toolStripLabel1// this.toolStripLabel1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {this.toolStripMenuItem1,this.页肢直线ToolStripMenuItem});this.toolStripLabel1.Name = "toolStripLabel1";this.toolStripLabel1.Size = new System.Drawing.Size(112, 29);this.toolStripLabel1.Text = "— 直线";this.toolStripLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;// // toolStripMenuItem1// this.toolStripMenuItem1.Name = "toolStripMenuItem1";this.toolStripMenuItem1.Size = new System.Drawing.Size(160, 30);this.toolStripMenuItem1.Text = "页头直线";this.toolStripMenuItem1.Click += new System.EventHandler(this.toolStripMenuItem1_Click);// // 页肢直线ToolStripMenuItem// this.页肢直线ToolStripMenuItem.Name = "页肢直线ToolStripMenuItem";this.页肢直线ToolStripMenuItem.Size = new System.Drawing.Size(160, 30);this.页肢直线ToolStripMenuItem.Text = "页脚直线";this.页肢直线ToolStripMenuItem.Click += new System.EventHandler(this.页肢直线ToolStripMenuItem_Click);// // toolStripSplitButton3// this.toolStripSplitButton3.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {this.插入页对ToolStripMenuItem,this.插入页脚ToolStripMenuItem1});this.toolStripSplitButton3.Image = ((System.Drawing.Image)(resources.GetObject("toolStripSplitButton3.Image")));this.toolStripSplitButton3.ImageTransparentColor = System.Drawing.Color.Magenta;this.toolStripSplitButton3.Name = "toolStripSplitButton3";this.toolStripSplitButton3.Size = new System.Drawing.Size(112, 34);this.toolStripSplitButton3.Text = "图形";// // 插入页对ToolStripMenuItem// this.插入页对ToolStripMenuItem.Name = "插入页对ToolStripMenuItem";this.插入页对ToolStripMenuItem.Size = new System.Drawing.Size(160, 30);this.插入页对ToolStripMenuItem.Text = "插入页头";this.插入页对ToolStripMenuItem.Click += new System.EventHandler(this.插入页对ToolStripMenuItem_Click);// // 插入页脚ToolStripMenuItem1// this.插入页脚ToolStripMenuItem1.Name = "插入页脚ToolStripMenuItem1";this.插入页脚ToolStripMenuItem1.Size = new System.Drawing.Size(160, 30);this.插入页脚ToolStripMenuItem1.Text = "插入页脚";this.插入页脚ToolStripMenuItem1.Click += new System.EventHandler(this.插入页脚ToolStripMenuItem1_Click);// // toolStripDropDownButton1// this.toolStripDropDownButton1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {this.插入页头ToolStripMenuItem1,this.插入页脚ToolStripMenuItem2});this.toolStripDropDownButton1.Image = global::WaterReport.Properties.Resources.加;this.toolStripDropDownButton1.ImageTransparentColor = System.Drawing.Color.Magenta;this.toolStripDropDownButton1.Name = "toolStripDropDownButton1";this.toolStripDropDownButton1.Size = new System.Drawing.Size(112, 34);this.toolStripDropDownButton1.Text = "条码";// // 插入页头ToolStripMenuItem1// this.插入页头ToolStripMenuItem1.Name = "插入页头ToolStripMenuItem1";this.插入页头ToolStripMenuItem1.Size = new System.Drawing.Size(160, 30);this.插入页头ToolStripMenuItem1.Text = "插入页头";this.插入页头ToolStripMenuItem1.Click += new System.EventHandler(this.插入页头ToolStripMenuItem1_Click);// // 插入页脚ToolStripMenuItem2// this.插入页脚ToolStripMenuItem2.Name = "插入页脚ToolStripMenuItem2";this.插入页脚ToolStripMenuItem2.Size = new System.Drawing.Size(160, 30);this.插入页脚ToolStripMenuItem2.Text = "插入页脚";this.插入页脚ToolStripMenuItem2.Click += new System.EventHandler(this.插入页脚ToolStripMenuItem2_Click);// // toolStripSeparator1// this.toolStripSeparator1.Name = "toolStripSeparator1";this.toolStripSeparator1.Size = new System.Drawing.Size(112, 6);// // toolStripSplitButton1// this.toolStripSplitButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;this.toolStripSplitButton1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {this.页脚ToolStripMenuItem,this.页头ToolStripMenuItem});this.toolStripSplitButton1.Image = ((System.Drawing.Image)(resources.GetObject("toolStripSplitButton1.Image")));this.toolStripSplitButton1.ImageTransparentColor = System.Drawing.Color.Magenta;this.toolStripSplitButton1.Name = "toolStripSplitButton1";this.toolStripSplitButton1.Size = new System.Drawing.Size(112, 29);this.toolStripSplitButton1.Text = "本页合计";// // 页脚ToolStripMenuItem// this.页脚ToolStripMenuItem.Name = "页脚ToolStripMenuItem";this.页脚ToolStripMenuItem.Size = new System.Drawing.Size(180, 30);this.页脚ToolStripMenuItem.Text = "页脚";this.页脚ToolStripMenuItem.Click += new System.EventHandler(this.页脚ToolStripMenuItem_Click);// // 页头ToolStripMenuItem// this.页头ToolStripMenuItem.Name = "页头ToolStripMenuItem";this.页头ToolStripMenuItem.Size = new System.Drawing.Size(180, 30);this.页头ToolStripMenuItem.Text = "页头";this.页头ToolStripMenuItem.Click += new System.EventHandler(this.页头ToolStripMenuItem_Click);// // toolStripSplitButton2// this.toolStripSplitButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;this.toolStripSplitButton2.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {this.插入页脚ToolStripMenuItem,this.插入页头ToolStripMenuItem});this.toolStripSplitButton2.Image = ((System.Drawing.Image)(resources.GetObject("toolStripSplitButton2.Image")));this.toolStripSplitButton2.ImageTransparentColor = System.Drawing.Color.Magenta;this.toolStripSplitButton2.Name = "toolStripSplitButton2";this.toolStripSplitButton2.Size = new System.Drawing.Size(112, 29);this.toolStripSplitButton2.Text = "页码";// // 插入页脚ToolStripMenuItem// this.插入页脚ToolStripMenuItem.Name = "插入页脚ToolStripMenuItem";this.插入页脚ToolStripMenuItem.Size = new System.Drawing.Size(160, 30);this.插入页脚ToolStripMenuItem.Text = "插入页脚";this.插入页脚ToolStripMenuItem.Click += new System.EventHandler(this.插入页脚ToolStripMenuItem_Click);// // 插入页头ToolStripMenuItem// this.插入页头ToolStripMenuItem.Name = "插入页头ToolStripMenuItem";this.插入页头ToolStripMenuItem.Size = new System.Drawing.Size(160, 30);this.插入页头ToolStripMenuItem.Text = "插入页头";this.插入页头ToolStripMenuItem.Click += new System.EventHandler(this.插入页头ToolStripMenuItem_Click);// // pptGrid// this.pptGrid.Dock = System.Windows.Forms.DockStyle.Right;this.pptGrid.Location = new System.Drawing.Point(920, 47);this.pptGrid.Margin = new System.Windows.Forms.Padding(4);this.pptGrid.Name = "pptGrid";this.pptGrid.Size = new System.Drawing.Size(250, 484);this.pptGrid.TabIndex = 4;this.pptGrid.PropertyValueChanged += new System.Windows.Forms.PropertyValueChangedEventHandler(this.PptGrid_PropertyValueChanged);// // splitter3// this.splitter3.Dock = System.Windows.Forms.DockStyle.Right;this.splitter3.Location = new System.Drawing.Point(916, 47);this.splitter3.Margin = new System.Windows.Forms.Padding(4);this.splitter3.Name = "splitter3";this.splitter3.Size = new System.Drawing.Size(4, 484);this.splitter3.TabIndex = 5;this.splitter3.TabStop = false;// // toolStrip2// this.toolStrip2.Font = new System.Drawing.Font("微软雅黑", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.toolStrip2.ImageScalingSize = new System.Drawing.Size(40, 40);this.toolStrip2.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {this.toolStripButton4,this.toolStripButton2,this.toolStripButton3,this.toolStripButton5,this.toolStripButton6});this.toolStrip2.Location = new System.Drawing.Point(0, 0);this.toolStrip2.Name = "toolStrip2";this.toolStrip2.Size = new System.Drawing.Size(1170, 47);this.toolStrip2.TabIndex = 6;this.toolStrip2.Text = "toolStrip2";// // toolStripButton4// this.toolStripButton4.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton4.Image")));this.toolStripButton4.ImageTransparentColor = System.Drawing.Color.Magenta;this.toolStripButton4.Name = "toolStripButton4";this.toolStripButton4.Size = new System.Drawing.Size(98, 44);this.toolStripButton4.Text = "新建";this.toolStripButton4.Click += new System.EventHandler(this.toolStripButton4_Click);// // toolStripButton2// this.toolStripButton2.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton2.Image")));this.toolStripButton2.ImageTransparentColor = System.Drawing.Color.Magenta;this.toolStripButton2.Name = "toolStripButton2";this.toolStripButton2.Size = new System.Drawing.Size(98, 44);this.toolStripButton2.Text = "打开";this.toolStripButton2.Click += new System.EventHandler(this.toolStripButton2_Click);// // toolStripButton3// this.toolStripButton3.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton3.Image")));this.toolStripButton3.ImageTransparentColor = System.Drawing.Color.Magenta;this.toolStripButton3.Name = "toolStripButton3";this.toolStripButton3.Size = new System.Drawing.Size(98, 44);this.toolStripButton3.Text = "保存";this.toolStripButton3.Click += new System.EventHandler(this.toolStripButton3_Click);// // toolStripButton5// this.toolStripButton5.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;this.toolStripButton5.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton5.Image")));this.toolStripButton5.ImageTransparentColor = System.Drawing.Color.Magenta;this.toolStripButton5.Name = "toolStripButton5";this.toolStripButton5.Size = new System.Drawing.Size(98, 44);this.toolStripButton5.Text = "关闭";this.toolStripButton5.Click += new System.EventHandler(this.toolStripButton5_Click);// // toolStripButton6// this.toolStripButton6.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton6.Image")));this.toolStripButton6.ImageTransparentColor = System.Drawing.Color.Magenta;this.toolStripButton6.Name = "toolStripButton6";this.toolStripButton6.Size = new System.Drawing.Size(98, 44);this.toolStripButton6.Text = "预览";this.toolStripButton6.Click += new System.EventHandler(this.toolStripButton6_Click);// // printDocument// this.printDocument.OriginAtMargins = true;this.printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument_PrintPage);// // pnlPage// this.pnlPage.Controls.Add(this.panel1);this.pnlPage.Controls.Add(this.splitter1);this.pnlPage.Dock = System.Windows.Forms.DockStyle.Fill;this.pnlPage.Location = new System.Drawing.Point(115, 47);this.pnlPage.Margin = new System.Windows.Forms.Padding(4);this.pnlPage.Name = "pnlPage";this.pnlPage.Size = new System.Drawing.Size(801, 484);this.pnlPage.TabIndex = 7;// // panel1// this.panel1.Controls.Add(this.grbData);this.panel1.Controls.Add(this.splitter4);this.panel1.Controls.Add(this.splitter2);this.panel1.Controls.Add(this.grbFooter);this.panel1.Controls.Add(this.grbHeader);this.panel1.Controls.Add(this.button1);this.panel1.Dock = System.Windows.Forms.DockStyle.Left;this.panel1.Location = new System.Drawing.Point(0, 4);this.panel1.Margin = new System.Windows.Forms.Padding(4);this.panel1.Name = "panel1";this.panel1.Size = new System.Drawing.Size(486, 480);this.panel1.TabIndex = 13;// // grbData// this.grbData.BackColor = System.Drawing.SystemColors.Control;this.grbData.Controls.Add(this.pnlData);this.grbData.Dock = System.Windows.Forms.DockStyle.Fill;this.grbData.Location = new System.Drawing.Point(0, 190);this.grbData.Margin = new System.Windows.Forms.Padding(4);this.grbData.Name = "grbData";this.grbData.Padding = new System.Windows.Forms.Padding(4);this.grbData.Size = new System.Drawing.Size(486, 130);this.grbData.TabIndex = 9;this.grbData.TabStop = false;this.grbData.Text = "数据";// // pnlData// this.pnlData.BackColor = System.Drawing.Color.White;this.pnlData.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;this.pnlData.Dock = System.Windows.Forms.DockStyle.Fill;this.pnlData.Location = new System.Drawing.Point(4, 26);this.pnlData.Margin = new System.Windows.Forms.Padding(4);this.pnlData.Name = "pnlData";this.pnlData.Size = new System.Drawing.Size(478, 100);this.pnlData.TabIndex = 0;// // splitter4// this.splitter4.Dock = System.Windows.Forms.DockStyle.Bottom;this.splitter4.Location = new System.Drawing.Point(0, 320);this.splitter4.Name = "splitter4";this.splitter4.Size = new System.Drawing.Size(486, 3);this.splitter4.TabIndex = 14;this.splitter4.TabStop = false;// // splitter2// this.splitter2.Dock = System.Windows.Forms.DockStyle.Top;this.splitter2.Location = new System.Drawing.Point(0, 187);this.splitter2.Name = "splitter2";this.splitter2.Size = new System.Drawing.Size(486, 3);this.splitter2.TabIndex = 13;this.splitter2.TabStop = false;// // grbFooter// this.grbFooter.BackColor = System.Drawing.SystemColors.Control;this.grbFooter.Controls.Add(this.pnlFooter);this.grbFooter.Dock = System.Windows.Forms.DockStyle.Bottom;this.grbFooter.Location = new System.Drawing.Point(0, 323);this.grbFooter.Margin = new System.Windows.Forms.Padding(4);this.grbFooter.Name = "grbFooter";this.grbFooter.Padding = new System.Windows.Forms.Padding(4);this.grbFooter.Size = new System.Drawing.Size(486, 157);this.grbFooter.TabIndex = 8;this.grbFooter.TabStop = false;this.grbFooter.Text = "页脚";// // pnlFooter// this.pnlFooter.BackColor = System.Drawing.Color.White;this.pnlFooter.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;this.pnlFooter.Dock = System.Windows.Forms.DockStyle.Fill;this.pnlFooter.Location = new System.Drawing.Point(4, 26);this.pnlFooter.Margin = new System.Windows.Forms.Padding(4);this.pnlFooter.Name = "pnlFooter";this.pnlFooter.Size = new System.Drawing.Size(478, 127);this.pnlFooter.TabIndex = 0;this.pnlFooter.ControlAdded += new System.Windows.Forms.ControlEventHandler(this.PnlFooter_ControlAdded);this.pnlFooter.ControlRemoved += new System.Windows.Forms.ControlEventHandler(this.PnlFooter_ControlRemoved);// // grbHeader// this.grbHeader.BackColor = System.Drawing.SystemColors.Control;this.grbHeader.Controls.Add(this.pnlHeader);this.grbHeader.Dock = System.Windows.Forms.DockStyle.Top;this.grbHeader.Location = new System.Drawing.Point(0, 28);this.grbHeader.Margin = new System.Windows.Forms.Padding(4);this.grbHeader.Name = "grbHeader";this.grbHeader.Padding = new System.Windows.Forms.Padding(4);this.grbHeader.Size = new System.Drawing.Size(486, 159);this.grbHeader.TabIndex = 7;this.grbHeader.TabStop = false;this.grbHeader.Text = "页头";// // pnlHeader// this.pnlHeader.AutoScroll = true;this.pnlHeader.BackColor = System.Drawing.Color.White;this.pnlHeader.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;this.pnlHeader.Dock = System.Windows.Forms.DockStyle.Fill;this.pnlHeader.Location = new System.Drawing.Point(4, 26);this.pnlHeader.Margin = new System.Windows.Forms.Padding(4);this.pnlHeader.Name = "pnlHeader";this.pnlHeader.Size = new System.Drawing.Size(478, 129);this.pnlHeader.TabIndex = 0;this.pnlHeader.ControlAdded += new System.Windows.Forms.ControlEventHandler(this.PnlHeader_ControlAdded);this.pnlHeader.ControlRemoved += new System.Windows.Forms.ControlEventHandler(this.PnlHeader_ControlRemoved);// // button1// this.button1.BackColor = System.Drawing.SystemColors.ControlDark;this.button1.Dock = System.Windows.Forms.DockStyle.Top;this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;this.button1.Location = new System.Drawing.Point(0, 0);this.button1.Margin = new System.Windows.Forms.Padding(4);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(486, 28);this.button1.TabIndex = 12;this.button1.Text = "页面设置";this.button1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;this.button1.UseVisualStyleBackColor = false;this.button1.Click += new System.EventHandler(this.button1_Click);// // splitter1// this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;this.splitter1.Location = new System.Drawing.Point(0, 0);this.splitter1.Margin = new System.Windows.Forms.Padding(4);this.splitter1.Name = "splitter1";this.splitter1.Size = new System.Drawing.Size(801, 4);this.splitter1.TabIndex = 10;this.splitter1.TabStop = false;// // printPreviewDialog// this.printPreviewDialog.AutoScrollMargin = new System.Drawing.Size(0, 0);this.printPreviewDialog.AutoScrollMinSize = new System.Drawing.Size(0, 0);this.printPreviewDialog.ClientSize = new System.Drawing.Size(400, 300);this.printPreviewDialog.Enabled = true;this.printPreviewDialog.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog.Icon")));this.printPreviewDialog.Name = "printPreviewDialog";this.printPreviewDialog.Visible = false;// // saveFileDialog// this.saveFileDialog.Filter = "报表文件(*.wrpt)|*.wrpt";// // printDialog1// this.printDialog1.UseEXDialog = true;// // panel2// this.panel2.Controls.Add(this.groupBox2);this.panel2.Controls.Add(this.splitter5);this.panel2.Controls.Add(this.groupBox1);this.panel2.Dock = System.Windows.Forms.DockStyle.Right;this.panel2.Location = new System.Drawing.Point(716, 47);this.panel2.Name = "panel2";this.panel2.Size = new System.Drawing.Size(200, 484);this.panel2.TabIndex = 8;// // groupBox2// this.groupBox2.Controls.Add(this.lstFooter);this.groupBox2.Dock = System.Windows.Forms.DockStyle.Fill;this.groupBox2.Location = new System.Drawing.Point(0, 299);this.groupBox2.Name = "groupBox2";this.groupBox2.Size = new System.Drawing.Size(200, 185);this.groupBox2.TabIndex = 2;this.groupBox2.TabStop = false;this.groupBox2.Text = "页脚元素";// // lstFooter// this.lstFooter.Dock = System.Windows.Forms.DockStyle.Fill;this.lstFooter.FormattingEnabled = true;this.lstFooter.ItemHeight = 19;this.lstFooter.Location = new System.Drawing.Point(3, 25);this.lstFooter.Name = "lstFooter";this.lstFooter.Size = new System.Drawing.Size(194, 157);this.lstFooter.TabIndex = 0;this.lstFooter.SelectedIndexChanged += new System.EventHandler(this.LstFooter_SelectedIndexChanged);// // splitter5// this.splitter5.Dock = System.Windows.Forms.DockStyle.Top;this.splitter5.Location = new System.Drawing.Point(0, 296);this.splitter5.Name = "splitter5";this.splitter5.Size = new System.Drawing.Size(200, 3);this.splitter5.TabIndex = 1;this.splitter5.TabStop = false;// // groupBox1// this.groupBox1.Controls.Add(this.lstHeader);this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;this.groupBox1.Location = new System.Drawing.Point(0, 0);this.groupBox1.Name = "groupBox1";this.groupBox1.Size = new System.Drawing.Size(200, 296);this.groupBox1.TabIndex = 0;this.groupBox1.TabStop = false;this.groupBox1.Text = "页头元素";// // lstHeader// this.lstHeader.Dock = System.Windows.Forms.DockStyle.Fill;this.lstHeader.FormattingEnabled = true;this.lstHeader.ItemHeight = 19;this.lstHeader.Location = new System.Drawing.Point(3, 25);this.lstHeader.Name = "lstHeader";this.lstHeader.Size = new System.Drawing.Size(194, 268);this.lstHeader.TabIndex = 0;this.lstHeader.SelectedIndexChanged += new System.EventHandler(this.LstHeader_SelectedIndexChanged);// // splitter6// this.splitter6.Dock = System.Windows.Forms.DockStyle.Right;this.splitter6.Location = new System.Drawing.Point(713, 47);this.splitter6.Name = "splitter6";this.splitter6.Size = new System.Drawing.Size(3, 484);this.splitter6.TabIndex = 9;this.splitter6.TabStop = false;// // toolStripSplitButton4// this.toolStripSplitButton4.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;this.toolStripSplitButton4.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {this.页脚ToolStripMenuItem1,this.页头ToolStripMenuItem1});this.toolStripSplitButton4.Image = ((System.Drawing.Image)(resources.GetObject("toolStripSplitButton4.Image")));this.toolStripSplitButton4.ImageTransparentColor = System.Drawing.Color.Magenta;this.toolStripSplitButton4.Name = "toolStripSplitButton4";this.toolStripSplitButton4.Size = new System.Drawing.Size(112, 29);this.toolStripSplitButton4.Text = "总合计";// // 页脚ToolStripMenuItem1// this.页脚ToolStripMenuItem1.Name = "页脚ToolStripMenuItem1";this.页脚ToolStripMenuItem1.Size = new System.Drawing.Size(180, 30);this.页脚ToolStripMenuItem1.Text = "页脚";this.页脚ToolStripMenuItem1.Click += new System.EventHandler(this.页脚ToolStripMenuItem1_Click);// // 页头ToolStripMenuItem1// this.页头ToolStripMenuItem1.Name = "页头ToolStripMenuItem1";this.页头ToolStripMenuItem1.Size = new System.Drawing.Size(180, 30);this.页头ToolStripMenuItem1.Text = "页头";this.页头ToolStripMenuItem1.Click += new System.EventHandler(this.页头ToolStripMenuItem1_Click);// // frmWaterReportEdit// this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 19F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.BackColor = System.Drawing.SystemColors.Control;this.ClientSize = new System.Drawing.Size(1170, 531);this.Controls.Add(this.splitter6);this.Controls.Add(this.panel2);this.Controls.Add(this.pnlPage);this.Controls.Add(this.splitter3);this.Controls.Add(this.pptGrid);this.Controls.Add(this.toolStrip1);this.Controls.Add(this.toolStrip2);this.DoubleBuffered = true;this.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);this.Name = "frmWaterReportEdit";this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;this.Text = "Water报表编辑器";this.WindowState = System.Windows.Forms.FormWindowState.Maximized;this.Load += new System.EventHandler(this.frmWaterReportEdit_Load);this.toolStrip1.ResumeLayout(false);this.toolStrip1.PerformLayout();this.toolStrip2.ResumeLayout(false);this.toolStrip2.PerformLayout();this.pnlPage.ResumeLayout(false);this.panel1.ResumeLayout(false);this.grbData.ResumeLayout(false);this.grbFooter.ResumeLayout(false);this.grbHeader.ResumeLayout(false);this.panel2.ResumeLayout(false);this.groupBox2.ResumeLayout(false);this.groupBox1.ResumeLayout(false);this.ResumeLayout(false);this.PerformLayout();}#endregionprivate System.Windows.Forms.ToolStrip toolStrip1;private System.Windows.Forms.ToolStripDropDownButton toolStripButton1;private System.Windows.Forms.ToolStripMenuItem 加入页头ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 加入页脚ToolStripMenuItem;private System.Windows.Forms.PropertyGrid pptGrid;private System.Windows.Forms.Splitter splitter3;private System.Windows.Forms.ToolStrip toolStrip2;private System.Windows.Forms.ToolStripButton toolStripButton2;private System.Windows.Forms.ToolStripButton toolStripButton3;private System.Windows.Forms.ToolStripButton toolStripButton4;private System.Windows.Forms.ToolStripButton toolStripButton5;private System.Windows.Forms.ToolStripButton toolStripButton6;private System.Windows.Forms.Panel pnlPage;private System.Windows.Forms.Splitter splitter1;private System.Windows.Forms.GroupBox grbData;private System.Windows.Forms.GroupBox grbFooter;private System.Windows.Forms.GroupBox grbHeader;private System.Windows.Forms.Button button1;private System.Windows.Forms.PrintPreviewDialog printPreviewDialog;private System.Windows.Forms.SaveFileDialog saveFileDialog;private System.Windows.Forms.OpenFileDialog openFileDialog;private System.Windows.Forms.ToolStripButton toolStripButton7;public System.Drawing.Printing.PrintDocument printDocument;private System.Windows.Forms.Panel panel1;private System.Windows.Forms.ToolStripDropDownButton toolStripLabel1;private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;private System.Windows.Forms.ToolStripMenuItem 页肢直线ToolStripMenuItem;private System.Windows.Forms.ToolStripDropDownButton toolStripSplitButton1;private System.Windows.Forms.ToolStripMenuItem 页脚ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 页头ToolStripMenuItem;private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;private System.Windows.Forms.ToolStripDropDownButton toolStripSplitButton2;private System.Windows.Forms.ToolStripMenuItem 插入页脚ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 插入页头ToolStripMenuItem;public System.Windows.Forms.Panel pnlHeader;public System.Windows.Forms.Panel pnlData;public System.Windows.Forms.Panel pnlFooter;private System.Windows.Forms.Splitter splitter4;private System.Windows.Forms.Splitter splitter2;private System.Windows.Forms.ToolStripDropDownButton toolStripSplitButton3;private System.Windows.Forms.ToolStripMenuItem 插入页对ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 插入页脚ToolStripMenuItem1;private System.Windows.Forms.ToolStripDropDownButton toolStripDropDownButton1;private System.Windows.Forms.ToolStripMenuItem 插入页头ToolStripMenuItem1;private System.Windows.Forms.ToolStripMenuItem 插入页脚ToolStripMenuItem2;public System.Windows.Forms.PrintDialog printDialog1;private System.Windows.Forms.Panel panel2;private System.Windows.Forms.GroupBox groupBox2;private System.Windows.Forms.Splitter splitter5;private System.Windows.Forms.GroupBox groupBox1;private System.Windows.Forms.Splitter splitter6;private System.Windows.Forms.ListBox lstFooter;private System.Windows.Forms.ListBox lstHeader;private System.Windows.Forms.ToolStripDropDownButton toolStripSplitButton4;private System.Windows.Forms.ToolStripMenuItem 页脚ToolStripMenuItem1;private System.Windows.Forms.ToolStripMenuItem 页头ToolStripMenuItem1;}
}
事件代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing.Printing;
using WaterClassLibrary;///打印图片,为打印二维码、条码做准备
///打印线条
///文本框参数传递
///表头合并namespace WaterReport
{public partial class frmWaterReportEdit : Form{/// <summary>/// 包含文件名的保存路径/// </summary>public string strSavePathWithName;/// <summary>/// 要打印的表格数据/// </summary>public DataTable dtData;/// <summary>/// 打开指定报表,以便设计/// </summary>/// <param name="path">报表所在路径及名称</param>public frmWaterReportEdit(string path){InitializeComponent();strSavePathWithName = path;OpenWRPT();}/// <summary>/// 实例报表/// </summary>/// <param name="path">报表所在路径及名称</param>/// <param name="d">要打印的表</param>/// <param name="pw">要打印的参数</param>public frmWaterReportEdit(string path, DataTable d, Cls_PrintWRPT.ParameterWRPT[] pw){InitializeComponent(); strSavePathWithName = path;OpenWRPT();#region 设置参数dtData = d;if (pw != null){foreach (Control c in pnlHeader.Controls){if (c is Cls_rptLabel || c is Cls_rptCode128A){for (int i = 0; i < pw.Length; i++){if (c.Name == pw[i].Name){c.Text = pw[i].value;}}}}foreach (Control c in pnlFooter.Controls){if (c is Cls_rptLabel || c is Cls_rptCode128A){for (int i = 0; i < pw.Length; i++){if (c.Name == pw[i].Name){c.Text = pw[i].value;}}}}}#endregion}public frmWaterReportEdit(){InitializeComponent();strSavePathWithName = "";printDocument.DefaultPageSettings.PaperSize =new PaperSize("Custom", 827,1169);headerAndFooter = new Cls_HeaderAndFooter();} #region 控件移动及调整大小(网络代码)private enum EnumMousePointPosition{MouseSizeNone = 0, //'无 MouseSizeRight = 1, //'拉伸右边框 MouseSizeLeft = 2, //'拉伸左边框 MouseSizeBottom = 3, //'拉伸下边框 MouseSizeTop = 4, //'拉伸上边框 MouseSizeTopLeft = 5, //'拉伸左上角 MouseSizeTopRight = 6, //'拉伸右上角 MouseSizeBottomLeft = 7, //'拉伸左下角 MouseSizeBottomRight = 8, //'拉伸右下角 MouseDrag = 9 // '鼠标拖动 }const int Band = 5;const int MinWidth = 10;const int MinHeight = 10;private EnumMousePointPosition m_MousePointPosition;private Point p, p1;private void MyMouseDown(object sender, System.Windows.Forms.MouseEventArgs e){p.X = e.X;p.Y = e.Y;p1.X = e.X;p1.Y = e.Y;}private void MyMouseLeave(object sender, System.EventArgs e){m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;this.Cursor = Cursors.Arrow;}private EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e){if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height)){if (e.X < Band){if (e.Y < Band) { return EnumMousePointPosition.MouseSizeTopLeft; }else{if (e.Y > -1 * Band + size.Height){ return EnumMousePointPosition.MouseSizeBottomLeft; }else{ return EnumMousePointPosition.MouseSizeLeft; }}}else{if (e.X > -1 * Band + size.Width){if (e.Y < Band){ return EnumMousePointPosition.MouseSizeTopRight; }else{if (e.Y > -1 * Band + size.Height){ return EnumMousePointPosition.MouseSizeBottomRight; }else{ return EnumMousePointPosition.MouseSizeRight; }}}else{if (e.Y < Band){ return EnumMousePointPosition.MouseSizeTop; }else{if (e.Y > -1 * Band + size.Height){ return EnumMousePointPosition.MouseSizeBottom; }else{ return EnumMousePointPosition.MouseDrag; }}}}}else{ return EnumMousePointPosition.MouseSizeNone; }}private void MyMouseMove(object sender, System.Windows.Forms.MouseEventArgs e){Control lCtrl = (sender as Control);if (e.Button == MouseButtons.Left){switch (m_MousePointPosition){case EnumMousePointPosition.MouseDrag:lCtrl.Left = lCtrl.Left + e.X - p.X;lCtrl.Top = lCtrl.Top + e.Y - p.Y;break;case EnumMousePointPosition.MouseSizeBottom:lCtrl.Height = lCtrl.Height + e.Y - p1.Y;p1.X = e.X;p1.Y = e.Y; //'记录光标拖动的当前点 break;case EnumMousePointPosition.MouseSizeBottomRight:lCtrl.Width = lCtrl.Width + e.X - p1.X;lCtrl.Height = lCtrl.Height + e.Y - p1.Y;p1.X = e.X;p1.Y = e.Y; //'记录光标拖动的当前点 break;case EnumMousePointPosition.MouseSizeRight:lCtrl.Width = lCtrl.Width + e.X - p1.X;// lCtrl.Height = lCtrl.Height + e.Y - p1.Y; p1.X = e.X;p1.Y = e.Y; //'记录光标拖动的当前点 break;case EnumMousePointPosition.MouseSizeTop:lCtrl.Top = lCtrl.Top + (e.Y - p.Y);lCtrl.Height = lCtrl.Height - (e.Y - p.Y);break;case EnumMousePointPosition.MouseSizeLeft:lCtrl.Left = lCtrl.Left + e.X - p.X;lCtrl.Width = lCtrl.Width - (e.X - p.X);break;case EnumMousePointPosition.MouseSizeBottomLeft:lCtrl.Left = lCtrl.Left + e.X - p.X;lCtrl.Width = lCtrl.Width - (e.X - p.X);lCtrl.Height = lCtrl.Height + e.Y - p1.Y;p1.X = e.X;p1.Y = e.Y; //'记录光标拖动的当前点 break;case EnumMousePointPosition.MouseSizeTopRight:lCtrl.Top = lCtrl.Top + (e.Y - p.Y);lCtrl.Width = lCtrl.Width + (e.X - p1.X);lCtrl.Height = lCtrl.Height - (e.Y - p.Y);p1.X = e.X;p1.Y = e.Y; //'记录光标拖动的当前点 break;case EnumMousePointPosition.MouseSizeTopLeft:lCtrl.Left = lCtrl.Left + e.X - p.X;lCtrl.Top = lCtrl.Top + (e.Y - p.Y);lCtrl.Width = lCtrl.Width - (e.X - p.X);lCtrl.Height = lCtrl.Height - (e.Y - p.Y);break;default:break;}if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth;if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight;}else{m_MousePointPosition = MousePointPosition(lCtrl.Size, e); //'判断光标的位置状态 switch (m_MousePointPosition) //'改变光标 {case EnumMousePointPosition.MouseSizeNone:this.Cursor = Cursors.Arrow; //'箭头 break;case EnumMousePointPosition.MouseDrag:this.Cursor = Cursors.SizeAll; //'四方向 break;case EnumMousePointPosition.MouseSizeBottom:this.Cursor = Cursors.SizeNS; //'南北 break;case EnumMousePointPosition.MouseSizeTop:this.Cursor = Cursors.SizeNS; //'南北 break;case EnumMousePointPosition.MouseSizeLeft:this.Cursor = Cursors.SizeWE; //'东西 break;case EnumMousePointPosition.MouseSizeRight:this.Cursor = Cursors.SizeWE; //'东西 break;case EnumMousePointPosition.MouseSizeBottomLeft:this.Cursor = Cursors.SizeNESW; //'东北到南西 break;case EnumMousePointPosition.MouseSizeBottomRight:this.Cursor = Cursors.SizeNWSE; //'东南到西北 break;case EnumMousePointPosition.MouseSizeTopLeft:this.Cursor = Cursors.SizeNWSE; //'东南到西北 break;case EnumMousePointPosition.MouseSizeTopRight:this.Cursor = Cursors.SizeNESW; //'东北到南西 break;default:break;}}}public void MyMouseUp(object sender, MouseEventArgs e){Control crl = (sender as Control);NoBackColor(pnlPage);button1.BackColor = SystemColors.ControlDark;crl.BackColor = SystemColors.ActiveCaption;crl.BringToFront();pnlHeader.BackColor = Color.White;pnlData.BackColor = Color.White;pnlFooter.BackColor = Color.White;pnlPage.BackColor = Color.White;//pnlAfterData.BackColor = Color.White;//显示控件属性pptGrid.SelectedObject = crl.Tag;//PageRefresh();}void NoBackColor(Control c){c.BackColor = SystemColors.Control;foreach(Control cc in c.Controls){NoBackColor(cc);}}/// <summary>/// 添加控件统一且必须的鼠标事件/// </summary>/// <param name="c"></param>public void AddEditControl(Control c){c.MouseDown += new MouseEventHandler(MyMouseDown);c.MouseLeave += new EventHandler(MyMouseLeave);c.MouseMove += new MouseEventHandler(MyMouseMove);c.MouseUp += new MouseEventHandler(MyMouseUp);}#endregion private void 加入页头ToolStripMenuItem_Click_1(object sender, EventArgs e){Cls_rptLabel cLabel = new Cls_rptLabel();AddEditControl(cLabel); pnlHeader.Controls.Add(cLabel);MyMouseUp(cLabel, null); }private void toolStripButton3_Click(object sender, EventArgs e){//保存报表 #region 保存前必须将所有滚动条滚动到0点位置// 在设置滚动条为0前保存各滚动条的位置,以便在保存后恢复int pnlHeaderVer = pnlHeader.VerticalScroll.Value;int pnlHeaderHor = pnlHeader.HorizontalScroll.Value;int pnlDataVer = pnlData.VerticalScroll.Value;int pnlDataHor = pnlData.HorizontalScroll.Value;int pnlFooterVer = pnlFooter.VerticalScroll.Value;int pnlFooterHor = pnlFooter.HorizontalScroll.Value;//pnlHeader.PerformLayout();//pnlData.PerformLayout();//pnlFooter.PerformLayout();// 因为滚动条不在0点时,各控件取到的位置信息将会出现偏移pnlHeader.VerticalScroll.Value = 0;pnlHeader.HorizontalScroll.Value = 0;pnlData.VerticalScroll.Value = 0;pnlData.HorizontalScroll.Value = 0;pnlFooter.VerticalScroll.Value = 0;pnlFooter.HorizontalScroll.Value = 0;#endregionif (strSavePathWithName == ""){DialogResult dr = saveFileDialog.ShowDialog();if (dr == DialogResult.OK){strSavePathWithName = saveFileDialog.FileName;}else{return;}}#region 页面设置信息保存//page\tstring strPageInfo = "page"+ "\tPaperSize\a"+ printDocument.DefaultPageSettings.PaperSize.Width.ToString()+ ","+ printDocument.DefaultPageSettings.PaperSize.Height.ToString()+ "\tMarginsBottom\a"+ printDocument.DefaultPageSettings.Margins.Bottom.ToString()+ "\tMarginsLeft\a"+ printDocument.DefaultPageSettings.Margins.Left.ToString()+ "\tMarginsRight\a"+ printDocument.DefaultPageSettings.Margins.Right.ToString()+ "\tMarginsTop\a"+ printDocument.DefaultPageSettings.Margins.Top.ToString()+ "\tLandscape\a"+ printDocument.DefaultPageSettings.Landscape.ToString()+ "\tHeaderHeight\a"+ headerAndFooter.HeaderHeight.ToString() //grbHeader.Height.ToString()+ "\tFooterHeigth\a"+ headerAndFooter.FooterHeight; //grbFooter.Height.ToString();#endregion#region 页头信息保存 foreach (Control cObject in pnlHeader.Controls){if (cObject is Cls_rptLabel){strPageInfo += ((Cls_rptLabel)cObject).GetSaveString("Header");}else if (cObject is Cls_rptDrawLineHV){strPageInfo += ((Cls_rptDrawLineHV)cObject).GetSaveString("Header");}else if (cObject is Cls_rptSumPageData){strPageInfo += ((Cls_rptSumPageData)cObject).GetSaveString("Header");}else if (cObject is Cls_rptPageNumber){strPageInfo += ((Cls_rptPageNumber)cObject).GetSaveString("Header");}else if (cObject is Cls_rptImage){strPageInfo += ((Cls_rptImage)cObject).GetSaveString("Header");}else if (cObject is Cls_rptCode128A){strPageInfo += ((Cls_rptCode128A)cObject).GetSaveString("Header");}else if (cObject is Cls_rptSumAllData){strPageInfo += ((Cls_rptSumAllData)cObject).GetSaveString("Header");}}#endregion#region 页脚信息保存foreach (Control cObject in pnlFooter.Controls){if (cObject is Cls_rptLabel){strPageInfo += ((Cls_rptLabel)cObject).GetSaveString("Footer");}else if (cObject is Cls_rptDrawLineHV){strPageInfo += ((Cls_rptDrawLineHV)cObject).GetSaveString("Footer");}else if (cObject is Cls_rptSumPageData){strPageInfo += ((Cls_rptSumPageData)cObject).GetSaveString("Footer");}else if (cObject is Cls_rptPageNumber){strPageInfo += ((Cls_rptPageNumber)cObject).GetSaveString("Footer");}else if (cObject is Cls_rptImage){strPageInfo += ((Cls_rptImage)cObject).GetSaveString("Footer");}else if (cObject is Cls_rptCode128A){strPageInfo += ((Cls_rptCode128A)cObject).GetSaveString("Footer");}else if (cObject is Cls_rptSumAllData){strPageInfo += ((Cls_rptSumAllData)cObject).GetSaveString("Footer");}}#endregion#region 数据表信息保存Cls_rptTable tmprptTable;foreach (Control cObject in pnlData.Controls){if (cObject is Cls_rptTable){#region 保存数据表FontConverter fc = new FontConverter();tmprptTable = (Cls_rptTable)cObject;//先保存表格信息strPageInfo += "\rCls_rptTable\t"+ "Table"+ "\'Location\a" + tmprptTable.Location.X.ToString()+ "," + tmprptTable.Location.Y.ToString()+ "\'RowsHeight\a" + tmprptTable.ColumnHeadersHeight.ToString()+ "\'Font\a"+ fc.ConvertToInvariantString(tmprptTable.Font)+ "\'OnePageRowCount\a"+ tmprptTable.RegularPagePrintCount.ToString();//再保存列信息 for (int i = 0; i < tmprptTable.Columns.Count; i++){strPageInfo += "\t"+ "Column"+ "\'Name\a" + tmprptTable.Columns[i].Name+ "\'Width\a" + tmprptTable.Columns[i].Width+ "\'HeaderText\a" + tmprptTable.Columns[i].HeaderText+ "\'AutoFont\a" + (((Cls_rptTable.PrintColumn)tmprptTable.Columns[i]).bolAutoFont == true ? "TRUE" : "FALSE")+ "\'Alignment\a" + ((Cls_rptTable.PrintColumn)tmprptTable.Columns[i]).myAlignment.ToString();strPageInfo += "\'SumRowSet\a" + tmprptTable.Columns[i].ToolTipText;}#endregion}}#endregionbool b = Cls_DataOperater_SQL.TxtWrite(strSavePathWithName, strPageInfo);if (b){this.Text = "Water报表编辑器" + "[" + strSavePathWithName + "]";MessageBox.Show("报表已保存");}else{MessageBox.Show("报表保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);}#region 在保存后恢复各滚动条的位置if (pnlHeaderHor == 0){pnlHeader.HorizontalScroll.Value = 1;}if (pnlHeaderVer == 0){pnlHeader.VerticalScroll.Value = 1;}if (pnlDataHor == 0){pnlData.HorizontalScroll.Value = 1; }if (pnlDataVer == 0){pnlData.VerticalScroll.Value = 1;}if (pnlFooterHor == 0){pnlFooter.HorizontalScroll.Value = 1;}if (pnlFooterVer == 0){pnlFooter.VerticalScroll.Value = 1;}pnlHeader.VerticalScroll.Value = pnlHeaderVer; pnlHeader.HorizontalScroll.Value = pnlHeaderHor;pnlHeader.PerformLayout();pnlData.VerticalScroll.Value = pnlDataVer; pnlData.HorizontalScroll.Value = pnlDataHor;pnlData.PerformLayout();pnlFooter.VerticalScroll.Value = pnlFooterVer; pnlFooter.HorizontalScroll.Value = pnlFooterHor;pnlFooter.PerformLayout();#endregion}private void toolStripButton5_Click(object sender, EventArgs e){this.Close();}Cls_HeaderAndFooter headerAndFooter;/// <summary>/// 页头、页脚实际设置与显示的偏差值/// </summary> private void button1_Click(object sender, EventArgs e){//页面设置 pptGrid.SelectedObject = printDocument;//Cls_HeaderAndFooter ch = new Cls_HeaderAndFooter();//ch.HeaderHeight = grbHeader.Height;//ch.FooterHeight = grbFooter.Height;//ch.HeaderHeight = headerAndFooter.Height;//ch.FooterHeight = headerAndFooter.Height;//new frmPageSetting(printDocument,ch).ShowDialog();new frmPageSetting(printDocument, headerAndFooter).ShowDialog();//grbHeader.Height = ch.HeaderHeight;//grbFooter.Height = ch.FooterHeight; PageRefresh();}private void toolStripButton2_Click(object sender, EventArgs e){//打开DialogResult dr = openFileDialog.ShowDialog();if (dr == DialogResult.OK){toolStripButton4_Click(null, null);strSavePathWithName = openFileDialog.FileName;this.Text = "Water报表编辑器" + "[" + strSavePathWithName + "]";}else{return;}OpenWRPT();}private void OpenWRPT(){ //先获取数据字符串string[] rptData = Cls_DataOperater_SQL.GetTxtContent(strSavePathWithName);if (rptData == null) { MessageBox.Show("获取报表文件失败"); return; }headerAndFooter = new Cls_HeaderAndFooter();for (int i = 0; i < rptData.Length; i++){ SetRPTObject(rptData[i]);}PageRefresh();}#region 读取保存的报表项 private void SetRPTObject(string newString){if (newString != ""){if (newString.IndexOf("page") >= 0){#region 页面设置string[] rptItem = newString.Split('\t');for (int i = 1; i < rptItem.Length; i++){string[] item = rptItem[i].Split('\a');if (item[0] == "PaperSize"){string[] strSize = item[1].Split(',');printDocument.DefaultPageSettings.PaperSize =new PaperSize("Custom", Convert.ToInt32(strSize[0]), Convert.ToInt32(strSize[1]));}else if (item[0] == "MarginsBottom"){printDocument.DefaultPageSettings.Margins.Bottom =Convert.ToInt32(item[1]);}else if (item[0] == "MarginsLeft"){printDocument.DefaultPageSettings.Margins.Left =Convert.ToInt32(item[1]);}else if (item[0] == "MarginsRight"){printDocument.DefaultPageSettings.Margins.Right =Convert.ToInt32(item[1]);}else if (item[0] == "MarginsTop"){printDocument.DefaultPageSettings.Margins.Top =Convert.ToInt32(item[1]);}else if (item[0] == "Landscape"){printDocument.DefaultPageSettings.Landscape = Convert.ToBoolean(item[1]);}else if (item[0] == "HeaderHeight"){//grbHeader.Height = Convert.ToInt32(item[1]);headerAndFooter.HeaderHeight = Convert.ToInt32(item[1]);}else if (item[0] == "FooterHeigth"){//grbFooter.Height = Convert.ToInt32(item[1]);headerAndFooter.FooterHeight = Convert.ToInt32(item[1]);}}#endregion}else if (newString.IndexOf("Cls_rptLabel") >= 0){new Cls_rptLabel(newString, this);}else if (newString.IndexOf("Cls_rptTable") >= 0){new Cls_rptTable(newString, this);}else if (newString.IndexOf("Cls_rptDrawLineHV") >= 0){new Cls_rptDrawLineHV(newString, this);}else if (newString.IndexOf("Cls_rptSumPageData") >= 0){new Cls_rptSumPageData(newString, this);}else if (newString.IndexOf("Cls_rptPageNumber") >= 0){new Cls_rptPageNumber(newString, this);}else if (newString.IndexOf("Cls_rptImage") >= 0){new Cls_rptImage(newString, this);}else if (newString.IndexOf("Cls_rptCode128A") >= 0){new Cls_rptCode128A(newString, this);}else if (newString.IndexOf("Cls_rptSumAllData") >= 0){new Cls_rptSumAllData(newString, this);}}} #endregionprivate void toolStripButton6_Click(object sender, EventArgs e){#region 保存前必须将所有滚动条滚动到0点位置// 在设置滚动条为0前保存各滚动条的位置,以便在保存后恢复int pnlHeaderVer = pnlHeader.VerticalScroll.Value;int pnlHeaderHor = pnlHeader.HorizontalScroll.Value;int pnlDataVer = pnlData.VerticalScroll.Value;int pnlDataHor = pnlData.HorizontalScroll.Value;int pnlFooterVer = pnlFooter.VerticalScroll.Value;int pnlFooterHor = pnlFooter.HorizontalScroll.Value;//pnlHeader.PerformLayout();//pnlData.PerformLayout();//pnlFooter.PerformLayout();// 因为滚动条不在0点时,各控件取到的位置信息将会出现偏移pnlHeader.VerticalScroll.Value = 0;pnlHeader.HorizontalScroll.Value = 0;pnlData.VerticalScroll.Value = 0;pnlData.HorizontalScroll.Value = 0;pnlFooter.VerticalScroll.Value = 0;pnlFooter.HorizontalScroll.Value = 0;#endregionPrintPreview();#region 在保存后恢复各滚动条的位置if (pnlHeaderHor == 0){pnlHeader.HorizontalScroll.Value = 1;}if (pnlHeaderVer == 0){pnlHeader.VerticalScroll.Value = 1;}if (pnlDataHor == 0){pnlData.HorizontalScroll.Value = 1;}if (pnlDataVer == 0){pnlData.VerticalScroll.Value = 1;}if (pnlFooterHor == 0){pnlFooter.HorizontalScroll.Value = 1;}if (pnlFooterVer == 0){pnlFooter.VerticalScroll.Value = 1;}pnlHeader.VerticalScroll.Value = pnlHeaderVer; pnlHeader.HorizontalScroll.Value = pnlHeaderHor;pnlHeader.PerformLayout();pnlData.VerticalScroll.Value = pnlDataVer; pnlData.HorizontalScroll.Value = pnlDataHor;pnlData.PerformLayout();pnlFooter.VerticalScroll.Value = pnlFooterVer; pnlFooter.HorizontalScroll.Value = pnlFooterHor;pnlFooter.PerformLayout();#endregion}/// <summary>/// 打印预览/// 必须先使用 frmWaterReportEdit(string path, DataTable d, Cls_PrintWRPT.ParameterWRPT[] pw)/// 方法进行报表实例化,否则不能正确显示内容/// </summary>public void PrintPreview(){ if (dtData == null){dtData = new DataTable();}printPreviewDialog.Document = printDocument;Form f = (Form)printPreviewDialog;f.WindowState = FormWindowState.Maximized;printPreviewDialog.ShowDialog();}private void toolStripButton4_Click(object sender, EventArgs e){//新建printDocument = new PrintDocument();printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);strSavePathWithName = ""; pnlHeader.Controls.Clear();pnlData.Controls.Clear();pnlFooter.Controls.Clear(); pptGrid.SelectedObject = null;this.Text = "Water报表编辑器";}private void 加入页脚ToolStripMenuItem_Click(object sender, EventArgs e){Cls_rptLabel cLabel = new Cls_rptLabel();AddEditControl(cLabel);pnlFooter.Controls.Add(cLabel);MyMouseUp(cLabel, null); }Cls_rptTable.cls_CurrentPrintDataArea CurrentPrintDataArea;/// <summary>/// 当前打印页的页码/// </summary>public int CurrentPageNumber;/// <summary>/// 总页数/// </summary>public int AllPageNumber;private void printDocument_PrintPage(object sender, PrintPageEventArgs e){ Graphics g = e.Graphics;g.PageUnit = GraphicsUnit.Display; #region 打印中间表格foreach (Control c in pnlData.Controls){if (c is Cls_rptTable){ Cls_rptTable printDT=(Cls_rptTable)c; printDT.DrawMe(e, dtData, headerAndFooter.HeaderHeight, headerAndFooter.FooterHeight, -1);CurrentPrintDataArea = printDT.CurrentPrintDataArea;AllPageNumber = printDT.AllPageNumber;CurrentPageNumber++; }}if (AllPageNumber == 0){AllPageNumber = 1;e.HasMorePages = false;}#endregion#region 打印页头内容/// 执行图片优先打印原则/// 因为使用大图片时,一般是要设置背景图片,这时必须先打印图片,/// 否则在其后打印的所有元素都会被復盖foreach(Control c in pnlHeader.Controls){if (c is Cls_rptImage){//这里使图片优先打印((Cls_rptImage)c).DrawMe(g, new Point(printDocument.DefaultPageSettings.Margins.Left, printDocument.DefaultPageSettings.Margins.Top));}}foreach (Control c in pnlHeader.Controls){ if (c is Cls_rptLabel){((Cls_rptLabel)c).DrawMe(g, new Point(printDocument.DefaultPageSettings.Margins.Left, printDocument.DefaultPageSettings.Margins.Top));}else if (c is Cls_rptDrawLineHV){((Cls_rptDrawLineHV)c).DrawMe(g, new Point(printDocument.DefaultPageSettings.Margins.Left, printDocument.DefaultPageSettings.Margins.Top));}else if (c is Cls_rptSumPageData){((Cls_rptSumPageData)c).DrawMe(g, new Point(printDocument.DefaultPageSettings.Margins.Left, printDocument.DefaultPageSettings.Margins.Top), dtData, CurrentPrintDataArea);}else if (c is Cls_rptPageNumber){((Cls_rptPageNumber)c).DrawMe(g, new Point(printDocument.DefaultPageSettings.Margins.Left, printDocument.DefaultPageSettings.Margins.Top), CurrentPageNumber, AllPageNumber);} else if (c is Cls_rptCode128A){((Cls_rptCode128A)c).DrawMe(g, new Point(printDocument.DefaultPageSettings.Margins.Left, printDocument.DefaultPageSettings.Margins.Top));}else if(c is Cls_rptSumAllData){((Cls_rptSumAllData)c).DrawMe(g, new Point(printDocument.DefaultPageSettings.Margins.Left, printDocument.DefaultPageSettings.Margins.Top), dtData);}}#endregion #region 打印页脚内容//计算页脚打印起始点Point FooterStart = new Point(printDocument.DefaultPageSettings.Margins.Left, printDocument.DefaultPageSettings.PaperSize.Height -headerAndFooter.FooterHeight- printDocument.DefaultPageSettings.Margins.Bottom ); foreach(Control c in pnlFooter.Controls){if (c is Cls_rptImage){((Cls_rptImage)c).DrawMe(g, new Point(FooterStart.X, FooterStart.Y));}}foreach (Control c in pnlFooter.Controls){if (c is Cls_rptLabel){ ((Cls_rptLabel)c).DrawMe(g, new Point(FooterStart.X, FooterStart.Y));}else if(c is Cls_rptDrawLineHV){ ((Cls_rptDrawLineHV)c).DrawMe(g, new Point(FooterStart.X, FooterStart.Y));}else if (c is Cls_rptSumPageData){((Cls_rptSumPageData)c).DrawMe(g, new Point(FooterStart.X, FooterStart.Y), dtData, CurrentPrintDataArea);}else if (c is Cls_rptPageNumber){((Cls_rptPageNumber)c).DrawMe(g, new Point(FooterStart.X, FooterStart.Y), CurrentPageNumber, AllPageNumber);}else if (c is Cls_rptSumAllData){((Cls_rptSumAllData)c).DrawMe(g, new Point(FooterStart.X, FooterStart.Y), dtData);}}#endregion} private void toolStripButton7_Click(object sender, EventArgs e){//加入表格if (pnlData.Controls.Count <= 0){Cls_rptTable cTable = new Cls_rptTable();cTable.Location = new Point(0, 0);cTable.Size = new Size(100, 50); cTable.MouseUp += new MouseEventHandler(MyMouseUp); pnlData.Controls.Add(cTable);MyMouseUp(cTable, null);}}private void PageRefresh(){ panel1.Width = printDocument.DefaultPageSettings.PaperSize.Width; //printDocument.PrinterSettings.DefaultPageSettings.Bounds.Width;}private void frmWaterReportEdit_Load(object sender, EventArgs e){PageRefresh(); }private void toolStripMenuItem1_Click(object sender, EventArgs e){Cls_rptDrawLineHV cLine = new Cls_rptDrawLineHV();cLine.Location = new Point(20, 10);AddEditControl(cLine);pnlHeader.Controls.Add(cLine);MyMouseUp(cLine, null);}private void 页肢直线ToolStripMenuItem_Click(object sender, EventArgs e){Cls_rptDrawLineHV cLine = new Cls_rptDrawLineHV();cLine.Location = new Point(20, 10);AddEditControl(cLine);pnlFooter.Controls.Add(cLine);MyMouseUp(cLine, null);}private void 页脚ToolStripMenuItem_Click(object sender, EventArgs e){Cls_rptSumPageData cSumLabel = new Cls_rptSumPageData();AddEditControl(cSumLabel);pnlFooter.Controls.Add(cSumLabel);MyMouseUp(cSumLabel, null);}private void 页头ToolStripMenuItem_Click(object sender, EventArgs e){Cls_rptSumPageData cSumLabel = new Cls_rptSumPageData();AddEditControl(cSumLabel);pnlHeader.Controls.Add(cSumLabel);MyMouseUp(cSumLabel, null);}private void 插入页脚ToolStripMenuItem_Click(object sender, EventArgs e){Cls_rptPageNumber cPageNumber = new Cls_rptPageNumber();AddEditControl(cPageNumber);pnlFooter.Controls.Add(cPageNumber);MyMouseUp(cPageNumber, null);}private void 插入页对ToolStripMenuItem_Click(object sender, EventArgs e){Cls_rptImage cImage = new Cls_rptImage();AddEditControl(cImage);pnlHeader.Controls.Add(cImage);MyMouseUp(cImage, null);}private void 插入页脚ToolStripMenuItem1_Click(object sender, EventArgs e){Cls_rptImage cImage = new Cls_rptImage();AddEditControl(cImage);pnlFooter.Controls.Add(cImage);MyMouseUp(cImage, null);}private void 插入页头ToolStripMenuItem1_Click(object sender, EventArgs e){Cls_rptCode128A cCode = new Cls_rptCode128A();AddEditControl(cCode);pnlHeader.Controls.Add(cCode);MyMouseUp(cCode,null);}private void 插入页脚ToolStripMenuItem2_Click(object sender, EventArgs e){Cls_rptCode128A cCode = new Cls_rptCode128A();AddEditControl(cCode);pnlHeader.Controls.Add(cCode);MyMouseUp(cCode, null);}/// <summary>/// 刷新页头控件列表/// </summary>private void headerListRefresh(){ lstHeader.Items.Clear(); foreach(Control c in pnlHeader.Controls){if (c.Name == ""){//如果没有设置名称,则自动设置 c.Name = getUnuseedName(pnlHeader, "headItem");}lstHeader.Items.Add(c.Name); }}private string getUnuseedName(Panel pnl,string namehead){ string returnName=namehead;bool isReturn=true;for (int i = 0; i < pnl.Controls.Count; i++){isReturn = true;returnName = namehead + i.ToString();foreach (Control c in pnl.Controls){if (c.Name == returnName){//有重名isReturn = false;break;} }if (isReturn == true){break;}}return returnName;}private void PnlHeader_ControlAdded(object sender, ControlEventArgs e){headerListRefresh();}private void PnlHeader_ControlRemoved(object sender, ControlEventArgs e){headerListRefresh();}/// <summary>/// 刷新页脚控件列表/// </summary>private void footerListRefresh(){lstFooter.Items.Clear(); foreach (Control c in pnlFooter.Controls){if (c.Name == ""){//如果没有设置名称,则自动设置c.Name = getUnuseedName(pnlFooter, "footItem");}lstFooter.Items.Add(c.Name); }}private void PnlFooter_ControlAdded(object sender, ControlEventArgs e){footerListRefresh();}private void PnlFooter_ControlRemoved(object sender, ControlEventArgs e){footerListRefresh();}private void LstHeader_SelectedIndexChanged(object sender, EventArgs e){string chooseName = lstHeader.Items[lstHeader.SelectedIndex].ToString();foreach(Control c in pnlHeader.Controls){if (c.Name == chooseName){MyMouseUp(c, null);}}}private void PptGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e){//MessageBox.Show(e.OldValue.ToString());headerListRefresh();footerListRefresh();}private void LstFooter_SelectedIndexChanged(object sender, EventArgs e){string chooseName = lstFooter.Items[lstFooter.SelectedIndex].ToString();foreach (Control c in pnlFooter.Controls){if (c.Name == chooseName){MyMouseUp(c, null);}}}private void 页脚ToolStripMenuItem1_Click(object sender, EventArgs e){Cls_rptSumAllData cSumLabel = new Cls_rptSumAllData();AddEditControl(cSumLabel);pnlFooter.Controls.Add(cSumLabel);MyMouseUp(cSumLabel, null);}private void 页头ToolStripMenuItem1_Click(object sender, EventArgs e){Cls_rptSumAllData cSumLabel = new Cls_rptSumAllData();AddEditControl(cSumLabel);pnlHeader.Controls.Add(cSumLabel);MyMouseUp(cSumLabel, null);}private void 插入页头ToolStripMenuItem_Click(object sender, EventArgs e){Cls_rptPageNumber cPageNumber = new Cls_rptPageNumber();AddEditControl(cPageNumber);pnlHeader.Controls.Add(cPageNumber);MyMouseUp(cPageNumber, null);}}/// <summary>/// 报表页头页脚高度/// </summary>public class Cls_HeaderAndFooter{/// <summary>/// 页头高度/// </summary>public int HeaderHeight;/// <summary>/// 页脚高度/// </summary>public int FooterHeight;}
}
CSDN提示,读完要用两小时以上…. ,喜欢的,觉得有用的不会只花两小时的,继续
到这里一个报表编辑器就做好了,可以实现报表的编辑,也可以把编辑器接到.net程序里,让客户自己打开报表修改。当然,最重要的还是要打印出来,打印的代码看下面。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing.Printing;namespace WaterReport
{/// <summary>/// Water报表/// 为了解决第三方报表版权问题/// 以及微软报表客户端修改及新版本支持问题而做/// </summary>public class Cls_PrintWRPT{#region 报表参数/// <summary>/// 报表参数/// </summary>public class ParameterWRPT{/// <summary>/// 参数对应文本框的名称/// </summary>public string Name;/// <summary>/// 参数值/// </summary>public string value;/// <summary>/// 实例货参数/// </summary>/// <param name="mName">参数对应文本框的名称</param>/// <param name="mValue">参数值</param>public ParameterWRPT(string mName, string mValue){Name = mName;value = mValue;}}#endregion/// <summary>/// 弹出选择打印机窗口/// </summary>/// <returns></returns>public string PrinterSelect(){//设置打印机return new frmPrinterSelect().My_getPrinterSelect();}/// <summary>/// 直接打印报表,使用默认打印机打印/// </summary>/// <param name="wprtPathWithFileName">water报表路径,包含报表文件名和后缀</param>/// <param name="dt">要打印的数据,列名和报表设计的列名要相同,否则不能绑定</param>/// <param name="pw">报表参数</param>///<param name="bolSelectPrinter">是否选择打印机,FALSE则使用默认打印机</param>public void printRPT(string wprtPathWithFileName, DataTable dt,ParameterWRPT[] pw,bool bolSelectPrinter){try{#region 重用代码,暂不用//frmWaterReportEdit f = new frmWaterReportEdit(// wprtPathWithFileName// , dt// , pw); #endregionif (bolSelectPrinter == false){//使用默认打印机PrintDocument p = new PrintDocument();// 获取默认打印机printRPT(wprtPathWithFileName, dt, pw, p.PrinterSettings.PrinterName);}else{//选择打印机string strPrinter = new frmPrinterSelect().My_getPrinterSelect();if (strPrinter == ""){//未选择打印机,取消打印return;}printRPT(wprtPathWithFileName, dt, pw, strPrinter);#region 重用代码,暂不用设置指定打印机//f.printDocument.PrinterSettings.PrinterName = strPrinter;设置打印时不显示打印提示窗口//PrintController printController = new StandardPrintController();//f.printDocument.PrintController = printController;设置打印机从默认纸盒进纸//f.printDocument.DefaultPageSettings.PaperSize.RawKind = 9;//f.printDocument.Print();#endregion}}catch (Exception ex){System.Windows.Forms.MessageBox.Show(ex.Message);}}/// <summary>/// 直接打印报表,指定打印机打印/// </summary>/// <param name="wprtPathWithFileName">water报表路径,包含报表文件名和后缀</param>/// <param name="dt">要打印的数据,列名和报表设计的列名要相同,否则不能绑定</param>/// <param name="pw">报表参数</param>/// <param name="toPrinter">指定打印机</paparam>public void printRPT(string wprtPathWithFileName, DataTable dt, ParameterWRPT[] pw,string toPrinter){try{frmWaterReportEdit f = new frmWaterReportEdit(wprtPathWithFileName, dt, pw);//指定打印机f.printDocument.PrinterSettings.PrinterName = toPrinter;//设置打印时不显示打印提示窗口PrintController printController = new StandardPrintController();f.printDocument.PrintController = printController;//设置打印机从默认纸盒进纸f.printDocument.DefaultPageSettings.PaperSize.RawKind = 9;f.printDocument.Print();}catch (Exception ex){System.Windows.Forms.MessageBox.Show(ex.Message);}} }
}
到这里,整个报表打印类就基本做完了。本来想上传DLL文件的,但一次都没有传过,暂时不会,改天研究了。只希望以上这些代码能帮到需要的人。以上的代码很多都是来源于网络。有些是自己编写的。当然,有部分代码调用了自己写的一个数据文件操作类,如果看官遇到了,了解功能自己写一下吧。或是哪天有空,我把DLL上传了也就简单些。今天就到这里了。