Nasıl yapılır: Bir Windows Formunda Metin Çizme
Aşağıdaki kod örneği, bir forma DrawString metin çizmek için Graphics yönteminin nasıl kullanılagelmektedir. Alternatif olarak, bir TextRenderer forma metin çizmek için kullanabilirsiniz. Daha fazla bilgi için, bkz. How to: Draw Text with GDI.
Örnek
public:
void DrawString()
{
System::Drawing::Graphics^ formGraphics = this->CreateGraphics();
String^ drawString = "Sample Text";
System::Drawing::Font^ drawFont =
gcnew System::Drawing::Font("Arial", 16);
System::Drawing::SolidBrush^ drawBrush = gcnew
System::Drawing::SolidBrush(System::Drawing::Color::Black);
float x = 150.0F;
float y = 50.0F;
System::Drawing::StringFormat^ drawFormat =
gcnew System::Drawing::StringFormat();
formGraphics->DrawString(drawString, drawFont, drawBrush, x,
y, drawFormat);
delete drawFont;
delete drawBrush;
delete formGraphics;
}
public void DrawString()
{
System.Drawing.Graphics formGraphics = this.CreateGraphics();
string drawString = "Sample Text";
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
float x = 150.0F;
float y = 50.0F;
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat();
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
drawFont.Dispose();
drawBrush.Dispose();
formGraphics.Dispose();
}
Public Sub DrawString()
Dim formGraphics As System.Drawing.Graphics = Me.CreateGraphics()
Dim drawString As String = "Sample Text"
Dim drawFont As New System.Drawing.Font("Arial", 16)
Dim drawBrush As New _
System.Drawing.SolidBrush(System.Drawing.Color.Black)
Dim x As Single = 150.0
Dim y As Single = 50.0
Dim drawFormat As New System.Drawing.StringFormat
formGraphics.DrawString(drawString, drawFont, drawBrush, _
x, y, drawFormat)
drawFont.Dispose()
drawBrush.Dispose()
formGraphics.Dispose()
End Sub
Kod Derleniyor
Olay DrawString işleyicisinde yöntemini Load çağıramazsiniz. Form yeniden boyutlandırılırsa veya başka bir form tarafından engellerse, çizilen içerik yeniden çizilecek değil. İçeriğinizin otomatik olarak yeniden boyanmalarını yapmak için yöntemini geçersiz kılmanız OnPaint gerekir.
Güçlü Programlama
Aşağıdaki koşullar özel bir duruma neden olabilir:
- Arial yazı tipi yüklü değil.