Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to output the current color code on the command prompt using batch programming?

I am making a tutorial program for a friend of mine using batching programming. I would like to know if it is possible if there is code I can write in the file that will display the current color code.

Example being the color is currently set to 0A and I want be displayed on the line saying:

echo The color is currently set to 0A.

I want my file to read the code that is set to and display it to help them remember what changes they have made as this is an example program for color codes in the command prompt/batch.

Thank you for your help!

like image 935
Thomas Chidwick Avatar asked Oct 20 '25 04:10

Thomas Chidwick


1 Answers

It is easy to make your own command to do this. Copy both below text files into GetConsoleColour.bat and GetConsoleColour.vb in the same folder. Double click the batch file and it will create GetConsoleColour.exe.

PS Colour is spelt right for my culture. As I'm writing it I don't see any need to use American spelling which in programming you usually have to do.

See https://learn.microsoft.com/en-us/windows/console/getconsolescreenbufferinfo

GetConsoleColour.exe prints the current console colour in hex and returns an errorlevel with the value

To use

C:\PathToFile\GetConsoleColour

I have a program here that sets text color line by line. It is the only technique that will work on all Windows computers.

Command Prompt Scripting: Problem with multiple colors in a batch file.

Also a similar program saying how many processes are in this console window - ListConsole.exe list the processes in the current console and returns an errorlevel saying how many . https://winsourcecode.blogspot.com/2019/05/listconsoleexe-list-processes-in.html


REM GetConsoleColour.bat
REM This file compiles GetConsoleColour.vb to GetConsoleColour.exe
REM GetConsoleColour.exe prints the current console colour and returns an errorlevel with the value
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\GetConsoleColour.exe" "%~dp0\GetConsoleColour.vb" 
pause

Note: There is only 4 lines of code here. The rest is just information the program needs to do those 4 lines. In a big program they would be hidden away in a separate file.

'GetConsoleColour.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module MyApplication 

Public Declare Function GetStdHandle Lib "kernel32" Alias "GetStdHandle" (ByVal nStdHandle As Long) As Long
Public Declare Function SetConsoleTextAttribute Lib "kernel32" Alias "SetConsoleTextAttribute" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
Public Declare Function GetConsoleScreenBufferInfo Lib "kernel32" (ByVal hConsoleOutput As Integer, ByRef lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Integer
Public Const STD_ERROR_HANDLE = -12&
Public Const STD_INPUT_HANDLE = -10&
Public Const STD_OUTPUT_HANDLE = -11&

 <StructLayout(LayoutKind.Sequential)> _
Public Structure COORD
    Public x As Short
    Public y As Short
End Structure

 <StructLayout(LayoutKind.Sequential)> _
Public Structure SMALL_RECT
    Public Left As Short
    Public Top As Short
    Public Right As Short
    Public Bottom As Short
End Structure

 <StructLayout(LayoutKind.Sequential)> _
Public Structure CONSOLE_SCREEN_BUFFER_INFO
    Public dwSize As COORD
    Public dwCursorPosition As COORD
    Public wAttributes As Integer
    Public srWindow As SMALL_RECT
    Public dwMaximumWindowSize As COORD
End Structure 


Sub Main()
    Dim hOut as IntPtr
    Dim Ret as Integer
    Dim CSBI as Console_Screen_Buffer_Info
    hOut  = GetStdHandle(STD_OUTPUT_HANDLE)
    Ret = GetConsoleScreenBufferInfo(hOut, CSBI)
    Console.Writeline(Hex(CSBI.wAttributes))
    Environment.ExitCode = CSBI.wAttributes
End Sub
End Module