TextRenderer.DrawText 方法

定義

使用指定的裝置內容、色彩和字型,在指定的位置上繪製指定的文字。

多載

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color)

使用指定的裝置內容、字型和色彩,在指定的位置上繪製指定的文字。

DrawText(IDeviceContext, String, Font, Rectangle, Color, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩、背景色彩和格式化指示,在指定的界限之內繪製指定的文字。

DrawText(IDeviceContext, String, Font, Point, Color, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩、背景色彩和格式化指示,在指定位置繪製指定的文字。

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩、背景色彩和格式化指示,在指定位置繪製指定的文字。

DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩和格式化指示,在指定的界限之內繪製指定的文字。

DrawText(IDeviceContext, String, Font, Rectangle, Color, Color)

使用指定的裝置內容、字型、色彩和背景色彩,在指定的界限之內繪製指定的文字。

DrawText(IDeviceContext, String, Font, Point, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩和格式化指示,在指定的位置上繪製指定的文字。

DrawText(IDeviceContext, String, Font, Point, Color, Color)

使用指定的裝置內容、字型、色彩和背景色彩,在指定的位置上繪製指定的文字。

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩、背景色彩和格式化指示,在指定的界限之內繪製指定的文字。

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, Color)

使用指定的裝置內容、字型、色彩和背景色彩,在指定的界限之內繪製指定的文字。

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩和格式化指示,在指定的位置上繪製指定的文字。

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, Color)

使用指定的裝置內容、字型、色彩和背景色彩,在指定的位置上繪製指定的文字。

DrawText(IDeviceContext, String, Font, Rectangle, Color)

使用指定的裝置內容、字型和色彩,在指定的界限內繪製指定的文字。

DrawText(IDeviceContext, String, Font, Point, Color)

使用指定的裝置內容、字型和色彩,在指定的位置上繪製指定的文字。

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color)

使用指定的裝置內容、字型和色彩,在指定的界限內繪製指定的文字。

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩和格式化指示,在指定的界限之內繪製指定的文字。

備註

類別所提供的 TextRenderer 文字轉譯是以 GDI 文字轉譯為基礎,不支援從Windows Forms列印。 請改用 DrawString 類別的 Graphics 方法。

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color)

使用指定的裝置內容、字型和色彩,在指定的位置上繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, pt As Point, foreColor As Color)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
ReadOnlySpan<Char>

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

pt
Point

Point,表示已繪製之文字的左上角。

foreColor
Color

Color,要套用到已繪製的文字。

例外狀況

dcnull

適用於

DrawText(IDeviceContext, String, Font, Rectangle, Color, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩、背景色彩和格式化指示,在指定的界限之內繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor, System::Drawing::Color backColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, bounds As Rectangle, foreColor As Color, backColor As Color, flags As TextFormatFlags)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
String

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

bounds
Rectangle

Rectangle,表示文字的界限。

foreColor
Color

Color,要套用至文字。

backColor
Color

Color,要套用至以 bounds 表示的區域。

flags
TextFormatFlags

TextFormatFlags 值的位元組合。

例外狀況

dcnull

範例

下列程式碼範例會示範如何使用 DrawText 方法。 若要執行此範例,請將程式碼貼到 Windows Form 中,並從表單的 Paint 事件處理常式呼叫 RenderText8 ,並傳遞 ePaintEventArgs

private void RenderText8(PaintEventArgs e)
{
    TextFormatFlags flags = TextFormatFlags.Bottom | TextFormatFlags.WordBreak;
    TextRenderer.DrawText(e.Graphics, "This is some text that will display on multiple lines.", this.Font,
        new Rectangle(10, 10, 100, 50), SystemColors.ControlText, SystemColors.ControlDark, flags);
}
Private Sub RenderText8(ByVal e As PaintEventArgs)
    Dim flags As TextFormatFlags = _
        TextFormatFlags.Bottom Or TextFormatFlags.WordBreak
    TextRenderer.DrawText(e.Graphics, _
        "This is some text that will display on multiple lines.", _
        Me.Font, New Rectangle(10, 10, 100, 50), _
        SystemColors.ControlText, SystemColors.ControlDark, flags)

End Sub

備註

參數 backcolor 會分別套用至 參數內的 .bounds 區域:如果 fontforecolorbackcolornullEmptyDrawText 則方法會分別在 指定的 dc 裝置內容中選取的字型或色彩中繪製文字。 如果 為 forecolorTransparent ,則不會繪製文字。

