[VC2005] GDI+を使って、フォームに文字を描画する方法

◆概要

この資料は、Microsoft(R) Visual C# 2005で GDI+を使って、フォームに文字を描画する方法について記述しています。


◆Sample

GDI+による文字の描画では、描画に使用するグラフィック オブジェクトへの参照を取得します。その後、テキストの描画に使用する Brush のインスタンスを作成し、Graphics オブジェクトの Graphics.DrawString メソッドを呼び出して、テキストをレンダリングします。 RectangleF オブジェクトを指定した場合、テキストは四角形内で折り返します。 それ以外の場合は、指定した座標を起点としてテキストが表示されます。

private void button1_Click(object sender, EventArgs e)
        {
            // 描画に使用するグラフィック オブジェクトへの参照を取得
             Graphics g = this.CreateGraphics();

            //テキストの描画に使用する Brush のインスタンスを作成
             System.Drawing.Drawing2D.LinearGradientBrush myBrush =
                            new System.Drawing.Drawing2D.LinearGradientBrush(
                    ClientRectangle, Color.Red, Color.Yellow,
                    System.Drawing.Drawing2D.LinearGradientMode.Horizontal
            );

            //テキストの表示に使用するフォントを作成
             Font myFont = new Font("Times New Roman", 24);

            g.DrawString("Look at this text!", myFont, myBrush,
                            new RectangleF(10, 10, 100, 200));
            g.DrawString("Look at this text!", myFont, myBrush, 10, 10);
        }


▼ページトップへ


◆実行結果

GDI+を使って、フォームに文字を描画



▼ページトップへ