none
ANSI Support for Vb, underscoring lines in console application. RRS feed

  • Question

  • Hello,

    I took a C# code from StackOverflow which was about adding an underscore effect/style to lines. Works perfect in C# but NOT in VB, I guess it is because many ascii/ansi character sets are different. Also I saw a post(on github) regarding the ansi escape codes, which would work in Javascript also, all worked in C# (Tested) thought.

    Here is the code I managed to "translate":


        <DllImport("Kernel32.dll", SetLastError:=True)>
        Public Function GetStdHandle(nStdHandle As IntPtr) As IntPtr
        End Function
    
        <DllImport("kernel32.dll", SetLastError:=True)>
        Private Function GetStdHandle(ByVal nStdHandle As Integer) As IntPtr
        End Function
        <DllImport("kernel32.dll")>
        Private Function GetConsoleMode(ByVal hConsoleHandle As IntPtr, <Out> ByRef lpMode As UInteger) As Boolean
        End Function
        <DllImport("kernel32.dll")>
        Private Function SetConsoleMode(ByVal hConsoleHandle As IntPtr, ByVal dwMode As UInteger) As Boolean
        End Function

    Dim DllHandler As IntPtr = GetStdHandle(-11) Dim Mode As UInteger GetConsoleMode(DllHandler, Mode) Mode = Mode Or 4 SetConsoleMode(DllHandler, Mode) Dim UNDERLINE = "\x1b[4m" Dim RESET = "\x1B[0m" Console.WriteLine("Some " + UNDERLINE + "underlined" + RESET + " text")

    It simply does not work ...

    And here's the original (C#) code (In case it's needed and is acceptable, elsewise I apologize.)

    using System;
    using System.Runtime.InteropServices;
    
    class Program
    {
        const int STD_OUTPUT_HANDLE = -11;
        const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4;
    
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr GetStdHandle(int nStdHandle);
    
        [DllImport("kernel32.dll")]
        static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
    
        [DllImport("kernel32.dll")]
        static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
    
        static void Main()
        {
            var handle = GetStdHandle(STD_OUTPUT_HANDLE);
            uint mode;
            GetConsoleMode(handle, out mode);
            mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
            SetConsoleMode(handle, mode);
    
            const string UNDERLINE = "\x1B[4m";
            const string RESET = "\x1B[0m";
            Console.WriteLine("Some " + UNDERLINE + "underlined" + RESET + " text");
        }
    }

    Thanks, my sincere regards, 4D1 (thEsp) !



    Monday, May 20, 2019 4:21 PM

Answers

  • Use :

    Dim UNDERLINE As String = ChrW(&H1B) + "[4m"
    Dim RESET As String = ChrW(&H1B) + "[0m"

    • Marked as answer by 4D1 (thEsp) Monday, May 20, 2019 5:45 PM
    Monday, May 20, 2019 5:29 PM