您可以使用其中一個採用 DrawText 參數的 TextFormatFlags 多載,管理文字的繪製方式。 例如,TextRenderer 的預設行為是將邊框距離加入至已繪製之文字的週框 (Bounding Rectangle),以容納突出 (Overhanging) 的圖像 (Glyph)。 如果您需要繪製一行文字而不含這些額外空格,則應該使用會採用 DrawTextMeasureText 參數的 SizeTextFormatFlags 版本。 如需範例,請參閱 MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)

類別所提供的 TextRenderer 文字轉譯是以 GDI 文字轉譯為基礎,不支援從Windows Forms列印。 請改用 DrawString 類別的方法 Graphics

TextRenderer不支援將定位停駐點新增至繪製的文字,不過您可以使用 旗標展開現有的定位停駐點 ExpandTabs

適用於

DrawText(IDeviceContext, String, Font, Point, Color, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩、背景色彩和格式化指示,在指定位置繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor, System::Drawing::Color backColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, pt As Point, foreColor As Color, backColor As Color, flags As TextFormatFlags)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
String

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

pt
Point

Point,表示已繪製之文字的左上角。

foreColor
Color

Color,要套用至文字。

backColor
Color

Color,要套用至已繪製之文字的背景區域。

flags
TextFormatFlags

TextFormatFlags 值的位元組合。

例外狀況

dcnull

範例

下列程式碼範例會示範如何使用 DrawText 方法。 若要執行此範例,請將程式碼貼到 Windows Form 中,並從表單的 Paint 事件處理常式呼叫 RenderText7 ,並傳遞 ePaintEventArgs

private void RenderText7(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "This is some text.", this.Font,
        new Point(10, 10), Color.White, Color.SteelBlue, TextFormatFlags.Default);
}
Private Sub RenderText7(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "This is some text.", _
        Me.Font, New Point(10, 10), Color.White, Color.SteelBlue, _
        TextFormatFlags.Default)

End Sub

備註

參數 backcolor 會套用包含繪製文字的矩形區域。 如果 font 或 為 nullEmptyforecolorDrawText 則方法會分別繪製目前在所指定 dc 裝置內容中選取的字型或色彩中的文字。 如果 為 forecolorTransparent ,則不會繪製文字。

您可以使用其中一個採用 DrawText 參數的 TextFormatFlags 多載,管理文字的繪製方式。 例如,TextRenderer 的預設行為是將邊框距離加入至已繪製之文字的週框 (Bounding Rectangle),以容納突出 (Overhanging) 的圖像 (Glyph)。 如果您需要繪製一行文字而不含這些額外空格,則應該使用會採用 DrawTextMeasureText 參數的 SizeTextFormatFlags 版本。 如需範例,請參閱 MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)

類別所提供的 TextRenderer 文字轉譯是以 GDI 文字轉譯為基礎,不支援從Windows Forms列印。 請改用 DrawString 類別的方法 Graphics

TextRenderer不支援將定位停駐點新增至繪製的文字,不過您可以使用 旗標展開現有的定位停駐點 ExpandTabs

注意

指定 DrawTextPoint 為所繪製文字左上角的方法,不會在 Windows 2000 上正確轉譯。 如果您的應用程式用於執行 Windows 2000 的機器上,您應該使用其中一個 DrawText 指定所繪製文字界限的方法 Rectangle

適用於

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩、背景色彩和格式化指示,在指定位置繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor, System::Drawing::Color backColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, pt As Point, foreColor As Color, backColor As Color, flags As TextFormatFlags)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
ReadOnlySpan<Char>

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

pt
Point

Point,表示已繪製之文字的左上角。

foreColor
Color

Color,要套用到已繪製的文字。

backColor
Color

Color,要套用至已繪製之文字的背景區域。

flags
TextFormatFlags

TextFormatFlags 值的位元組合。

例外狀況

dc 上所宣告的預設值是 null

適用於

DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩和格式化指示,在指定的界限之內繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, bounds As Rectangle, foreColor As Color, flags As TextFormatFlags)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
String

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

bounds
Rectangle

Rectangle,表示文字的界限。

foreColor
Color

Color,要套用到已繪製的文字。

flags
TextFormatFlags

TextFormatFlags 值的位元組合。

例外狀況

dcnull

範例

下列程式碼範例會示範如何使用 DrawText 方法。 若要執行此範例,請將程式碼貼到 Windows Form 中,並從表單的 Paint 事件處理常式呼叫 RenderText6 ,並傳遞 ePaintEventArgs

