MrKris1224

Change console colors table.

4 posts in this topic

#1 ·  Posted (edited)

Hi, i want to change colors table in console for my own RGB colors. I modify some functions from @Mat's console UDF.

It's look like this:

 

Global Const $tagCONSOLE_SCREEN_BUFFER_INFOEX = "ULONG Size; SHORT SizeX; SHORT SizeY;SHORT CursorPositionX;" & _
        "SHORT CursorPositionY; SHORT Attributes; SHORT Left; SHORT Top; SHORT Right; SHORT Bottom;" & _
        "SHORT MaximumWindowSizeX; SHORT MaximumWindowSizeY; WORD PopupAttributes; BOOL FullscreenSupported; DWORD ColorTable[16];"

$vKernel = DllOpen(@SystemDir & "\Kernel32.dll")
$CONSOLE_OUTPUT = _Console_GetStdHandle(-11)

Func _Console_Colors()
    Local $ColorTable[16] = [0xFF0000, 0xFF0000, 0xFF0000, 0xFF0000, _
    0xFF0000, 0xFF0000, 0xFF0000, 0xFF0000, _
    0xFF0000, 0xFF0000, 0xFF0000, 0xFF0000, _
    0xFF0000, 0xFF0000, 0xFF0000, 0xFF0000]
    Local $tConsoleScreenBufferInfoEx = DllStructCreate($tagCONSOLE_SCREEN_BUFFER_INFOEX)
    DllCall($vKernel, "bool", "GetConsoleScreenBufferInfoEx", "handle", $CONSOLE_OUTPUT, "ptr", DllStructGetPtr($tConsoleScreenBufferInfoEx))
    DllStructSetData($tConsoleScreenBufferInfoEx, "ColorTable[16]", $ColorTable)
    DllCall($vKernel, "bool", "SetConsoleScreenBufferInfoEx", "handle", $CONSOLE_OUTPUT, "ptr", DllStructGetPtr($tConsoleScreenBufferInfoEx))
EndFunc

And some other shi... code.

But when i use this function then write something in console colors are same as before.

What's wrong in this code?

Edited by MrKris1224

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Characters don't use just the colour table, each character also has attributes. By default these are set to be white text on black background (see here). 

I wrote a function to set the colour table, and another to set attributes. Try playing around with combinations. With setting the colortable, everything is blue (as you'd expect with all the colours being the same).

#include <Array.au3>
#include '..\Console.au3'

_Console_Alloc()

Local $ColorTable[16] = [0xFFFFFF, 0xFF0000, 0xFF0000, 0xFF0000, _
        0xFF0000, 0xFF0000, 0xFF0000, 0xFF0000, _
        0xFF0000, 0xFF0000, 0xFF0000, 0xFF0000, _
        0xFF0000, 0xFF0000, 0xFF0000, 0xFF0000]


$aOldColors = _Console_GetScreenBufferColorTable()
;~ _ArrayDisplay($aOldColors, 0xFF0000)

;~ $fSuccess = _Console_SetScreenBufferColorTable(-1, $ColorTable)
;~ If Not $fSuccess Then MsgBox(0, "Error (ColorTable)", @error & @CRLF & @extended)

$fSuccess = _Console_SetScreenBufferAttributes(-1, BitOR($FOREGROUND_BLUE, $FOREGROUND_GREEN, $FOREGROUND_INTENSITY, $BACKGROUND_BLUE))
If Not $fSuccess Then MsgBox(0, "Error (Attributes)", @error & @CRLF & @extended)

;~ $aNewColors = _Console_GetScreenBufferColorTable()
;~ _ArrayDisplay($aNewColors, 0xFF0000)

_Console_SetTitle("Hey look =P")

_Console_Write("Hello, World!" & @LF)

_Console_Pause()
_Console_SetScreenBufferColorTable(-1, $aOldColors)
_Console_Free()



Func _Console_SetScreenBufferAttributes($hConsoleOutput, $iAttributes, $hDll = -1)
    If $hConsoleOutput = -1 Then $hConsoleOutput = _Console_GetStdHandle($STD_OUTPUT_HANDLE, $hDll)
    If $iAttributes = Default Then $iAttributes = 0

    ; Get current buffer info
    $tConsoleScreenBufferInfoEx = _Console_GetScreenBufferInfoEx($hConsoleOutput, $hDll)
    If @error Then Return SetError(1, @error, False)

    ; Modify attributes
    $tConsoleScreenBufferInfoEx.Attributes = $iAttributes

    ; Set new buffer info
    $fSuccess = _Console_SetScreenBufferInfoExEx($hConsoleOutput, DllStructGetPtr($tConsoleScreenBufferInfoEx), $hDll)
    If Not $fSuccess Then Return SetError(2, @error, False)

    Return True
EndFunc   ;==>_Console_SetScreenBufferAttributes

Func _Console_SetScreenBufferColorTable($hConsoleOutput, $aiColorTable, $hDll = -1)
    If $hConsoleOutput = -1 Then $hConsoleOutput = _Console_GetStdHandle($STD_OUTPUT_HANDLE, $hDll)
    If Not IsArray($aiColorTable) Then Return False
    If UBound($aiColorTable, 0) <> 1 Then Return False
    If UBound($aiColorTable, 1) < 16 Then Return False

    ; Get current buffer info
    $tConsoleScreenBufferInfoEx = _Console_GetScreenBufferInfoEx($hConsoleOutput, $hDll)
    If @error Then Return SetError(1, @error, False)

    $tConsoleScreenBufferInfoEx.Attributes = BitOR($FOREGROUND_RED, $FOREGROUND_BLUE, $FOREGROUND_GREEN, $BACKGROUND_RED, $BACKGROUND_BLUE, $BACKGROUND_GREEN)

    ; Modify color table
    For $i = 0 To 15
        DllStructSetData($tConsoleScreenBufferInfoEx, "ColorTable", $aiColorTable[$i], $i + 1)
    Next

    ; Set new buffer info
    $fSuccess = _Console_SetScreenBufferInfoExEx($hConsoleOutput, DllStructGetPtr($tConsoleScreenBufferInfoEx), $hDll)
    If Not $fSuccess Then Return SetError(2, @error, False)

    Return True
EndFunc   ;==>_Console_SetScreenBufferColorTable

Regards,

Mat

 

To clarify since it's not apparent from MSDN, the index in the colour table is with the bits of the attribute in the order IBGR.

Edited by Mat
Added link between attributes and the colour table.
1 person likes this

Share this post


Link to post
Share on other sites

I use registry:

[HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe]
"ScreenColors"=dword:000000f0
"PopupColors"=dword:000000fc
"NumberOfHistoryBuffers"=dword:0000000a
"ScreenBufferSize"=dword:0066005a
"WindowSize"=dword:001e005a
"HistoryBufferSize"=dword:00000064
"FontSize"=dword:000c0008
"FontFamily"=dword:00000030
"FontWeight"=dword:00000190

 


Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

 

Share this post


Link to post
Share on other sites

Thanks bro :) It works perfect :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now