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

解決


永遠の近鉄ファン  2008-01-18 14:20:15  No: 138654  IP: 192.*.*.*

OS:WinXp
環境:VS2005(VB)
印字方法:PrintDocument

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

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

編集 削除
Hongliang  2008-01-18 14:52:13  No: 138655  IP: 192.*.*.*

んーと、DrawString で描画しているなら、StringFormat を指定するやつを使えば簡単に中央ぞろえできますが。

編集 削除
永遠の近鉄ファン  2008-01-18 15:04:45  No: 138656  IP: 192.*.*.*

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

編集 削除