Postupy: Kreslení svislého textu v rozhraní Windows Forms
Následující příklad kódu ukazuje, jak kreslit svislý text ve formuláři pomocí DrawString metody Graphics.
Příklad
public:
void DrawVerticalString()
{
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();
drawFormat->FormatFlags = StringFormatFlags::DirectionVertical;
formGraphics->DrawString(drawString, drawFont, drawBrush, x,
y, drawFormat);
delete drawFont;
delete drawBrush;
delete formGraphics;
}
public void DrawVerticalString()
{
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();
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
drawFont.Dispose();
drawBrush.Dispose();
formGraphics.Dispose();
}
Public Sub DrawVerticalString()
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
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical
formGraphics.DrawString(drawString, drawFont, drawBrush, _
x, y, drawFormat)
drawFont.Dispose()
drawBrush.Dispose()
formGraphics.Dispose()
End Sub
Probíhá kompilace kódu
Tuto metodu nelze volat v obslužné rutině Load události. Nakreslený obsah nebude překreslen, pokud je formulář změněn nebo zakryt jiným formulářem. Pokud chcete, aby se obsah automaticky překreslovat, měli byste tuto metodu OnPaint přepsat.
Robustní programování
Následující podmínky mohou způsobit výjimku:
- Písmo Arial není nainstalováno.
Viz také
Spolupracujte s námi na GitHubu
Zdroj tohoto obsahu najdete na GitHubu, kde můžete také vytvářet a kontrolovat problémy a žádosti o přijetí změn. Další informace najdete v našem průvodci pro přispěvatele.
.NET Desktop feedback
Váš názor
https://aka.ms/ContentUserFeedback.
Připravujeme: V průběhu roku 2024 budeme postupně vyřazovat problémy z GitHub coby mechanismus zpětné vazby pro obsah a nahrazovat ho novým systémem zpětné vazby. Další informace naleznete v tématu:Odeslat a zobrazit názory pro