VB.NET2019において、PictureBoxに縦書きで文字を書くコードを作成しています。 '指定座標に一文字描画 Sub DrawChar(ByVal drawChar As String, ByVal font As Font, ByVal pt As Point, ByVal g As Graphics)
Dim ZenkakuStf As New StringFormat(StringFormatFlags.DirectionVertical) Dim brush1 As Brush = New SolidBrush(ForeColorLabel.ForeColor)
g.DrawString(drawChar, font, brush1, pt, ZenkakuStf) End Sub
PictureBoxに縦書きで1文字表示する、こんな感じのメソッドを作成しました。 ひらがな、カタカナ、漢字等は問題なく表示されます。 ここで問題なのが"−"という文字で"|"のように縦線で表示したいのですが、"−"のように横線で表示されてしまいます。 If (drawChar = "−") Then g.DrawString(drawChar, CharFont, brush1, pt) Else g.DrawString(drawChar, CharFont, brush1, pt, ZenkakuStf) End If のように"−"だけ縦書き指定を外してみましたが、やはり"−"になってしまいます。 縦線に表示する方法はないでしょうか。
■No35041に返信(キウイさんの記事)
> ここで問題なのが"−"という文字で"|"のように縦線で表示したいのですが、"−"のように横線で表示されてしまいます。
以下のようなコードを使って試してみましたが (VS 2022)、
・.NET Framework 4.8
・.NET 6
・.NET 5
・.NET Core 3.1
のすべてにおいて、縦書きで正しく表示されました。
特殊なフォントを指定したりしていないでしょうか。
--- コード ---
Private Const Message As String = "初めて投稿される方は、必ず「書き込みのルールについて(12/4/2更新)」をお読みください。"
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
Dim format As New StringFormat(StringFormatFlags.DirectionVertical)
Using font As New Font("遊ゴシック", 15.0)
e.Graphics.DrawString(Message, font, Brushes.Black, 10.0, 10.0, format)
e.Graphics.DrawString(Message, font, Brushes.Black, 40.0, 40.0)
End Using
End Sub
--- コード ---
■No35043に返信(YuOさんの記事) > ■No35041に返信(キウイさんの記事) >>ここで問題なのが"−"という文字で"|"のように縦線で表示したいのですが、"−"のように横線で表示されてしまいます。 > > 以下のようなコードを使って試してみましたが (VS 2022)、 > ・.NET Framework 4.8 > ・.NET 6 > ・.NET 5 > ・.NET Core 3.1 > のすべてにおいて、縦書きで正しく表示されました。 > 特殊なフォントを指定したりしていないでしょうか。 > > --- コード --- > Private Const Message As String = "初めて投稿される方は、必ず「書き込みのルールについて(12/4/2更新)」をお読みください。" > > Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint > Dim format As New StringFormat(StringFormatFlags.DirectionVertical) > Using font As New Font("遊ゴシック", 15.0) > e.Graphics.DrawString(Message, font, Brushes.Black, 10.0, 10.0, format) > e.Graphics.DrawString(Message, font, Brushes.Black, 40.0, 40.0) > End Using > End Sub > --- コード --- > 指定のフォントはデフォルト値である「MS ゴシック」「14.25」です。
If (drawChar = "−") Then Dim format As New StringFormat(StringFormatFlags.DirectionVertical) Using font1 As New Font("遊ゴシック", 14.0) g.DrawString("−", font1, Brushes.Black, 10.0, 10.0, format) g.DrawString("−", font1, Brushes.Black, 40.0, 40.0) End Using
'g.DrawString("|", CharFont, brush1, pt) Else g.DrawString(drawChar, CharFont, brush1, pt, ZenkakuStf) End If ↑のようにしてみましたが、表示されるのは横線でした。
OSバージョンは ------------------------------------------------- エディション Windows 10 Home バージョン 21H2 インストール日 ‎2022/‎01/‎05 OS ビルド 19044.1645 エクスペリエンス Windows Feature Experience Pack 120.2212.4170.0 -------------------------------------------------
以下のコードで、 Dim CharFont As Font Dim fontFam As FontFamily Dim ZenkakuStf As New StringFormat(StringFormatFlags.DirectionVertical)
fontFam = font.FontFamily CharFont = New Font(fontFam, 14)
Dim brush1 As Brush = New SolidBrush(ForeColorLabel.ForeColor)
参考情報として、VerticalOrientation.txt を紹介しておきます。 UAX #50 (Unicode Vertical Text Layout) https://www.unicode.org/Public/UCD/latest/ucd/VerticalOrientation.txt
FF0D '−' や 002D '-' は「R」で、 30FC 'ー' は「Tr」だそうな。
# U - Upright, the same orientation as in the code charts # R - Rotated 90 degrees clockwise compared to the code charts # Tu - Transformed typographically, with fallback to Upright # Tr - Transformed typographically, with fallback to Rotated
Option Strict On Public Class Form1 Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint Const LANGID_JA_JP As Integer = &H411 'NoFontFallback は、非対応の文字を代替フォントではなく豆腐文字で描画するための指定 Dim vertFmt As New StringFormat(StringFormatFlags.DirectionVertical Or StringFormatFlags.NoFontFallback, LANGID_JA_JP) Dim horiFmt As New StringFormat(StringFormatFlags.NoFontFallback, LANGID_JA_JP) Dim charcters = ChrW(&H201C) & ChrW(&HFF0D) & ChrW(&H2026) _ & ChrW(&H300C) & ChrW(&H300D) & ChrW(&H2D) & ChrW(&H30FC) _ & ChrW(&HFE31) & ChrW(&HFE32) & ChrW(&HFE33) _ & ChrW(&H7C) & ChrW(&HFF5C) & ChrW(&H201D) Dim fontNames = {"MS ゴシック", "メイリオ", "游ゴシック", "NSimSun", "Microsoft JhengHei"} e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias For x = 0 To fontNames.Length - 1 'Dim fontName = $"@{fontNames(x)}" Dim fontName = fontNames(x) Using charFont As New Font(fontName, 14.0F) Dim pt1 As New Point(15 + x * 65, 15) e.Graphics.DrawString($"{charcters}//{fontName}", charFont, Brushes.MediumBlue, pt1, vertFmt) Dim pt2 As New Point(350, 15 + x * 45) e.Graphics.DrawString($"{charcters}//{fontName}", charFont, Brushes.Crimson, pt2, horiFmt) End Using Next End Sub End Class