Jump to content

_WinAPI_SetConsoleTextAttribute()


Kealper
 Share

Recommended Posts

I noticed that WinAPI.au3 does not come with a UDF for the WinAPI function SetConsoleTextAttribute, which allows a user to change the text color/background for specific pieces of text, which can be quite useful when trying to get a user's attention to an error (e.g. make the line stating the error bright red), or to say something good has happened (e.g. make the line saying the good news bright green). I have set the UDF up so it could (hopefully) be included into WinAPI.au3 to give us all a more complete WinAPI experience. :mellow:

Example script demonstrating this function below. (Lots of comments for such simple code!)

IT MUST BE COMPILED AS A CONSOLE APPLICATION (CUI) TO WORK!

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
If Not @Compiled Then Exit MsgBox(4096, "_WinAPI_SetConsoleTextAttribute", "Please compile this as a console application to see what this function does.") ;Exit if script is not compiled
#include <WinAPI.au3>

$dll = DllOpen("Kernel32.dll") ;Open Kernel32.dll because we will be using it a lot
$hnd = _WinAPI_GetStdHandle(1) ;Get the AutoIt process's standard output handle

For $i = 0 To 255 ;Loop through all possible color combinations
    If $i = 0 Then
        _WinAPI_SetConsoleTextAttribute($hnd, 240, $dll) ;The color used for 0 is black text with black background, set background to white so first line does not look like blank space
        ConsoleWrite("0 - Colors" & @CRLF) ;Write first line with white background
        ContinueLoop ;Skip last two lines in first loop
    EndIf
    _WinAPI_SetConsoleTextAttribute($hnd, $i, $dll) ;Change text color to $i
    ConsoleWrite($i & " - Colors" & @CRLF) ;Write $i so you can see all 256 colors
Next

While 1 ;Hold script in a loop until closed
    Sleep(500)
WEnd

; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_SetConsoleTextAttribute
; Description ...: Changes the text and/or background color of text in the Windows console.
; Syntax.........: _WinAPI_SetConsoleTextAttribute($hConsole, $iColor, $hDLL = "Kernel32.dll")
; Parameters ....: $hConsole    - Handle to the console screen buffer
;                  $iColor      - Color style to apply to the console.
;                  $hDLL        - Handle to the DLL to call. (Leave blank to open Kernel32.dll each call)
; Return values .: Success      - True
;                  Failure      - False
; Author ........: Ken Piper (Kealper)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........: @@MsdnLink@@ SetConsoleTextAttribute
; Example .......:
; ===============================================================================================================================
Func _WinAPI_SetConsoleTextAttribute($hConsole, $iColor, $hDLL = "Kernel32.dll")
    $ret = DllCall($hDLL, "BOOL", "SetConsoleTextAttribute", "HANDLE", $hConsole, "WORD", $iColor)
    If @error Then Return SetError(@error, @extended, False)
    Return $ret[0]
EndFunc

And here is just the function.

; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_SetConsoleTextAttribute
; Description ...: Changes the text and/or background color of text in the Windows console.
; Syntax.........: _WinAPI_SetConsoleTextAttribute($hConsole, $iColor, $hDLL = "Kernel32.dll")
; Parameters ....: $hConsole    - Handle to the console screen buffer
;                  $iColor      - Color style to apply to the console.
;                  $hDLL        - Handle to the DLL to call. (Leave blank to open Kernel32.dll each call)
; Return values .: Success      - True
;                  Failure      - False
; Author ........: Ken Piper (Kealper)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........: @@MsdnLink@@ SetConsoleTextAttribute
; Example .......:
; ===============================================================================================================================
Func _WinAPI_SetConsoleTextAttribute($hConsole, $iColor, $hDLL = "Kernel32.dll")
    $ret = DllCall($hDLL, "BOOL", "SetConsoleTextAttribute", "HANDLE", $hConsole, "WORD", $iColor)
    If @error Then Return SetError(@error, @extended, False)
    Return $ret[0]
EndFunc

I hope this could be of use to other people, as the only other reference to coloring console text was a broken link on another post.

Edited by Kealper
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...