private void RenderText6(PaintEventArgs e)
{
    TextFormatFlags flags = TextFormatFlags.Bottom | TextFormatFlags.EndEllipsis;
    TextRenderer.DrawText(e.Graphics, "This is some text that will be clipped at the end.", this.Font,
        new Rectangle(10, 10, 100, 50), SystemColors.ControlText, flags);
}
Private Sub RenderText6(ByVal e As PaintEventArgs)
    Dim flags As TextFormatFlags = TextFormatFlags.Bottom Or _
        TextFormatFlags.EndEllipsis
    TextRenderer.DrawText(e.Graphics, _
    "This is some text that will be clipped at the end.", _
    Me.Font, New Rectangle(10, 10, 100, 50), SystemColors.ControlText, flags)

End Sub

備註

如果 font 或 為 nullEmptycolorDrawText 則方法會分別繪製目前在所指定 dc 裝置內容中選取的字型或色彩中的文字。 如果 為 forecolorTransparent ,則不會繪製文字。

您可以使用其中一個採用 DrawText 參數的 TextFormatFlags 多載,管理文字的繪製方式。 例如,TextRenderer 的預設行為是將邊框距離加入至已繪製之文字的週框 (Bounding Rectangle),以容納突出 (Overhanging) 的圖像 (Glyph)。 如果您需要繪製一行文字而不含這些額外空格,則應該使用會採用 DrawTextMeasureText 參數的 SizeTextFormatFlags 版本。 如需範例,請參閱 MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)

類別所提供的 TextRenderer 文字轉譯是以 GDI 文字轉譯為基礎,不支援從Windows Forms列印。 請改用 DrawString 類別的方法 Graphics

TextRenderer不支援將定位停駐點新增至繪製的文字,不過您可以使用 旗標展開現有的定位停駐點 ExpandTabs

適用於

DrawText(IDeviceContext, String, Font, Rectangle, Color, Color)

使用指定的裝置內容、字型、色彩和背景色彩,在指定的界限之內繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor, System::Drawing::Color backColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, bounds As Rectangle, foreColor As Color, backColor As Color)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
String

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

bounds
Rectangle

Rectangle,表示文字的界限。

foreColor
Color

Color,要套用到已繪製的文字。

backColor
Color

Color,要套用至以 bounds 表示的區域。

例外狀況

dcnull

範例

下列程式碼範例會示範如何使用 DrawText 方法。 若要執行此範例,請將程式碼貼到 Windows Form 中,並從表單的 Paint 事件處理常式呼叫 RenderText4 ,並傳遞 ePaintEventArgs

private void RenderText4(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text.", this.Font,
        new Rectangle(10, 10, 70, 70), SystemColors.ControlText, 
        SystemColors.ControlDark);
}
Private Sub RenderText4(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Regular Text.", _
        Me.Font, New Rectangle(10, 10, 70, 70), _
        SystemColors.ControlText, SystemColors.ControlDark)

End Sub

備註

參數 backColor 會套用至 參數內的 bounds 區域。 如果 為 fontbackcolor ,則為 或 Emptynullforecolor ,則 DrawText 方法會繪製目前在所指定 dc 裝置內容中選取的字型或色彩中的文字。 如果 為 forecolorTransparent ,則不會繪製文字。

這個方法會產生水準和垂直置中于 參數所 bounds 指定矩形中的文字。 若要變更文字的繪製方式,請使用 採用參數類型的 版本 DrawTextTextFormatFlags

類別所提供的 TextRenderer 文字轉譯是以 GDI 文字轉譯為基礎,不支援從Windows Forms列印。 請改用 DrawString 類別的方法 Graphics

適用於

DrawText(IDeviceContext, String, Font, Point, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩和格式化指示,在指定的位置上繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, pt As Point, foreColor As Color, flags As TextFormatFlags)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
String

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

pt
Point

Point,表示已繪製之文字的左上角。

foreColor
Color

Color,要套用到已繪製的文字。

flags
TextFormatFlags

TextFormatFlags 值的位元組合。

例外狀況

dcnull

範例

下列程式碼範例會示範如何使用 DrawText 方法。 若要執行此範例,請將程式碼貼到 Windows Form 中,並從表單的 Paint 事件處理常式呼叫 RenderText5 ,並傳遞 ePaintEventArgs

private void RenderText5(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Some text.",
        this.Font, new Point(10, 10), SystemColors.ControlText, TextFormatFlags.Bottom);
}
Private Sub RenderText5(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Some text.", _
    Me.Font, New Point(10, 10), SystemColors.ControlText, _
    TextFormatFlags.Bottom)

End Sub

備註

