用 GDI+ 呈现字符串
获取对将用于绘图的图形对象的引用。有关更多信息,请参见用 GDI+ 创建图形图像。
' Visual Basic
' Obtains a reference to the graphics object for Button1
Dim g as Graphics = Button1.CreateGraphics
// C#
// Obtains a reference to the Graphics object for Button1
Graphics g = Button1.CreateGraphics();
// C++
// Obtains a reference to the Graphics object for button1
Graphics* g = button1->CreateGraphics();
创建绘制文本要使用的“画笔”的实例。例如:
' Visual Basic
Dim mybrush As New Drawing2D.LinearGradientBrush(ClientRectangle, _
Color.Red, Color.Yellow, Drawing2D.LinearGradientMode.Horizontal)
// C++
System::Drawing::Drawing2D::LinearGradientBrush* myBrush = new
System::Drawing::Drawing2D::LinearGradientBrush(ClientRectangle,
Color::Red, Color::Yellow, System::Drawing::Drawing2D::
LinearGradientMode::Horizontal);
创建显示文本要使用的字体。例如:
' Visual Basic
Dim myFont as New Font("Times New Roman", 24)
// C#
Font myFont = new Font("Times New Roman", 24);
// C++
System::Drawing::Font* myFont =
new System::Drawing::Font(S"Times New Roman", 24);
调用 Graphics 对象的 Graphics.DrawString 方法来呈现文本。
如果提供 RectangleF 对象,则文本将在矩形中换行。
否则,文本将从您提供的起始坐标处开始。
' Visual Basic
g.DrawString("Look at this text!", myFont, myBrush, New _
RectangleF(10, 10, 100, 200))
g.DrawString("Look at this text!", myFont, myBrush, 10, 10)
// C#
g.DrawString("Look at this text!", myFont, myBrush, new
RectangleF(10, 10, 100, 200));
g.DrawString("Look at this text!", myFont, myBrush, 10, 10);
// C++
g->DrawString(S"Look at this text!", myFont, myBrush,
RectangleF(10, 10, 100, 200));
g->DrawString(S"Look at this text!", myFont, myBrush, 10, 10);