ANSI Support for Vb, underscoring lines in console application.

  • Thread starter Thread starter 4D1 (thEsp)
  • Start date Start date
4

4D1 (thEsp)

Guest
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) !

Continue reading...
 
Back
Top