Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ANSI Escape characters & Rich Text Boxes

Status
Not open for further replies.

rjr9999

Programmer
Apr 7, 2001
183
US
Ok, quick question here, before I cause myself some major headaches. Is there any current function I can use to recognize ANSI Escape Secuences within a text and/or rich text box? eg. [31m = the color red...Or am I going to have to write myself a new control or module? Any help is appriciated, as usual ;)

Thanks,
Rob
 
I'm working on wrinting one right now. It's looking pretty ugly though because of the lack of a common closing character (sometimes there is a *, mostly not though). If you find something out please let me know. If I manage to develop a decent parser for ansi escape codes I'll e-mail you.

Glad I'm not the only one losing sleep over this B-)
 
This is something very simple I threw together to test perfect macros for a MUD a friend of mine made (time based combat). I've managed to quickly and efficiently remove ansi color codes, however, instructing a text box or rich text box to display the color is a VERY slow process..I'm working with DirectX right now (in VC++) to attemt to create a control that can more efficiently make color changes. This should be a good starting point though. Don't mind that it's sloppy, I just threw it together to make a point to this friend of mine ;). Oh, another problem I've been running into is efficiently clipping the excess text in the text box. This is also something I'll be trying to make more efficient with my new control.

Code:
Option Explicit

'
' 32-Bit Windows functions needed
Private Declare Function ArrPtr& Lib "msvbvm60.dll" Alias "VarPtr" (ptr() As Any)
Private Declare Sub RtlMoveMemory Lib "kernel32" (dst As Any, src As Any, ByVal nBytes&)

Private SafeArrayHeader(5) As Long      ' Header for the SafeArray Map
Private Map() As Integer                ' Maps onto the Text1's string

Dim scroll_total As Long
Dim buf1 As String
Dim parry_timer As Long
Dim parry_type As Integer
Dim last_sent As String
Dim current_sent As String
Dim timed_read As Long

Private Sub Command1_Click()
    Winsock1.Connect
End Sub

Private Sub Command2_Click()
    Text1.Text = ""
    scroll_total = 0
End Sub

Private Sub Form_Load()
    ' Set up the SafeArrayHeader
    SafeArrayHeader(0) = 1              ' Number of dimensions
    SafeArrayHeader(1) = 2              ' Bytes per element (integer = 2)
    ' SafeArrayHeader(3) = StrPtr(Text1)  ' Pointer to the 1st character
    SafeArrayHeader(4) = &H7FFFFFFF     ' Array size
  
    ' Force Map to use SafeArrayHeader as its own header
    RtlMoveMemory ByVal ArrPtr(Map), VarPtr(SafeArrayHeader(0)), 4
    
    scroll_total = 0
    parry_timer = 0
End Sub

Private Sub Form_Unload(Cancel As Integer)
    ' Clean up our mess, do not prevent this from executing!
    RtlMoveMemory ByVal ArrPtr(Map), 0&, 4
End Sub

Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyDelete Then
        Text2.Text = ""
    End If
    If KeyCode = vbKeyUp Then
        Winsock1.SendData "fly 5 north" + Chr(13) + Chr(10)
    End If
    If KeyCode = vbKeyDown Then
        Winsock1.SendData "fly 5 south" + Chr(13) + Chr(10)
    End If
    If KeyCode = vbKeyLeft Then
        Winsock1.SendData "fly 5 west" + Chr(13) + Chr(10)
    End If
    If KeyCode = vbKeyRight Then
        Winsock1.SendData "fly 5 east" + Chr(13) + Chr(10)
    End If
    If KeyCode = vbKeyPageUp Then
        current_sent = Text2.Text
        Text2.Text = last_sent
    End If
    If KeyCode = vbKeyPageDown Then
        Text2.Text = current_sent
    End If
End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
        Winsock1.SendData Text2.Text + Chr(13) + Chr(10)
        last_sent = Text2.Text
        Text2.Text = ""
    End If
End Sub

