VB.net Graphics.DrawString 和 DirectionRightToLeft 混淆了我的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17837257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:22:22  来源:igfitidea点击:

VB.net Graphics.DrawString with DirectionRightToLeft mixes up my string

vb.net

提问by gezzuzz

here is a sample code that isolates my problem.im trying to draw a string from right to left.. if my string starts with numbers then has a comma with a letter it rearranges my string.. but if I write it without the format its fine.. open a new project and add this form to see for yourself.. thank you

这是一个隔离我的问题的示例代码。我试图从右到左绘制一个字符串。如果我的字符串以数字开头,然后有一个带字母的逗号,它会重新排列我的字符串。很好..打开一个新项目并添加此表单以供您自己查看..谢谢

Public Class Form1
    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        Dim myFont As Font = New Font("Zipper", 24, FontStyle.Bold)
        Dim myBrush As Brush = Brushes.Black
        Dim line1 As String = "220516,5130.02,N,00046.34,W,213.8,T,218.0,M,0004.6,N"
        Dim format As StringFormat = New StringFormat(StringFormatFlags.DirectionRightToLeft)
        Me.Width = 1400
        e.Graphics.DrawString(line1, myFont, myBrush, 1300, 0, format)
        e.Graphics.DrawString(line1, myFont, myBrush, 100, 50)
    End Sub
End Class

采纳答案by LarsTech

Try using the Alignment property:

尝试使用 Alignment 属性:

Dim format As New StringFormat
format.Alignment = StringAlignment.Far

You are also not disposing your font object. For that, a simple Using bracket works well:

您也没有处理您的字体对象。为此,一个简单的 Using 括号效果很好:

Using myFont As Font = New Font("Zipper", 24, FontStyle.Bold)
  Dim myBrush As Brush = Brushes.Black
  Dim line1 As String = "220516,5130.02,N,00046.34,W,213.8,T,218.0,M,0004.6,N"
  Dim format As New StringFormat
  format.Alignment = StringAlignment.Far
  Me.Width = 1400
  e.Graphics.DrawString(line1, myFont, myBrush, 1300, 0, format)
  e.Graphics.DrawString(line1, myFont, myBrush, 100, 50)
End Using