格式化文字
若要將特殊格式套用於文字,請初始化 StringFormat 物件,並將該物件的位址傳遞至 Graphics 類別的 DrawString 方法。
若要在矩形中繪製格式化的文字,需要有 Graphics、FontFamily、Font、RectF、StringFormat 和 Brush 物件。
對齊文字
下列範例會在矩形中繪製文字。每一行文字都會置中,而且整個文字區塊也會置於矩形中央 (從上到下)。
Dim myText As String = "Use StringFormat and RectangleF objects to center text in a rectangle."
Dim fontFamily As New FontFamily("Arial")
Dim font As New Font( _
fontFamily, _
12, _
FontStyle.Bold, _
GraphicsUnit.Point)
Dim rect As New Rectangle(30, 10, 120, 140)
Dim stringFormat As New StringFormat()
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
' Center each line of text.
stringFormat.Alignment = StringAlignment.Center
' Center the block of text (top to bottom) in the rectangle.
stringFormat.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(myText, font, solidBrush, RectangleF.op_implicit(rect), stringFormat)
Dim pen As Pen = Pens.Black
e.Graphics.DrawRectangle(pen, rect)
[C#]
string text = "Use StringFormat and RectangleF objects to center text in a rectangle.";
FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(
fontFamily,
12, FontStyle.Bold,
GraphicsUnit.Point);
Rectangle rect = new Rectangle(30, 10, 120, 140);
StringFormat stringFormat = new StringFormat();
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));
// Center each line of text.
stringFormat.Alignment = StringAlignment.Center;
// Center the block of text (top to bottom) in the rectangle.
stringFormat.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(text, font, solidBrush, rect, stringFormat);
Pen pen = Pens.Black;
e.Graphics.DrawRectangle(pen, rect);
下圖顯示的是矩形和置中的文字。
上述程式碼會設定 StringFormat 物件的兩個屬性:Alignment 和 LineAlignment。Alignment 屬性會指定每一行文字都要水平置於傳遞至 DrawString 方法的矩形中央。LineAlignment 屬性則會指定文字區塊要垂直 (從上到下) 置於矩形中央。
Center 值是 StringAlignment 列舉型別的成員。
設定定位停駐點
您可以呼叫 StringFormat 物件的 SetTabStops 方法,然後將這個 StringFormat 物件傳遞至 Graphics 類別的 DrawString 方法,即可設定定位停駐點。
下列範例會在 150、250 和 350 設定定位停駐點,然後程式碼會顯示名稱和考試成績的索引標籤式清單。
Dim myText As String = _
"Name" + ControlChars.Tab + _
"Test 1" + ControlChars.Tab + _
"Test 2" + ControlChars.Tab + _
"Test 3" + ControlChars.Cr
myText = myText + "Joe" + ControlChars.Tab + _
"95" + ControlChars.Tab + _
"88" + ControlChars.Tab + +
"91" + ControlChars.Cr
myText = myText + "Mary" + ControlChars.Tab + _
"98" + ControlChars.Tab + _
"84" + ControlChars.Tab + _
"90" + ControlChars.Cr
myText = myText + "Sam" + ControlChars.Tab + _
"42" + ControlChars.Tab + _
"76" + ControlChars.Tab + _
"98" + ControlChars.Cr
myText = myText + "Jane" + ControlChars.Tab + _
"65" + ControlChars.Tab + _
"73" + ControlChars.Tab + _
"92" + ControlChars.Cr
Dim fontFamily As New FontFamily("Courier New")
Dim font As New Font( _
fontFamily, _
12, _
FontStyle.Regular, _
GraphicsUnit.Point)
Dim rect As New Rectangle(10, 10, 450, 100)
Dim stringFormat As New StringFormat()
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
Dim tabs As Single() = {150, 100, 100, 100}
stringFormat.SetTabStops(0, tabs)
e.Graphics.DrawString(myText, font, solidBrush, RectangleF.op_implicit(rect), stringFormat)
Dim pen As Pen = Pens.Black
e.Graphics.DrawRectangle(pen, rect)
[C#]
string text = "Name\tTest 1\tTest 2\tTest 3\n";
text = text + "Joe\t95\t88\t91\n";
text = text + "Mary\t98\t84\t90\n";
text = text + "Sam\t42\t76\t98\n";
text = text + "Jane\t65\t73\t92\n";
FontFamily fontFamily = new FontFamily("Courier New");
Font font = new Font(
fontFamily,
12,
FontStyle.Regular,
GraphicsUnit.Point);
Rectangle rect = new Rectangle(10, 10, 450, 100);
StringFormat stringFormat = new StringFormat();
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));
float[] tabs = {150, 100, 100, 100};
stringFormat.SetTabStops(0, tabs);
e.Graphics.DrawString(text, font, solidBrush, rect, stringFormat);
Pen pen = Pens.Black;
e.Graphics.DrawRectangle(pen, rect);
下圖顯示的是索引標籤式文字。
上述程式碼會將兩個引數傳遞至 SetTabStops 方法。第二個引數是包含定位點位移 (Offset) 的陣列。傳遞至 SetTabStops 第一個引數是 0,表示陣列中的第一個位移是從週框左邊緣的位置 0 開始測量。
繪製垂直文字
您可以使用 StringFormat 物件指定垂直繪製文字,而非水平繪製。
下列範例會將 DirectionVertical 值指派給 StringFormat 物件的 FormatFlags 屬性。然後會再將這個 StringFormat 物件傳遞至 Graphics 類別的 DrawString 方法。DirectionVertical 值是 StringFormatFlags 列舉型別的成員。
Dim myText As String = "Vertical text"
Dim fontFamily As New FontFamily("Lucida Console")
Dim font As New Font( _
fontFamily, _
14, _
FontStyle.Regular, _
GraphicsUnit.Point)
Dim pointF As New PointF(40, 10)
Dim stringFormat As New StringFormat()
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
stringFormat.FormatFlags = StringFormatFlags.DirectionVertical
e.Graphics.DrawString(myText, font, solidBrush, pointF, stringFormat)
[C#]
string text = "Vertical text";
FontFamily fontFamily = new FontFamily("Lucida Console");
Font font = new Font(
fontFamily,
14,
FontStyle.Regular,
GraphicsUnit.Point);
PointF pointF = new PointF(40, 10);
StringFormat stringFormat = new StringFormat();
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));
stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
e.Graphics.DrawString(text, font, solidBrush, pointF, stringFormat);
下圖顯示的是垂直文字。