您可以使用其中一個採用 DrawText 參數的 TextFormatFlags 多載,管理文字的繪製方式。 例如,TextRenderer 的預設行為是將邊框距離加入至已繪製之文字的週框 (Bounding Rectangle),以容納突出 (Overhanging) 的圖像 (Glyph)。 如果您需要繪製一行文字而不含這些額外空格,則應該使用會採用 DrawTextMeasureText 參數的 SizeTextFormatFlags 版本。 如需範例,請參閱 MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)

參數 backColor 會套用至 參數內的 bounds 區域。 如果 font 或 為 nullColor.EmptyforecolorDrawText 則方法會分別繪製目前在所指定 dc 裝置內容中選取的字型或色彩中的文字。 如果 為 forecolorTransparent ,則不會繪製文字。

類別所提供的 TextRenderer 文字轉譯是以 GDI 文字轉譯為基礎,不支援從Windows Forms列印。 請改用 DrawString 類別的方法 Graphics

TextRenderer不支援將定位停駐點新增至繪製的文字,不過您可以使用 旗標展開現有的定位停駐點 ExpandTabs

注意

指定 DrawTextPoint 為所繪製文字左上角的方法,不會在 Windows 2000 上正確轉譯。 如果您的應用程式用於執行 Windows 2000 的機器上,您應該使用其中一個 DrawText 指定所繪製文字界限的方法 Rectangle

適用於

DrawText(IDeviceContext, String, Font, Point, Color, Color)

使用指定的裝置內容、字型、色彩和背景色彩,在指定的位置上繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor, System::Drawing::Color backColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, pt As Point, foreColor As Color, backColor As Color)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
String

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

pt
Point

Point,表示已繪製之文字的左上角。

foreColor
Color

Color,要套用到已繪製的文字。

backColor
Color

Color,要套用至已繪製之文字的背景區域。

例外狀況

dcnull

範例

下列程式碼範例會示範如何使用 DrawText 方法。 若要執行此範例,請將程式碼貼到 Windows Form 中,並從表單的 Paint 事件處理常式呼叫 RenderText3 ,並傳遞 ePaintEventArgs

private void RenderText3(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font,
        new Point(10, 10), Color.Red, Color.PowderBlue);
}
Private Sub RenderText3(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Regular Text", Me.Font, _
        New Point(10, 10), Color.Red, Color.PowderBlue)

End Sub

備註

參數 backColor 會套用至包含繪製文字的矩形區域。 如果 為 fontbackcolor ,則為 或 Color.Emptynullforecolor ,則 DrawText 方法會繪製目前在所指定 dc 裝置內容中選取的字型或色彩中的文字。 如果 為 forecolorTransparent ,則不會繪製文字。

您可以使用其中一個採用 DrawText 參數的 TextFormatFlags 多載,管理文字的繪製方式。

類別所提供的 TextRenderer 文字轉譯是以 GDI 文字轉譯為基礎,不支援從Windows Forms列印。 請改用 DrawString 類別的方法 Graphics

注意

指定 DrawTextPoint 為所繪製文字左上角的方法,不會在 Windows 2000 上正確轉譯。 如果您的應用程式用於執行 Windows 2000 的機器上,您應該使用其中一個 DrawText 指定所繪製文字界限的方法 Rectangle

適用於

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩、背景色彩和格式化指示,在指定的界限之內繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor, System::Drawing::Color backColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, bounds As Rectangle, foreColor As Color, backColor As Color, flags As TextFormatFlags)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
ReadOnlySpan<Char>

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

bounds
Rectangle

Rectangle,表示文字的界限。

foreColor
Color

Color,要套用到已繪製的文字。

backColor
Color

Color,要套用至已繪製之文字的背景區域。

flags
TextFormatFlags

TextFormatFlags 值的位元組合。

例外狀況

dcnull

適用於

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, Color)

使用指定的裝置內容、字型、色彩和背景色彩,在指定的界限之內繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor, System::Drawing::Color backColor);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, bounds As Rectangle, foreColor As Color, backColor As Color)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
ReadOnlySpan<Char>

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

bounds
Rectangle

Rectangle,表示文字的界限。

foreColor
Color

Color,要套用到已繪製的文字。

backColor
Color

Color,要套用至已繪製之文字的背景區域。

例外狀況

dcnull

適用於

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩和格式化指示,在指定的位置上繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, pt As Point, foreColor As Color, flags As TextFormatFlags)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
ReadOnlySpan<Char>

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

pt
Point

Point,表示已繪製之文字的左上角。

foreColor
Color

Color,要套用到已繪製的文字。

flags
TextFormatFlags

TextFormatFlags 值的位元組合。

例外狀況

dcnull

適用於

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, Color)