All replies

  • Use :

    Dim UNDERLINE As String = ChrW(&H1B) + "[4m"
    Dim RESET As String = ChrW(&H1B) + "[0m"

    • Marked as answer by 4D1 (thEsp) Monday, May 20, 2019 5:45 PM
    Monday, May 20, 2019 5:29 PM
  • Thanks, it can't work any better !!!
    Monday, May 20, 2019 5:45 PM
  • Use :

    Dim UNDERLINE As String = ChrW(&H1B) + "[4m"
    Dim RESET As String = ChrW(&H1B) + "[0m"

    P.S

    If you don't mind... Can you please explain how to use ChrW with a ANSI color escape sequence? For example, \x1B equaled ChrW(&H1B), what \033 would be equal to?
    Thanks.

    Monday, May 20, 2019 7:50 PM
  • I think you're talking about the same value (ESC hexa/octal)

    See for example : Colors In Terminal

    Monday, May 20, 2019 8:24 PM
  • Appreciate it... 

    There's just a tiny problem, if I want to set bold text what values should I use? I did a research and found out that value 1 will change console's text to bold, but with ChrW(&H1B) + "[1" I got same results... Any suggestion?

    Monday, May 20, 2019 8:34 PM
  •  "[1m" is brightness only :

    Monday, May 20, 2019 9:20 PM
  • So these aren't usable in Vb right ?

    ╔══════════╦════════════════════════════════╦═════════════════════════════════════════════════════════════════════════╗
    ║  Code    ║             Effect             ║                                   Note                                  ║
    ╠══════════╬════════════════════════════════╬═════════════════════════════════════════════════════════════════════════╣
    ║ 0        ║  Reset / Normal                ║  all attributes off                                                     ║
    ║ 1        ║  Bold or increased intensity   ║                                                                         ║
    ║ 2        ║  Faint (decreased intensity)   ║  Not widely supported.                                                  ║
    ║ 3        ║  Italic                        ║  Not widely supported. Sometimes treated as inverse.                    ║
    ║ 4        ║  Underline                     ║                                                                         ║
    ║ 5        ║  Slow Blink                    ║  less than 150 per minute                                               ║
    ║ 6        ║  Rapid Blink                   ║  MS-DOS ANSI.SYS; 150+ per minute; not widely supported                 ║
    ║ 7        ║  [[reverse video]]             ║  swap foreground and background colors                                  ║
    ║ 8        ║  Conceal                       ║  Not widely supported.                                                  ║
    ║ 9        ║  Crossed-out                   ║  Characters legible, but marked for deletion.  Not widely supported.    ║
    ║ 10       ║  Primary(default) font         ║                                                                         ║
    ║ 11–19    ║  Alternate font                ║  Select alternate font `n-10`                                           ║
    ║ 20       ║  Fraktur                       ║  hardly ever supported                                                  ║
    ║ 21       ║  Bold off or Double Underline  ║  Bold off not widely supported; double underline hardly ever supported. ║
    ║ 22       ║  Normal color or intensity     ║  Neither bold nor faint                                                 ║
    ║ 23       ║  Not italic, not Fraktur       ║                                                                         ║
    ║ 24       ║  Underline off                 ║  Not singly or doubly underlined                                        ║
    ║ 25       ║  Blink off                     ║                                                                         ║
    ║ 27       ║  Inverse off                   ║                                                                         ║
    ║ 28       ║  Reveal                        ║  conceal off                                                            ║
    ║ 29       ║  Not crossed out               ║                                                                         ║
    ║ 30–37    ║  Set foreground color          ║  See color table below                                                  ║
    ║ 38       ║  Set foreground color          ║  Next arguments are `5;n` or `2;r;g;b`, see below                       ║
    ║ 39       ║  Default foreground color      ║  implementation defined (according to standard)                         ║
    ║ 40–47    ║  Set background color          ║  See color table below                                                  ║
    ║ 48       ║  Set background color          ║  Next arguments are `5;n` or `2;r;g;b`, see below                       ║
    ║ 49       ║  Default background color      ║  implementation defined (according to standard)                         ║
    ║ 51       ║  Framed                        ║                                                                         ║
    ║ 52       ║  Encircled                     ║                                                                         ║
    ║ 53       ║  Overlined                     ║                                                                         ║
    ║ 54       ║  Not framed or encircled       ║                                                                         ║
    ║ 55       ║  Not overlined                 ║                                                                         ║
    ║ 60       ║  ideogram underline            ║  hardly ever supported                                                  ║
    ║ 61       ║  ideogram double underline     ║  hardly ever supported                                                  ║
    ║ 62       ║  ideogram overline             ║  hardly ever supported                                                  ║
    ║ 63       ║  ideogram double overline      ║  hardly ever supported                                                  ║
    ║ 64       ║  ideogram stress marking       ║  hardly ever supported                                                  ║
    ║ 65       ║  ideogram attributes off       ║  reset the effects of all of 60-64                                      ║
    ║ 90–97    ║  Set bright foreground color   ║  aixterm (not in standard)                                              ║
    ║ 100–107  ║  Set bright background color   ║  aixterm (not in standard)                                              ║
    ╚══════════╩════════════════════════════════╩═════════════════════════════════════════════════════════════════════════╝


    Monday, May 20, 2019 9:31 PM
  • Windows codes are at : Console Virtual Terminal Sequences

    (38 works for any RGB color)

    Monday, May 20, 2019 9:35 PM