方法 : 中抜きの文字列を作成する

更新 : 2007 年 11 月

通常、Windows Presentation Foundation (WPF) アプリケーションでテキスト文字列に装飾を追加する場合は、別個の文字、つまりグリフのコレクションとしてのテキストを使用します。たとえば、線形グラデーション ブラシを作成し、それを TextBox オブジェクトの Foreground プロパティに適用できます。テキスト ボックスを表示または編集すると、テキスト文字列の現在の文字セットに、線形グラデーション ブラシが自動的に適用されます。

テキスト ボックスに適用された線形グラデーション ブラシの例
線形グラデーション ブラシで表示されるテキスト

テキストを Geometry オブジェクトに変換し、人の目をひきつける他の種類のテキストを作成することもできます。たとえば、テキスト文字列のアウトラインに基づいて Geometry オブジェクトを作成できます。

アウトライン ジオメトリのテキストに適用された線形グラデーション ブラシの例
線形グラデーション ブラシを使用するテキスト アウトライン

テキストを Geometry オブジェクトに変換すると、テキストは文字の集まりではなくなります。つまり、文字列内の文字を変更することはできません。ただし、変換されたテキストのストロークおよび塗りつぶしのプロパティを変更することで、テキストの外観を変えることができます。ストロークは、変換したテキストのアウトラインを参照します。塗りつぶしは、変換したテキストのアウトラインの内側の領域を参照します。

変換されたテキストのストロークおよび塗りつぶしを変更して、視覚効果を作成するいくつかの方法を次の例に示します。

ストロークおよび塗りつぶしを別々の色に設定した例
塗りつぶしとストロークに別の色を使用するテキストストロークに適用したイメージ ブラシの例
ストロークに適用されるイメージ ブラシを含むテキスト

変換されたテキストの境界ボックスの四角形または強調表示を変更することもできます。変換されたテキストのストロークおよび強調表示を変更して、視覚効果を作成する方法の一例を次に示します。

ストロークおよび強調表示に適用したイメージ ブラシの例
ストロークに適用されるイメージ ブラシを含むテキスト

ms745816.alert_note(ja-jp,VS.90).gifメモ :

次のコード例を含むコード サンプル全体については、「外枠付きテキスト カスタム コントロールのサンプル」を参照してください。

使用例

テキストを Geometry オブジェクトに変換する場合の要点は、FormattedText オブジェクトを使用することです。このオブジェクトを作成したら、BuildGeometry メソッドと BuildHighlightGeometry メソッドを使用して、テキストを Geometry オブジェクトに変換できます。最初のメソッドは、書式設定されたテキストのジオメトリを返します。2 番目のメソッドは、書式設定されたテキストの境界ボックスのジオメトリを返します。FormattedText オブジェクトを作成し、書式設定されたテキストとその境界ボックスのジオメトリを取得する方法を次のコード例に示します。

/// <summary>
/// Create the outline geometry based on the formatted text.
/// </summary>
public void CreateText()
{
    System.Windows.FontStyle fontStyle = FontStyles.Normal;
    FontWeight fontWeight = FontWeights.Medium;

    if (Bold == true) fontWeight = FontWeights.Bold;
    if (Italic == true) fontStyle = FontStyles.Italic;

    // Create the formatted text based on the properties set.
    FormattedText formattedText = new FormattedText(
        Text,
        CultureInfo.GetCultureInfo("en-us"),
        FlowDirection.LeftToRight,
        new Typeface(
            Font,
            fontStyle,
            fontWeight,
            FontStretches.Normal),
        FontSize,
        System.Windows.Media.Brushes.Black // This brush does not matter since we use the geometry of the text. 
        );

    // Build the geometry object that represents the text.
    _textGeometry = formattedText.BuildGeometry(new System.Windows.Point(0, 0));

    // Build the geometry object that represents the text hightlight.
    if (Highlight == true)
    {
        _textHighLightGeometry = formattedText.BuildHighlightGeometry(new System.Windows.Point(0, 0));
    }
}

取得された Geometry オブジェクトを表示するには、変換されたテキストを表示しているオブジェクトの DrawingContext にアクセスする必要があります。これらのコード例では、ユーザー定義のレンダリングをサポートするクラスの派生カスタム コントロール オブジェクトを作成することで、これを行います。カスタム コントロールの作成の詳細については、「外枠付きテキスト カスタム コントロールのサンプル」を参照してください。

カスタム コントロールで Geometry オブジェクトを表示するには、OnRender メソッドのオーバーライドを提供します。オーバーライドしたメソッドでは、DrawGeometry メソッドを使用して Geometry オブジェクトを描画する必要があります。

/// <summary>
/// OnRender override draws the geometry of the text and optional highlight.
/// </summary>
/// <param name="drawingContext">Drawing context of the OutlineText control.</param>
protected override void OnRender(DrawingContext drawingContext)
{
    // Draw the outline based on the properties that are set.
    drawingContext.DrawGeometry(Fill, new System.Windows.Media.Pen(Stroke, StrokeThickness), _textGeometry);

    // Draw the text highlight based on the properties that are set.
    if (Highlight == true)
    {
        drawingContext.DrawGeometry(null, new System.Windows.Media.Pen(Stroke, StrokeThickness), _textHighLightGeometry);
    }
}

参照

処理手順

外枠付きテキスト カスタム コントロールのサンプル

概念

書式設定されたテキストの描画