本例说明一种报表组件的实现方法。原理:将各个表格元素,以图像的方式设置、添加到纸张画布上,生成图像,打印图像。功能:1、支持SQL Serrver、Access数据库,如需其他数据库,也可很方便的添加到代码中,自动获取数据表,在元素中可选择添加字段到报表中;2、报表元素可自由拖放、设置大小、内容、字体、字体样式等(元素非PictureBox组件);3、支持全程的“撤销”、“重做”;4、可保存、读取报表文件;5、预览、打印没加上去,原理就是根据元素生成一幅或多幅图像,再输出到打印机,自行修改添加。这里说明一下,这只是个规划草稿,只是描述了一种报表组件的设计方式,所以代码写的较为繁琐、冗余,代码无委托线程等,所以比较容易看明白。 ///
/// 剪切复制粘贴组件操作 /// public class OperationObject { public bool isData { get; set; } // 是否有剪切复制粘贴数据 public Rectangle Region { get; set; } // 大小范围 public bool IsDragging { get; set; } // 能否拖动 public Point DraggingPoint { get; set; } // 组件拖动位置 public Bitmap Setimage { get; set; } // 组件图像 public int SetType { get; set; } // 组件类型 public bool isContent { get; set; } // 组件内是否有内容 public int Field_BandType { get; set; } // 栏目类型 public Bitmap Field_Img { get; set; } // 图像组件的图像 public string Field_Text { get; set; } // 文字 public string Field_TextFont { get; set; } // 字体 public int Field_TextFontSize { get; set; } // 字体大小 public FontStyle Field_TextFontStyle { get; set; } // 文字样式 public string Field_Align { get; set; } // 对齐方式 public int Field_ImgZoom { get; set; } // 图像缩放 public bool[] Field_BoxLine { get; set; } // 边框和斜线 public int Field_Shape { get; set; } // 形状的类型 } public static OperationObject operationObject; ///
/// 组件操作记录 /// public class RecordObject { public int Num; // 序号 public Rectangle Region; // 大小范围 public bool IsDragging; // 能否拖动 public Point DraggingPoint; // 组件拖动位置 public Bitmap Setimage; // 组件图像 public int SetType; // 组件类型 public bool isContent; // 组件内是否有内容 public int Field_BandType; // 栏目类型 public Bitmap Field_Img; // 图像组件的图像 public string Field_Text; // 文字 public string Field_TextFont; // 字体 public int Field_TextFontSize; // 字体大小 public FontStyle Field_TextFontStyle; // 文字样式 public string Field_Align; // 对齐方式 public int Field_ImgZoom; // 图像缩放 public bool[] Field_BoxLine; // 边框和斜线 public int Field_Shape; // 形状的类型 public RecordObject( int _Num,Rectangle _Region, bool _IsDragging, Point _DraggingPoint, Bitmap _Setimage, int _SetType, bool _isContent, int f_BandType, Bitmap f_Img, string f_Text, string f_TextFont, int f_TextFontSize, FontStyle f_TextFontStyle, string f_Align, int f_ImgZoom, bool[] f_BoxLine, int f_Shape) { Num = _Num; Region = _Region; IsDragging = _IsDragging; DraggingPoint = _DraggingPoint; Setimage = _Setimage; SetType = _SetType; isContent = _isContent; Field_BandType = f_BandType; Field_Img = f_Img; Field_Text = f_Text; Field_TextFont = f_TextFont; Field_TextFontSize = f_TextFontSize; Field_TextFontStyle = f_TextFontStyle; Field_Align = f_Align; Field_ImgZoom = f_ImgZoom; Field_BoxLine = f_BoxLine; Field_Shape = f_Shape; } } public static List
> recordObject; /// /// 组件移动 /// public abstract class ADraggableGDIObject { public abstract Rectangle Region { get; set; } // 大小范围 public abstract bool IsDragging { get; set; } // 能否拖动 public abstract Point DraggingPoint { get; set; } // 组件拖动位置 public abstract Bitmap Setimage { get; set; } // 组件图像 public abstract int SetType { get; set; } // 组件类型 public abstract void OnPaint(PaintEventArgs e); // 重绘 public abstract bool isContent { get; set; } // 组件内是否有内容 public abstract int Field_BandType { get; set; } // 栏目类型 public abstract Bitmap Field_Img { get; set; } // 图像组件的图像 public abstract string Field_Text { get; set; } // 文字 public abstract string Field_TextFont { get; set; } // 字体 public abstract int Field_TextFontSize { get; set; } // 字体大小 public abstract FontStyle Field_TextFontStyle { get; set; } // 文字样式 public abstract string Field_Align { get; set; } // 对齐方式 public abstract int Field_ImgZoom { get; set; } // 图像缩放 public abstract bool[] Field_BoxLine { get; set; } // 边框和斜线 public abstract int Field_Shape { get; set; } // 形状的类型 } /// /// 组件移动对象定义 /// public class Draggable : ADraggableGDIObject { private bool m_IsDragging; // 能否拖动 private Point m_DraggingPoint; // 组件拖动位置 private Rectangle m_Region; // 大小范围 private Bitmap m_image; // 组件图像 private int m_controlType; // 组件类型 private bool m_isContent; // 组件内是否有内容 private int f_BandType; // 栏目类型 private Bitmap f_Img; // 图像组件的图像 private string f_Text; // 文字 private string f_TextFont; // 字体 private int f_TextFontSize; // 字体大小 private FontStyle f_TextFontStyle; // 文字样式 private string f_Align; // 对齐方式 private int f_ImgZoom; // 图像缩放 private bool[] f_BoxLine = new bool[8] { false, false, false, false, false, false, false, false }; // 边框和斜线 private int f_Shape; // 形状的类型 public Draggable(int startx, int starty, int _settype) { m_Region = new Rectangle(startx, starty, 51, 51); m_controlType = _settype; m_isContent = false; } public override int SetType { get { return m_controlType; } set { m_controlType = value; } } public override Rectangle Region { get { return m_Region; } set { m_Region = value; } } public override Bitmap Setimage { get { return m_image; } set { m_image = value; } } public override void OnPaint(PaintEventArgs e) { e.Graphics.DrawImage(m_image, m_Region); } public override bool IsDragging { get { return m_IsDragging; } set { m_IsDragging = value; } } public override Point DraggingPoint { get { return m_DraggingPoint; } set { m_DraggingPoint = value; } } public override bool isContent { get { return m_isContent; } set { m_isContent = value; } } public override int Field_BandType { get { return f_BandType; } set { f_BandType = value; } } public override Bitmap Field_Img { get { return f_Img; } set { f_Img = value; } } public override string Field_Text { get { return f_Text; } set { f_Text = value; } } public override string Field_TextFont { get { return f_TextFont; } set { f_TextFont = value; } } public override int Field_TextFontSize { get { return f_TextFontSize; } set { f_TextFontSize = value; } } public override FontStyle Field_TextFontStyle { get { return f_TextFontStyle; } set { f_TextFontStyle = value; } } public override string Field_Align { get { return f_Align; } set { f_Align = value; } } public override int Field_ImgZoom { get { return f_ImgZoom; } set { f_ImgZoom = value; } } public override bool[] Field_BoxLine { get { return f_BoxLine; } set { f_BoxLine = value; } } public override int Field_Shape { get { return f_Shape; } set { f_Shape = value; } } } public static List m_DraggableGDIObjects; /// /// 序列化保存读取组件 /// [Serializable] public class SerializerObject { public string Data_Type; // 数据库类型 public string Data_DataName; //数据库名 public string Data_Table; // 数据表 public string Data_UserName; //用户名 public string Data_Password; // 密码 public string Data_ConnectionIP; // 地址 public string Page_TypeFace; public int Page_Direction; public int[] Page_Margin; public int controlNum; public Rectangle Region; // 大小范围 public bool IsDragging; // 能否拖动 public Point DraggingPoint; // 组件拖动位置 public Bitmap Setimage; // 组件图像 public int SetType; // 组件类型 public bool isContent; // 组件内是否有内容 public int Field_BandType; // 栏目类型 public Bitmap Field_Img; // 图像组件的图像 public string Field_Text; // 文字 public string Field_TextFont; // 字体 public int Field_TextFontSize; // 字体大小 public FontStyle Field_TextFontStyle; // 文字样式 public string Field_Align; // 对齐方式 public int Field_ImgZoom; // 图像缩放 public bool[] Field_BoxLine; // 边框和斜线 public int Field_Shape; // 形状的类型 public bool[] Band_Flag; // 栏目只出现一次 public SerializerObject( string _DataType, string _DataName,string _DataTable, string _DataUserName, string _DataPassword, string _DataConnectionIP, string _TypeFace, int _Direction, int[] _Margin, int _Num, Rectangle _Region, bool _IsDragging, Point _DraggingPoint, Bitmap _Setimage, int _SetType, bool _isContent, int f_BandType, Bitmap f_Img, string f_Text, string f_TextFont, int f_TextFontSize, FontStyle f_TextFontStyle, string f_Align, int f_ImgZoom, bool[] f_BoxLine, int f_Shape, bool[] _BandFlag) { Data_Type = _DataType; Data_DataName = _DataName; Data_Table = _DataTable; Data_UserName = _DataUserName; Data_Password = _DataPassword; Data_ConnectionIP = _DataConnectionIP; Page_TypeFace = _TypeFace; Page_Direction = _Direction; Page_Margin = _Margin; controlNum = _Num; Region = _Region; IsDragging = _IsDragging; DraggingPoint = _DraggingPoint; Setimage = _Setimage; SetType = _SetType; isContent = _isContent; Field_BandType = f_BandType; Field_Img = f_Img; Field_Text = f_Text; Field_TextFont = f_TextFont; Field_TextFontSize = f_TextFontSize; Field_TextFontStyle = f_TextFontStyle; Field_Align = f_Align; Field_ImgZoom = f_ImgZoom; Field_BoxLine = f_BoxLine; Field_Shape = f_Shape; Band_Flag = _BandFlag; } } public static List serializerObject = new List(); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static bool Change_Flag = false; // 是否改变了组件 /*窗体定义*/ public static Point screen_Location; // 窗体位置 public static Size screen_Size; //窗体大小 public static bool setFont_Flag = false; // 选择字体窗口打开标志 public static bool setFontSize_Flag = false; // 选择字体大小窗口打开标志 public static bool setDate_Flag = false; // 选择日期窗口打开标志 public static bool setPageCode_Flag = false; // 选择页码窗口打开标志 /*数据库*/ public static string Data_Type = ""; // 数据库类型 public static string Data_DataName = ""; // 数据库名 public static string Data_Table = ""; // 数据表 public static string Data_UserName = ""; //用户名 public static string Data_Password = ""; // 密码 public static string Data_ConnectionIP = ""; // 地址 /*组件区域位置*/ public static PointF[] CursorArea = new PointF[8]; // 鼠标区域 public static Font LabelFont = new Font("微软雅黑", 9, FontStyle.Regular); // label 字体设置 public static bool ShowLine = true; // 是否显示组件标线 /*设计页面*/ public static string Page_TypeFace = "A4"; // 纸张类型 public static int Page_Direction = 0; // 纸张方向 public static Size[] Page_Area = new Size[2] { new Size(789,1136), new Size(1136,789) }; // 横向、纵向纸张大小 public static int[] Page_Margin = new int[4] { 0,0,0,0 }; // 纸张边距 public static PanelEx pageDesing; // 设计框 public static PanelEx pageContainer; // 页面容器 public static PanelEx A4_PageType; // 页面 public static bool controlPlace = false; // 能否放置组件 public static int controlType = -1; // 组件类型 public static int control_Num = -1; // 组件序号 public static int oper_Record = -1; // 操作动作序号 /*底部状态信息栏*/ public static PanelEx statePanel; public static PanelEx[] control_Input = new PanelEx[8]; /*选择形状、栏目标志*/ public static int Select_Shape = 0; public static int Select_Band = 0; public static bool[] Band_Flag = new bool[3] { false,false,false}; // 栏目只出现一次