Jump to content

Colored Output on CMD


 Share

Recommended Posts

Hi there,

is there any possibility to color lines in a DOS box (not with color.exe)?

Like ConsoleWrite("test", $color)?

Any Help would be appreciated!

Thanks,

Hannes

:graduated:

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Yes... It is. And thanks for reminding me.

You use the windows api, which has a lot of functions for use with the console. To see the most basic usage, see my Au3Int (All the console functions are at the bottom).

Console Functions (MSDN): http://msdn.microsoft.com/en-us/library/ms682073.aspx

FillConsoleOutputAttribute: http://msdn.microsoft.com/en-us/library/ms682662.aspx

I can't give you examples or code yet, as it's all at home.

hConsoleOutput: Use GetStdHandle with a value of -11 (off the top of my head)

Edited by Mat
Link to comment
Share on other sites

If you are going for a single different output color you can just change

--HKCU\Software\Microsoft\Command Processor

--DefaultColor

using the same hex values afforded to color.exe

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

sending the color command changes that window and only that window on the fly, my solution will not change on the fly, but will however alter the default color with which all future command windows open..

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

The effect is the same though. Furthermore, if he wanted to use your method he'd have to go through the process of writing before making the prompt.

And finally, I have a funny feeling that yours applies to all consoles. Not that I've tried though. I didn't read your post fully, my bad.

Here is the function:

; #FUNCTION# ====================================================================================================================
; Name...........: _Console_FillOutputAttribute
; Description ...: Sets the character attributes for a specified number of character cells, beginning at the specified
;                  coordinates in a screen buffer.
; Syntax.........: _Console_FillOutputAttribute($hConsoleOutput, $iAttribute, $nLength, $iX, $iY [, $hDll ] )
; Parameters ....: $hConsoleOutput      - A handle to the console screen buffer. The handle must have the GENERIC_WRITE access
;                                         right.
;                  $iAttribute          - The attributes to use when writing to the console screen buffer.
;                                       |FOREGROUND_BLUE - Text color contains blue.
;                                       |FOREGROUND_GREEN - Text color contains green.
;                                       |FOREGROUND_RED - Text color contains red.
;                                       |FOREGROUND_INTENSITY - Text color is intensified.
;                                       |BACKGROUND_BLUE - Background color contains blue.
;                                       |BACKGROUND_GREEN - Background color contains green.
;                                       |BACKGROUND_RED - Background color contains red.
;                                       |BACKGROUND_INTENSITY - Background color is intensified.
;                                       |COMMON_LVB_LEADING_BYTE - Leading byte.
;                                       |COMMON_LVB_TRAILING_BYTE - Trailing byte.
;                                       |COMMON_LVB_GRID_HORIZONTAL - Top horizontal.
;                                       |COMMON_LVB_GRID_LVERTICAL - Left vertical.
;                                       |COMMON_LVB_GRID_RVERTICAL - Right vertical.
;                                       |COMMON_LVB_REVERSE_VIDEO - Reverse foreground and background attributes.
;                                       |COMMON_LVB_UNDERSCORE - Underscore.
;                  $nLength             - The number of character cells to be set to the specified color attributes.
;                  $iX                  - The X coordinate that specifies the character coordinates of the first
;                                         cell whose attributes are to be set.
;                  $iY                  - The Y coordinate that specifies the character coordinates of the first
;                                         cell whose attributes are to be set.
;                  $hDll                - A handle to a dll to use. This prevents constant opening of the dll which could slow it
;                                         down. If you are calling lots of functions from the same dll then this recommended.
; Return values .: Success              - True, the number of character cells whose attributes were actually set is returned in
;                                         @extended.
;                  Failure              - False
; Author ........: Matt Diesel (Mat)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........: http://msdn.microsoft.com/en-us/library/ms682662.aspx
; Example .......: No
; ===============================================================================================================================
Func _Console_FillOutputAttribute($hConsoleOutput, $iAttribute, $nLength, $iX, $iY, $hDll = -1)
    Local $aResult, $tCOORD

    If $hDll = -1 Then $hDll = $__gvKernel32
    If $hConsoleOutput = -1 Then $hConsoleOutput = _Console_GetStdHandle($STD_OUTPUT_HANDLE, $hDll)

    $tCOORD = BitShift($iY, -16) + $iX

    $aResult = DllCall($hDll, "bool", "FillConsoleOutputAttribute", _
            "handle", $hConsoleOutput, _
            "word", $iAttribute, _
            "dword", $nLength, _
            "int", $tCOORD, _
            "dword*", 0)
    If @error Then Return SetError(@error, @extended, False)

    Return SetExtended($aResult[5], $aResult[0] <> 0)
EndFunc   ;==>_Console_FillOutputAttribute
Edited by Mat
Link to comment
Share on other sites

I would like to try your script but it throws at

_Console_GetStdHandle - can you post that function

as well what should $_gvKernel32 be set to?

all apologies if either or both of those are retarded queries.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

I would like to try your script but it throws at

_Console_GetStdHandle - can you post that function

as well what should $_gvKernel32 be set to?

all apologies if either or both of those are retarded queries.

No worries... $_gvKernel32 is "Kernel32.dll" by default, but I usually set it to DllOpen("Kernel32.dll").

; #FUNCTION# ====================================================================================================================
; Name...........: _Console_GetStdHandle
; Description ...: Retrieves a handle for the standard input, standard output, or standard error device.
; Syntax.........: _Console_GetStdHandle( [ $nStdHandle [, $hDll ]] )
; Parameters ....: $nStdHandle          - The standard device. Default is STD_OUTPUT_HANDLE. This parameter can be one of the
;                                         following values:
;                                       |STD_INPUT_HANDLE - The standard input device. Initially, this is the console input
;                                                           buffer, CONIN$.
;                                       |STD_OUTPUT_HANDLE - The standard output device. Initially, this is the active console
;                                                            screen buffer, CONOUT$.
;                                       |STD_ERROR_HANDLE - The standard error device. Initially, this is the active console
;                                                           screen buffer, CONOUT$.
;                  $hDll                - A handle to a dll to use. This prevents constant opening of the dll which could slow it
;                                         down. If you are calling lots of functions from the same dll then this recommended.
; Return values .: Success              - A handle to the specified device, or a redirected handle set by a previous call to
;                                         _Console_SetStdHandle. The handle has GENERIC_READ and GENERIC_WRITE access rights,
;                                         unless the application has used SetStdHandle to set a standard handle with lesser
;                                         access.
;                  Failure              - zero
; Author ........: Matt Diesel (Mat)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........: http://msdn.microsoft.com/en-us/library/ms683231.aspx
; Example .......: No
; ===============================================================================================================================
Func _Console_GetStdHandle($nStdHandle = -11, $hDll = -1)
    Local $aResult

    If $hDll = -1 Then $hDll = $__gvKernel32

    $aResult = DllCall($hDll, "handle", "GetStdHandle", "dword", $nStdHandle)
    If @error Then Return SetError(@error, @extended, 0)

    Return $aResult[0]
EndFunc   ;==>_Console_GetStdHandle

But I would download it below.

I really should post the full thing, but it's no where near completion. What I'm going to do is post it in test form for people to use. But not post it on the forum just yet. Maybe I'll let other people submit code changes, as this is not the first time someones needed it.

http://code.google.com/p/consoleau3/

You can download everything from there. I'll be updating it more thoroughly soon. If you want to help out then just pm me your google account username and I'll add you. You can then edit pages and upload new version.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...