HOW to set background and text colour using masm32?ANY suggestion?

Started by goo5257, September 11, 2008, 01:41:28 AM

Previous topic - Next topic

goo5257

Before i m using masm615, i jz need to use the interrupt 10h to set the colour.
But, it is totally different for masm32...
Can any1 help me?

GregL

goo5257,

To set the console colors use SetConsoleTextAttribute. To get the current colors use GetConsoleScreenBufferInfo and look at the returned CONSOLE_SCREEN_BUFFER_INFO.wAttributes.


goo5257

Em...
I am afraid that i do not understand at all about the function...
Is it a macro?
can you please explain deeply?
I am new to this...


BlackVortex

Quote from: goo5257 on September 11, 2008, 01:46:50 PM
Em...
I am afraid that i do not understand at all about the function...
Is it a macro?
can you please explain deeply?
I am new to this...


They are APIs ... you invoke them. Check those links for the parameters.

Neil

Click on the links that greg has posted & read it up.

Basically the API works like this   :-  invoke SetTextColor,14,0

The first parameter sets the text colour & the second the background colour, if you use the example I have posted you will get yellow text on a black background. You have a choice of 16 colours for text & background ranging from 0 (black) to 15 (bright white). Try using it with different parameters.


hutch--

goo,

You will need to tell us first what type of Window it is and how you are displaying the text.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Neil

Didn't have my head in gear on my last post, for that to work you need this proc in your code, this is for a console:-


SetTextColor proc fore:DWORD,back:DWORD

    LOCAL hStdOut:DWORD

    invoke GetStdHandle,STD_OUTPUT_HANDLE
    mov   hStdOut,eax
    mov   eax,back
    shl   eax,4
    or    eax,fore
    invoke SetConsoleTextAttribute,hStdOut,eax
    ret
   
SetTextColor endp

goo5257

I jz want to colour certain part of the text with different colour.
And, certain part of background with colour...

I dun understand at all for the console...
Before i use masm615, i jz need to use int 10h den can do all those thg d..
But, now seems like i need to noe more about its macros,function, and all those lib that have been created...
Is there got any appendix for me to refered?


goo5257

Hey...
That's work...Thanks Neil...
Be4 u dint post d PROC for me..
So, it dint work.
Now it is work...
Thanks a lot...
But, Can i set d colour according to d cursor that i had set?
As i cant set the cursor means which word that i set tis colour and which word that i set another colour?

hutch--

goo,

You will have to learn a bit more about win32 code before you can do most of these things. You set a normal window background colour in the WNDCLASSEX structure when you register a window class. The system controlled colours are not normally changed by a windows code so if you want non-system colours in display, you have to control the areas yourself. You can draw text in any colour you like with the API DrawText() but you will need to know how to set it up first.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

goo5257

Can you guys give me the some reference for me to study all those things?

MichaelW

Setting the background and text color for a win32 console app is much like doing it from a 16-bit DOS app using the interrupt 10h functions. The main differences are the use of 32-bit code instead of 16-bit code, and in the functions used.

The SetTextColor procedure that Neil posted sets up and calls the SetConsoleTextAttribute function. It sets the attributes of the characters written to the console screen buffer by WriteFile, WriteConsole, etc. It works somewhat like a BASIC COLOR statement, if you are familiar with that. The colors are normal 4-bit IRGB character mode attributes where:
bit3 = intensity
bit2 = red
bit1 = green
bit0 = blue
The background attribute is stored in the high-order 4 bits of the attribute byte, and the foreground attribute in the low-order 4 bits. Here is a simple example that should be built as a console app. Note that the print macro actually uses the WriteFile function to generate the output. Note also that there is a loc macro that uses the SetConsoleCursorPosition function to set the console cursor position.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
SetTextColor proc fore:DWORD,back:DWORD

    LOCAL hStdOut:DWORD

    invoke GetStdHandle,STD_OUTPUT_HANDLE
    mov   hStdOut,eax
    mov   eax,back
    shl   eax,4
    or    eax,fore
    invoke SetConsoleTextAttribute,hStdOut,eax
    ret
   
