■ 文字列を指定範囲内に中央揃えで印刷したい

[Visual Basic 初心者掲示板] [過去ログの一覧]


永遠の近鉄ファン 2008/01/18(金) 14:20:15 <初心者>
OS:WinXp
環境:VS2005(VB)
印字方法:PrintDocument

表題の件のとおり文字を中央揃えで印字したいです。
具体的に知りたいのは印字する文字の幅を取得方法です。

どうすればよいのでしょうか

Hongliang 2008/01/18(金) 14:52:13
んーと、DrawString で描画しているなら、StringFormat を指定するやつを使えば簡単に中央ぞろえできますが。

永遠の近鉄ファン 2008/01/18(金) 15:04:45 <初心者>
[[解決]]

http://msdn2.microsoft.com/ja-jp/library/332kzs7c(VS.80).aspx
に以下のコードがありましたので使えそうです。
ありがとうございました。

Dim text1 As String = "Use StringFormat and Rectangle objects to" & _
    " center text in a rectangle."
Dim font1 As New Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point)
Try
    Dim rect1 As New Rectangle(10, 10, 130, 140)
    
    ' Create a StringFormat object with the each line of text, and the block
    ' of text centered on the page.
    Dim stringFormat As New StringFormat()
    stringFormat.Alignment = StringAlignment.Center
    stringFormat.LineAlignment = StringAlignment.Center
    
    ' Draw the text and the surrounding rectangle.
    e.Graphics.DrawString(text1, font1, Brushes.Blue, rect1, stringFormat)
    e.Graphics.DrawRectangle(Pens.Black, rect1)
Finally
    font1.Dispose()
End Try

毎週金曜日はポイント最大3倍!さらに4倍のチャンスも!

Programming Library