- - PR -
文字列の改行について
1
投稿者 | 投稿内容 | ||||
---|---|---|---|---|---|
|
投稿日時: 2008-07-05 18:17
VB2005で帳票を作成しています。
e.Graphics.DrawRectangle(Pens.Black, Rectangle) で四角形を作り Dim Strs As String = Str1 & vbNewLine & Str2 vbNewLine & Str3 e.Graphics.DrawString(Strs, Me.Font, Brushes.Black, Rect, Format) で文字列を描くと ____ | | |Str1 | |Str2 | |Str3 | |____| の様になるコードを書きました。 (線がずれていますが、四角形の中央に文字列1・2・3が改行されて表示されています) ここで質問です。 Str1・Str2・Str3の改行幅を設定する方法もしくは 四角形の高さで均等に文字列を均等に割り付ける方法を探しています。 何かいい方法がありましたら、教えて下さい。 宜しくお願い致します。 | ||||
|
投稿日時: 2008-07-06 11:37
泥臭い方法しか思いつきません。ごめんね。
まず、Graphics#MeasureStrngで文字の高さを決定します。私はよくアルファベットのXを使用します。 次に行数。改行を含む文字列をSplit関数で分解し、配列サイズから求めます。 これで素材はそろいました。等間隔のスペースで描画する場合の余白は、 領域の高さ = 行数 * 文字の高さ + (行数 + 1) * 余白の高さ となるので、ここから余白の高さを求めます。 あとは、Split関数で分解した文字列の配列を回し、余白と文字高さを考慮しながら一行ずつ描画します。 これでそれなりの見栄えにはなるでしょう。きっと。 | ||||
|
投稿日時: 2008-07-07 19:58
かずくん さん
返答ありがとうございます。
上記を参考に文字の高さに Me.Height を使っても出来ました。 ありがとうございました。 Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint Dim rect As New Rectangle(New Point(40, 40), New Size(100, 100)) Dim str() As String = {"str1", "str2", "str3"} e.Graphics.DrawRectangle(Pens.Black, rect) Dim He As Int16 = Rect.Size.Height '四角形の幅 Dim FontHeight As Int16 = Me.Font.Height * Str.Length '文字の高さ Dim YohakuHeight As Int16 = (He - FontHeight) / (str.Length + 1) '文字列の描画 For ii As Int16 = 0 To Str.Length - 1 Select Case ii Case 0 rect.Y += YohakuHeight Case Else rect.Y += (Me.Font.Height) + YohakuHeight End Select e.Graphics.DrawString(str(ii), Me.Font, Brushes.Black, rect, StrFormat(StringAlignment.Near, StringAlignment.Center)) Next End Sub Private Shared _StrFormat As New StringFormat(StringFormatFlags.NoClip) Public Shared ReadOnly Property StrFormat(ByVal LengthPosi As StringAlignment, _ ByVal SidePosi As StringAlignment) As StringFormat Get _StrFormat.LineAlignment = LengthPosi _StrFormat.Alignment = SidePosi Return _StrFormat End Get End Property |
1