使用指定的裝置內容、字型、色彩和背景色彩,在指定的位置上繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor, System::Drawing::Color backColor);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, pt As Point, foreColor As Color, backColor As Color)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
ReadOnlySpan<Char>

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

pt
Point

Point,表示已繪製之文字的左上角。

foreColor
Color

Color,要套用到已繪製的文字。

backColor
Color

Color,要套用至已繪製之文字的背景區域。

例外狀況

dcnull

適用於

DrawText(IDeviceContext, String, Font, Rectangle, Color)

使用指定的裝置內容、字型和色彩,在指定的界限內繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, bounds As Rectangle, foreColor As Color)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
String

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

bounds
Rectangle

Rectangle,表示文字的界限。

foreColor
Color

Color,要套用到已繪製的文字。

例外狀況

dcnull

範例

下列程式碼範例會示範如何使用 DrawText 方法。 若要執行此範例,請將程式碼貼到 Windows Form 中,並從表單的事件處理常式呼叫 RenderText2 ,並 e 傳遞為 PaintEventArgsPaint

private void RenderText2(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font,
        new Rectangle(10, 10, 100, 100), SystemColors.ControlText);
}
Private Sub RenderText2(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Regular Text", _
        Me.Font, New Rectangle(10, 10, 100, 100), _
        SystemColors.ControlText)

End Sub

備註

如果 font 或 分別為 nullColor.Emptyforecolor ,則 DrawText 方法會繪製目前在 所 dc 指定裝置內容中選取的字型或色彩中的文字。 如果 forecolorTransparent ,則不會繪製文字。

這個方法會導致文字水準和垂直置中于 參數所 bounds 指定的矩形中。 若要變更文字的繪製方式,請使用 採用 TextFormatFlags 參數類型的 版本 DrawText

類別所提供的 TextRenderer 文字轉譯是以 GDI 文字轉譯為基礎,不支援從Windows Forms列印。 請改用 DrawString 類別的 Graphics 方法。

另請參閱

適用於

DrawText(IDeviceContext, String, Font, Point, Color)

使用指定的裝置內容、字型和色彩,在指定的位置上繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, pt As Point, foreColor As Color)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
String

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

pt
Point

Point,表示已繪製之文字的左上角。

foreColor
Color

Color,要套用到已繪製的文字。

例外狀況

dcnull

範例

下列程式碼範例會示範如何使用 DrawText 方法。 若要執行此範例,請將程式碼貼到 Windows Form 中,並從表單的事件處理常式呼叫 RenderText1 ,並 e 傳遞為 PaintEventArgsPaint

private void RenderText1(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font, 
        new Point(10, 10), SystemColors.ControlText);
}
Private Sub RenderText1(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Regular Text", _
        Me.Font, New Point(10, 10), SystemColors.ControlText)

End Sub

備註

您可以使用其中一個採用 DrawText 參數的 TextFormatFlags 多載,管理文字的繪製方式。

如果 為 nullfontforecolorColor.Empty ,則 DrawText 方法會以 目前在 所 dc 指定的裝置內容中選取的字型或色彩繪製文字。 如果 forecolorTransparent ,則不會繪製文字。

類別所提供的 TextRenderer 文字轉譯是以 GDI 文字轉譯為基礎,不支援從Windows Forms列印。 請改用 DrawString 類別的 Graphics 方法。

注意

指定 DrawText 為繪製文字左上角的方法 Point ,無法在 Windows 2000 上正確呈現。 如果您的應用程式適用于執行 Windows 2000 的機器,您應該使用其中一種方法 DrawText 來指定 Rectangle 繪製文字界限的 。

另請參閱

適用於

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color)

使用指定的裝置內容、字型和色彩,在指定的界限內繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, bounds As Rectangle, foreColor As Color)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
ReadOnlySpan<Char>

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

bounds
Rectangle

Rectangle,表示文字的界限。

foreColor
Color

Color,要套用到已繪製的文字。

例外狀況

dcnull

適用於

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, TextFormatFlags)

使用指定的裝置內容、字型、色彩和格式化指示,在指定的界限之內繪製指定的文字。

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, bounds As Rectangle, foreColor As Color, flags As TextFormatFlags)

參數

dc
IDeviceContext

要在其中繪製文字的裝置內容。

text
ReadOnlySpan<Char>

要繪製的文字。

font
Font

Font,要套用到已繪製的文字。

bounds
Rectangle

Rectangle,表示文字的界限。

foreColor
Color

Color,要套用到已繪製的文字。

flags
TextFormatFlags

TextFormatFlags 值的位元組合。

例外狀況

dcnull

適用於