SetTextColor endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    invoke SetTextColor, 15, 1
    print "XXXXXXXXXXXXXX",13,10
    invoke SetTextColor, 0, 7
    print "XXXXXXXXXXXXXX",13,10
    invoke SetTextColor, 4, 0
    print "XXXXXXXXXXXXXX",13,10,13,10
    invoke SetTextColor, 7, 0
    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


To change the colors of existing characters, you could use the FillConsoleOutputAttribute function.

See Console Functions for more information.
eschew obfuscation

GregL

goo,

Here's a procedure to get the current console colors.


.686P
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE

INCLUDE windows.inc
INCLUDE kernel32.inc

.CODE
;-----------------------------------------------------------
GetConsoleColors PROC pFG:PTR DWORD, pBG:PTR DWORD
    ; If the procedure succeeds, it returns the current colors
    ; in the variables pointed to by pFG and pBG, and returns 0 in eax.
    ;
    ; If the procedure fails it returns the error from the call to
    ; GetConsoleScreenBufferInfo.

    LOCAL CSBI:CONSOLE_SCREEN_BUFFER_INFO

    IFNDEF hStdOut
        .DATA
            hStdOut DWORD 0
        .CODE
    ENDIF

    .IF hStdOut == 0
        ; Get a handle to the STDOUT screen buffer
        INVOKE GetStdHandle, STD_OUTPUT_HANDLE
        mov hStdOut, eax
    .ENDIF

    INVOKE GetConsoleScreenBufferInfo, hStdOut, ADDR CSBI
    .IF eax != 0
        ; get the low nibble
        mov edx, pFG
        mov ax, CSBI.wAttributes
        and al, 0Fh
        movzx ecx, al
        mov [edx], ecx
        ; get the high nibble
        mov edx, pBG
        mov ax, CSBI.wAttributes
        and al, 0F0h
        shr al, 4
        movzx ecx, al
        mov [edx], ecx
        ; set the return
        xor eax, eax
    .ELSE
        ; set the return
        INVOKE GetLastError
    .ENDIF
    ret
GetConsoleColors ENDP
;-----------------------------------------------------------
END



Neil

Michael you got me thinking when you mentioned:-

       To change the colors of existing characters, you could use the FillConsoleOutputAttribute function.

A while ago you gave me a proc to change the attributes of the whole console buffer :-

    SetScreenColor proc fore:DWORD,back:DWORD

    LOCAL hOutPut:DWORD
    LOCAL noc    :DWORD
    LOCAL cnt    :DWORD
    LOCAL sbi    :CONSOLE_SCREEN_BUFFER_INFO

    invoke GetStdHandle,STD_OUTPUT_HANDLE
    mov hOutPut, eax
    invoke GetConsoleScreenBufferInfo,hOutPut,ADDR sbi
    mov eax, sbi.dwSize     ;2 word values returned for screen size
    push ax
    rol eax, 16
    mov cx, ax
    pop ax
    mul cx
    cwde
    mov cnt, eax
    mov   edx,back
    shl   edx,4
    or    edx,fore
    invoke FillConsoleOutputAttribute,hOutPut,edx,cnt,NULL,ADDR noc
    ret

SetScreenColor endp

Now my question is how do I modify it to change only one character position, it looks as though it needs a COORD structure & perhaps do away with LOCAL sbi    :CONSOLE_SCREEN_BUFFER_INFO, but so far I can't get it to work, any suggestions would be appreciated.

Neil

I have done a bit more work on this & come up with this code:-

SetCellColor proc fore:DWORD,back:DWORD,x:DWORD,y:DWORD

    LOCAL hOutPut :DWORD
    LOCAL noc       :DWORD
    LOCAL cnt       :DWORD
    LOCAL x          :DWORD
    LOCAL y          :DWORD
   
    invoke GetStdHandle,STD_OUTPUT_HANDLE
    mov hOutPut, eax
    mov   edx,back
    shl   edx,4
    or    edx,fore
    mov  ecx, x
    mov  eax, y
    shl  eax, 16
    mov  ax, cx
    mov ecx,1
    invoke FillConsoleOutputAttribute,hOutPut,edx,ecx,eax,ADDR noc
    ret

SetcellColor endp

Am I on the right track?