Private Sub Timer1_Timer()
 parry_timer = parry_timer + 1
 If parry_timer >= 159 Then
    If parry_type = 1 Then
        Winsock1.SendData "parry high" + Chr(13) + Chr(10)
        parry_timer = 0
        Timer1.Enabled = False
    End If
    If parry_type = 2 Then
        Winsock1.SendData "parry low" + Chr(13) + Chr(10)
        parry_timer = 0
        Timer1.Enabled = False
    End If
 End If
End Sub

Private Sub Timer2_Timer()
timed_read = timed_read + 1
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim i As Long
    Dim temp As String
    Dim j As Long
    
    Winsock1.GetData temp
    
    ' Make sure Map is pointing to the correct memory location
    SafeArrayHeader(3) = StrPtr(temp)
    
    timed_read = 0
    Timer2.Enabled = True
    For i = LBound(Map) To Len(temp)
        If Map(i) <> vbNull Then
            If Map(i) = 10 Then
                i = i + 1
                Text1.Text = Text1.Text + Chr(13) + Chr(10)
            ElseIf Map(i) = 27 Then
                i = i + 3
                If Map(i) <> vbNull Then
                    If Chr(Map(i)) = &quot;;&quot; Then
                        i = i + 3
                    End If
                End If
            Else
                Text1.Text = Right(Text1.Text, 1000)
                If Map(i) <> vbNull Then
                    If Chr(Map(i)) = &quot;*&quot; Then
                        If Chr(Map(i + 1)) = &quot; &quot; Then
                            j = 2
                            While Chr(Map(i + j)) <> &quot; &quot;
                                j = j + 1
                            Wend
                            If Chr(Map(i + j + 1)) = &quot;r&quot; Then
                                If (Chr(Map(i + j + 14)) = &quot;l&quot;) Or (Chr(Map(i + j + 14)) = &quot;r&quot;) Then
                                    parry_type = 1
                                    parry_timer = 0 + timed_read
                                    Timer2.Enabled = False
                                    timed_read = 0
                                    Timer1.Enabled = True
                                End If
                            End If
                            If Chr(Map(i + j + 1)) = &quot;b&quot; Then
                                If (Chr(Map(i + j + 10)) = &quot;h&quot;) Then
                                    parry_type = 1
                                    parry_timer = 0 + timed_read
                                    Timer2.Enabled = False
                                    timed_read = 0
                                    Timer1.Enabled = True
                                End If
                                If (Chr(Map(i + j + 10)) = &quot;l&quot;) Then
                                    parry_type = 2
                                    parry_timer = 0 + timed_read
                                    Timer2.Enabled = False
                                    timed_read = 0
                                    Timer1.Enabled = True
                                End If
                            End If
                        End If
                    End If
                    Text1.Text = Text1.Text + Chr(Map(i))
                End If
            End If
        End If
    Next i
    scroll_total = scroll_total + i
    Form1.Caption = scroll_total
    Text1.SelStart = 65000
End Sub
Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;

- Currently down
 
Last night i ran across entry's in the MS Knowledge Base and found numerous API calls that can be made via the windows usre32.dll. Among these are calls for determining and controlling cursor movement inside the textbox. I think that these calls will help you control line length and help things on a bit. The search I did was: howto api, in the MSDN July 2001 CD set. Q186271 is the number for the howto that I refer to HOWTO: Manipulate Text Box Contents. If you like I can e-mail it to you or something if for some reason you can't find it. There are also a few websites that I found that might also be of help, for one.

I'm writing a telnet/tn3270 emulator right now and it's all I can do to remain sane right now, so any help is appreciated. I just about have the telnet side working right now aside of emulating the ANSI escape sequences properly right now in the textbox object. If you would like a preview of the code I'm writing give me a holler sometime.
 
>rich text box to display the color is a VERY slow process

Bit of a sweeping statement...
 
So what is the best option for displaying text in VB? I'm open to suggestions.




Make something that even an idiot can use, and only an idiot will want to use it. - Murphy B-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top