如何:在已繪製的文字中設定定位停駐點
.NET Framework 4.5
您可以經由呼叫 StringFormat 物件的 SetTabStops 方法,然後將該 StringFormat 物件傳遞至 Graphics 類別的 DrawString 方法,以設定文字的定位停駐點。
注意事項 |
---|
System.Windows.Forms.TextRenderer 不支援將定位停駐點加入至繪製的文字,但您可以使用 TextFormatFlags.ExpandTabs 旗標擴充現有的定位停駐點。
|
下圖顯示的是索引標籤式文字。
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)
- 前述範例是專為與 Windows Form 搭配使用而設計的,而且它需要有 PaintEventArgs e (這是 PaintEventHandler 的參數)。
沒有留言:
張貼留言