在VB.NET中自定义文本解析 [英] Custom text parsing in VB.NET

查看:81
本文介绍了在VB.NET中自定义文本解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我很快就会创建一个游戏,我已经为服务器制作了一个配置文件。服务器有一个配置编辑器,允许用户以易于理解的方式查看和编辑配置文件。



它允许使用以下标签:



$ c [r,g,b](更改文字颜色)

$ b(使文字粗体)

$ i(使文字斜体)

$ u(强调文字)

$ s(敲出文本)



解析器通过拉出任何标签,更改文本格式然后绘制它来工作在ListViewDrawSubItem事件中。



例如,

Hi
I'm going to be creating a game soon and I've made a config file for the server. The server has a Configuration editor, which lets the user view and edit the config file in an easy-to-understand way.

It allows for the following tags:

$c[r,g,b] (changes the text colour)
$b (makes the text bold)
$i (makes the text italic)
$u (underlines the text)
$s (strikes the text out)

The parser works by pulling out any tags, changing the text format, then drawing it in the ListViewDrawSubItem event.

For example,

$bWelcome back, $c[128,255,255]$name

变成

Welcome back, <name>

(其中-name-是淡蓝色)。



正如您在图片中看到的那样,任何标签都可以在$ c []之后或单词的开头直接使用,但不能在其他配置中使用。



如果语法正确,我怎么能修改我的代码以允许字符串中任何地方的任意数量的标签?例如,$ c [0,255,0] H $ bel $ il $ so应该变为H el l o



相关代码:



(where -name- is light blue).

As you can see in the images, any tag is allowed directly after $c[] or at the start of a word, but in no other configuration.

How could I modify my code to allow any number of tags anywhere in the string, as long as the syntax is correct? For example, "$c[0,255,0]H$bel$il$so" should become Hello

Relevant code:

Private Sub configListView_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles configListView.DrawSubItem

       If e.Item.Selected Then
           e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), e.Bounds)
       Else
           e.Graphics.FillRectangle(New SolidBrush(configListView.BackColor), e.Bounds)
       End If

       If e.ColumnIndex = 0 Then
           TextRenderer.DrawText(e.Graphics, e.SubItem.Text, New Font(Me.Font, Nothing), New Point(e.Bounds.Left + 3, e.Bounds.Top + 2), configListView.ForeColor)
           Return
       End If

       Dim textToDraw As Dictionary(Of Color, String) = Utils.getColorsFrom(e.SubItem.Text, configListView.ForeColor)

       Dim x As Integer = e.Bounds.Left + 3
       Dim y As Integer = e.Bounds.Top + 2

       For Each d As KeyValuePair(Of Color, String) In textToDraw

           Dim color As Color = d.Key
           Dim text As String = d.Value

           If text = "True" Then
               color = Color.FromArgb(128, 255, 128)
           ElseIf text = "False" Then
               color = Color.FromArgb(255, 128, 128)
           ElseIf Integer.TryParse(text, New Integer) Then
               color = Color.FromArgb(128, 128, 255)
           End If

           Dim font As Font = Me.Font

           If text.StartsWith("$b") Then
               font = New Font(Me.Font, FontStyle.Bold)
               text = text.Substring(2, text.Length - 2)
           ElseIf text.StartsWith("$i") Then
               font = New Font(Me.Font, FontStyle.Italic)
               text = text.Substring(2, text.Length - 2)
           ElseIf text.StartsWith("$u") Then
               font = New Font(Me.Font, FontStyle.Underline)
               text = text.Substring(2, text.Length - 2)
           ElseIf text.StartsWith("$s") Then
               font = New Font(Me.Font, FontStyle.Strikeout)
               text = text.Substring(2, text.Length - 2)
           End If

           text = text.Replace("$name", "<name>")

           TextRenderer.DrawText(e.Graphics, text, font, New Point(x, y), color)

           x += TextRenderer.MeasureText(text, Font).Width - 6

       Next

   End Sub





Utils.vb





Utils.vb

Shared Function splitByWord(str As String, delimeterWord As String, allowEmpties As Boolean) As String()
        Return str.Split(New String() {delimeterWord}, IIf(allowEmpties, StringSplitOptions.None, StringSplitOptions.RemoveEmptyEntries))
    End Function

    Shared Function countOf(c As String, str As String) As Integer

        Dim count As Integer = 0

        For Each ch In str
            If c = ch Then count += 1
        Next

        Return count

    End Function

    Shared Function getColorsFrom(str As String, defaultColor As Color) As Dictionary(Of Color, String)

        Dim result As New Dictionary(Of Color, String)

        For Each s As String In Utils.splitByWord(str, "$c", False)

            If Not (s.Substring(0, 1) = "[" AndAlso s.Contains("]")) Then
                result.Add(defaultColor, s)
                Continue For
            End If

            Dim rgb As String = s.Substring(1, s.Length - 1).Split("]")(0)

            If Not Utils.countOf(",", rgb) = 2 Then
                result.Add(defaultColor, s)
                Continue For
            End If

            Dim text As String = s.Substring(rgb.Length + 2, s.Length - rgb.Length - 2)
            Dim r As Integer = 0
            Dim g As Integer = 0
            Dim b As Integer = 0

            Try
                r = rgb.Split(",")(0)
                g = rgb.Split(",")(1)
                b = rgb.Split(",")(2)

                result.Add(Color.FromArgb(r, g, b), text)
            Catch
                result.Add(defaultColor, s)
                Continue For
            End Try

        Next

        Return result

    End Function





http://dc773.4shared.com/img/BtucjLtOce/s23/15666a14bf0/scr1 [ ^ ]



http://dc773.4shared.com/img/y_v8KfcVce/s23/15666a14fd8/scr2 [ ^ ]



我尝试了什么:



我试过完善上面的代码没有帮助的几个小时。



http://dc773.4shared.com/img/BtucjLtOce/s23/15666a14bf0/scr1[^]

http://dc773.4shared.com/img/y_v8KfcVce/s23/15666a14fd8/scr2[^]

What I have tried:

I've tried perfecting the above code for hours without help.

推荐答案

c [r,g,b](更改文字颜色)
c[r,g,b] (changes the text colour)


b(使文字变为粗体)
b (makes the text bold)


i(使文本斜体)
i (makes the text italic)


这篇关于在VB.NET中自定义文本解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