文檔首頁>>Stimulsoft Reports.WinForms教程-2019>>【Stimulsoft Reports.WinForms教程】將自定義組件添加到報表設(shè)計器
【Stimulsoft Reports.WinForms教程】將自定義組件添加到報表設(shè)計器
【下載Stimulsoft Reports.Ultimate最新版本】
此示例顯示如何創(chuàng)建自定義組件并將其添加到報表設(shè)計器。自定義組件提供了在報表設(shè)計器中添加帶有參數(shù)的組件并在報表中使用它的功能。例如,您可以添加stimulsoft報表中不存在的圖表。
首先,指定屬性及其值:
[StiServiceBitmap(typeof(MyCustomComponent), "CustomComponent.MyCustomComponent1.gif")] [StiToolbox(true)] [StiContextTool(typeof(IStiShift))] [StiContextTool(typeof(IStiGrowToHeight))] [StiDesigner(typeof(MyCustomComponentDesigner))] [StiV1Builder(typeof(MyCustomComponentV1Builder))] [StiV2Builder(typeof(MyCustomComponentV2Builder))] [StiGdiPainter(typeof(MyCustomComponentGdiPainter))] ...
接下來,創(chuàng)建一個MyCustomComponent類并指定繼承的類。此外,覆蓋必需的組件屬性:
... public class MyCustomComponent : StiComponent, IStiBorder, IStiBrush { #region StiComponent override /// <summary> /// Gets value to sort a position in the toolbox. /// </summary> public override int ToolboxPosition { get { return 500; } } /// <summary> /// Gets a localized name of the component category. /// </summary> public override string LocalizedCategory { get { return StiLocalization.Get("Report", "Components"); } } /// <summary> /// Gets a localized component name. /// </summary> public override string LocalizedName { get { return "MyCustomComponent1"; } } #endregion ...
向組件添加新屬性——Border and Brush(邊框和畫筆),并設(shè)置其默認值:
... #region IStiBorder private StiBorder border = new StiBorder(); /// <summary> /// Gets or sets a frame of the component. /// </summary> [StiCategory("Appearance")] [StiSerializable] [Description("Gets or sets frame of the component.")] public StiBorder Border { get { return border; } set { border = value; } } #endregion #region IStiBrush private StiBrush brush = new StiSolidBrush(Color.Transparent); /// <summary> /// Gets or sets a brush to fill a component. /// </summary> [StiCategory("Appearance")] [StiSerializable] [Description("Gets or sets a brush to fill a component.")] public StiBrush Brush { get { return brush; } set { brush = value; } } #endregion ...
將必要的構(gòu)造函數(shù)添加到MyCustomComponent類型的新組件:
... #region this /// <summary> /// Creates a new component of the type MyCustomComponent. /// </summary> public MyCustomComponent() : this(RectangleD.Empty) { } /// <summary> /// Creates a new component of the type MyCustomComponent. /// </summary> /// <param name="rect">The rectangle describes size and position of the component.</param> public MyCustomComponent(RectangleD rect) : base(rect) { PlaceOnToolbox = true; } #endregion }
該AddCustomComponent方法添加自定義組件的StiConfig.Services收集,現(xiàn)在我們可以在報表設(shè)計器工具欄中找到它:
private static void AddCustomComponent() { StiConfig.Load(); StiOptions.Engine.ReferencedAssemblies = new string[]{ "System.Dll", "System.Drawing.Dll", "System.Windows.Forms.Dll", "System.Data.Dll", "System.Xml.Dll", "Stimulsoft.Controls.Dll", "Stimulsoft.Base.Dll", "Stimulsoft.Report.Dll", #region Add reference to your assembly "CustomComponent.exe" #endregion }; StiConfig.Services.Add(new MyCustomComponent()); StiConfig.Services.Add(new MyCustomComponentWithDataSource()); StiConfig.Services.Add(new MyCustomComponentWithExpression()); StiConfig.Save(); }
示例代碼的結(jié)果如下圖所示: