カスタム レポート アイテムの実行時コンポーネントは、任意の CLS 準拠の言語を使用して Microsoft .NET Framework コンポーネントとして実装され、実行時にレポート プロセッサによって呼び出されます。 実行時コンポーネントのプロパティをデザイン環境で定義するには、カスタム レポート アイテムの対応するデザイン時コンポーネントを変更します。
完全に実装されたカスタム レポート アイテムの例については、「SQL Server Reporting Services Product Samples」 (SQL Server Reporting Services の製品サンプル) を参照してください。
定義オブジェクトとインスタンス オブジェクト
カスタム レポート アイテムを実装する前に、 定義オブジェクト オブジェクトと instance オブジェクトの違いを理解しておくことが重要です。 定義オブジェクトはカスタム レポート アイテムの RDL 表記を提供するのに対し、インスタンス オブジェクトは定義オブジェクトの評価されたバージョンです。 レポートのアイテムごとに定義オブジェクトは 1 つだけあります。 式を含む定義オブジェクトのプロパティにアクセスすると、未評価の式文字列が取得されます。 インスタンス オブジェクトには定義オブジェクトの評価されたバージョンが含まれるので、インスタンス オブジェクトはアイテムの定義オブジェクトと一対多のリレーションシップを持つことができます。 たとえば、詳細行に Tablix を含む CustomReportItem データ領域がレポートにある場合、定義オブジェクトは 1 つしか存在しませんが、インスタンス オブジェクトはデータ領域の各行に存在します。
ICustomReportItem インターフェイスを実装する
CustomReportItemランタイム コンポーネントを作成するには、Microsoft.ReportingServices.ProcessingCore.dllで定義されているICustomReportItem インターフェイスを実装する必要があります。
namespace Microsoft.ReportingServices.OnDemandReportRendering
{
public interface ICustomReportItem
{
void GenerateReportItemDefinition(CustomReportItem customReportItem);
void EvaluateReportItemInstance(CustomReportItem customReportItem);
}
}
ICustomReportItem インターフェイスを実装すると、GenerateReportItemDefinitionとEvaluateReportItemInstanceの 2 つのメソッド スタブが自動的に生成されます。 GenerateReportItemDefinition メソッドは最初に呼び出され、定義プロパティの設定と、アイテムのレンダリングに使用される定義プロパティとインスタンス プロパティの両方を含むImage オブジェクトの作成に使用されます。 EvaluateReportItemInstance メソッドは、定義オブジェクトが評価された後に呼び出され、項目のレンダリングに使用されるインスタンス オブジェクトが提供されます。
次の実装例は、コントロールの名前をイメージとして表示するカスタム レポート アイテムを示しています。
namespace Microsoft.Samples.ReportingServices
{
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using Microsoft.ReportingServices.OnDemandReportRendering;
public class PolygonsCustomReportItem : ICustomReportItem
{
#region ICustomReportItem Members
public void GenerateReportItemDefinition(CustomReportItem cri)
{
// Create the Image object that will be
// used to render the custom report item
cri.CreateCriImageDefinition();
Image polygonImage = (Image)cri.GeneratedReportItem;
}
public void EvaluateReportItemInstance(CustomReportItem cri)
{
// Get the Image definition
Image polygonImage = (Image)cri.GeneratedReportItem;
// Create the image for the custom report item
polygonImage.ImageInstance.ImageData = DrawImage(cri);
}
#endregion
/// <summary>
/// Creates an image of the CustomReportItem's name
/// </summary>
private byte[] DrawImage(CustomReportItem customReportItem)
{
int width = 1; // pixels
int height = 1; // pixels
int resolution = 75; // dpi
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height);
bitmap.SetResolution(resolution, resolution);
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);
graphics.PageUnit = System.Drawing.GraphicsUnit.Pixel;
// Get the Font for the Text
System.Drawing.Font font = new System.Drawing.Font(System.Drawing.FontFamily.GenericMonospace,
12, System.Drawing.FontStyle.Regular);
// Get the Brush for drawing the Text
System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen);
// Get the measurements for the image
System.Drawing.SizeF maxStringSize = graphics.MeasureString(customReportItem.Name, font);
width = (int)(maxStringSize.Width + 2 * font.GetHeight(resolution));
height = (int)(maxStringSize.Height + 2 * font.GetHeight(resolution));
bitmap.Dispose();
bitmap = new System.Drawing.Bitmap(width, height);
bitmap.SetResolution(resolution, resolution);
graphics.Dispose();
graphics = System.Drawing.Graphics.FromImage(bitmap);
graphics.PageUnit = System.Drawing.GraphicsUnit.Pixel;
// Draw the text
graphics.DrawString(customReportItem.Name, font, brush, font.GetHeight(resolution),
font.GetHeight(resolution));
// Create the byte array of the image data
MemoryStream memoryStream = new MemoryStream();
bitmap.Save(memoryStream, ImageFormat.Bmp);
memoryStream.Position = 0;
byte[] imageData = new byte[memoryStream.Length];
memoryStream.Read(imageData, 0, imageData.Length);
return imageData;
}
}
}