アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
Public Shared Function MeasureText ( _ dc As IDeviceContext, _ text As String, _ font As Font, _ proposedSize As Size, _ flags As TextFormatFlags _ ) As Size
Dim dc As IDeviceContext Dim text As String Dim font As Font Dim proposedSize As Size Dim flags As TextFormatFlags Dim returnValue As Size returnValue = TextRenderer.MeasureText(dc, text, font, proposedSize, flags)
public static Size MeasureText ( IDeviceContext dc, string text, Font font, Size proposedSize, TextFormatFlags flags )
public: static Size MeasureText ( IDeviceContext^ dc, String^ text, Font^ font, Size proposedSize, TextFormatFlags flags )
public static Size MeasureText ( IDeviceContext dc, String text, Font font, Size proposedSize, TextFormatFlags flags )
public static function MeasureText ( dc : IDeviceContext, text : String, font : Font, proposedSize : Size, flags : TextFormatFlags ) : Size
パラメータ
- dc
-
テキストを計測するデバイス コンテキスト。
- text
-
計測するテキスト。
- font
-
計測するテキストに適用される Font。
- proposedSize
-
初期の外接する四角形の Size。
- flags
-
計測するテキストに適用される書式指定。
戻り値
指定した font と書式で描画される text の Size (ピクセル単位)。| 例外の種類 | 条件 |
|---|---|
| dc が null 参照 (Visual Basic では Nothing) です。 |
MeasureText メソッドは、テキスト サイズの確認時に、proposedSize パラメータと flags パラメータを使用して、高さの関係を示します。1 行のテキストを計測したときに proposedSize パラメータが Int32.MaxValue より大きい高さを持つ Size を表している場合、返される Size が調整されて、実際のテキストの高さが反映されます。
TextFormatFlags パラメータを受け取る DrawText オーバーロードの 1 つを使用して、テキストの描画方法を操作できます。たとえば、TextRenderer の既定の動作では、グリフの突出部が収まるように、描画されるテキストの外接する四角形にパディングが追加されます。この余分な領域なしでテキストの行を描画する必要がある場合は、例に示されているように、Size パラメータと TextFormatFlags パラメータを受け取るバージョンの DrawText および MeasureText を使用します。
MeasureText メソッドおよび DrawText メソッドを使用して、異なるフォント スタイルで 1 行のテキストを描画する方法の例を次に示します。この例を実行するには、次のコードを Windows フォームに貼り付け、フォームの Paint イベント ハンドラから、e を PaintEventArgs として渡すことにより、DrawALineOfText を呼び出します。
Private Sub DrawALineOfText(ByVal e As PaintEventArgs) ' Declare strings to render on the form. Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"} ' Declare fonts for rendering the strings. Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _ New Font("Arial", 14, FontStyle.Italic), _ New Font("Arial", 14, FontStyle.Regular)} Dim startPoint As New Point(10, 10) ' Set TextFormatFlags to no padding so strings are drawn together. Dim flags As TextFormatFlags = TextFormatFlags.NoPadding ' Declare a proposed size with dimensions set to the maximum integer value. Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue) ' Measure each string with its font and NoPadding value and draw it to the form. For i As Integer = 0 To stringsToPaint.Length - 1 Dim size As Size = TextRenderer.MeasureText(e.Graphics, _ stringsToPaint(i), fonts(i), proposedSize, flags) Dim rect As Rectangle = New Rectangle(startPoint, size) TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _ startPoint, Color.Black, flags) startPoint.X += size.Width Next End Sub
private static void DrawALineOfText(PaintEventArgs e) { // Declare strings to render on the form. string[] stringsToPaint = { "Tail", "Spin", " Toys" }; // Declare fonts for rendering the strings. Font[] fonts = { new Font("Arial", 14, FontStyle.Regular), new Font("Arial", 14, FontStyle.Italic), new Font("Arial", 14, FontStyle.Regular) }; Point startPoint = new Point(10, 10); // Set TextFormatFlags to no padding so strings are drawn together. TextFormatFlags flags = TextFormatFlags.NoPadding; // Declare a proposed size with dimensions set to the maximum integer value. Size proposedSize = new Size(int.MaxValue, int.MaxValue); // Measure each string with its font and NoPadding value and // draw it to the form. for (int i = 0; i < stringsToPaint.Length; i++) { Size size = TextRenderer.MeasureText(e.Graphics, stringsToPaint[i], fonts[i], proposedSize, flags); Rectangle rect = new Rectangle(startPoint, size); TextRenderer.DrawText(e.Graphics, stringsToPaint[i], fonts[i], startPoint, Color.Black, flags); startPoint.X += size.Width; } }
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。