The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: goo5257 on September 11, 2008, 01:41:28 AM

Title: HOW to set background and text colour using masm32?ANY suggestion?
Post by: goo5257 on September 11, 2008, 01:41:28 AM
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?
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: GregL on September 11, 2008, 03:01:08 AM
goo5257,

To set the console colors use SetConsoleTextAttribute (http://msdn.microsoft.com/en-us/library/ms686047(VS.85).aspx). To get the current colors use GetConsoleScreenBufferInfo (http://msdn.microsoft.com/en-us/library/ms683171(VS.85).aspx) and look at the returned CONSOLE_SCREEN_BUFFER_INFO.wAttributes.

Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: 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...

Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: BlackVortex on September 11, 2008, 01:48:07 PM
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.
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: Neil on September 11, 2008, 02:30:48 PM
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.

Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: hutch-- on September 11, 2008, 03:38:33 PM
goo,

You will need to tell us first what type of Window it is and how you are displaying the text.
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: Neil on September 11, 2008, 04:45:11 PM
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
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: goo5257 on September 12, 2008, 12:48:58 AM
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?

Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: goo5257 on September 12, 2008, 01:01:40 AM
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?
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: hutch-- on September 12, 2008, 01:26:32 AM
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.
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: goo5257 on September 12, 2008, 07:26:41 AM
Can you guys give me the some reference for me to study all those things?
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: MichaelW on September 12, 2008, 02:34:20 PM
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 (http://msdn.microsoft.com/en-us/library/ms686047.aspx) 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 (http://msdn.microsoft.com/en-us/library/ms686025(VS.85).aspx) 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 (http://msdn.microsoft.com/en-us/library/ms682662(VS.85).aspx) function.

See  Console Functions (http://msdn.microsoft.com/en-us/library/ms682073.aspx) for more information.
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: GregL on September 12, 2008, 06:51:19 PM
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


Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: Neil on September 13, 2008, 08:57:06 AM
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.
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: Neil on September 14, 2008, 09:04:44 AM
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?
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: Neil on September 14, 2008, 09:19:39 AM
That code won't assemble because I think the eax register is being overwritten by invoke :(
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: BlackVortex on September 14, 2008, 09:27:40 AM
Quote from: Neil on September 14, 2008, 09:19:39 AM
That code won't assemble because I think the eax register is being overwritten by invoke :(
Hah, use another register/address
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: Neil on September 14, 2008, 09:30:35 AM
This version assembles OK, but doesn't appear to do anything ::)

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

    LOCAL hOutPut:DWORD
    LOCAL noc    :DWORD
    LOCAL coord  :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 coord,eax
    mov ecx,1
    invoke FillConsoleOutputAttribute,hOutPut,edx,ecx,coord,ADDR noc
    ret

SetCellColor endp
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: Neil on September 14, 2008, 09:41:20 AM
It works, I got the parameters mixed up in the call :green
Any advice on improving it be be welcome.
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: herge on September 14, 2008, 12:32:02 PM

Hi ALL:


; TEXTCOL.ASM 9:12:44 PM Saturday, September 13, 2008
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
    first dd 0
    public szPrim
    szPrim  db 16 dup(?),0
    db 0
    CRLF    db 13,10
    db 0
    .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
  public start

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    mov     eax, 15
    mov     ebx, 1
    mov     ecx, 14
@@:
    push    eax
    push    ebx
    push    ecx
    mov     first, eax    
    invoke SetTextColor, eax, ebx
    print "XXXXXXX "
    invoke crt__ultoa, first, addr szPrim , 10
    invoke StdOut, addr szPrim
    print " "
    invoke crt__ultoa, ebx, addr szPrim , 10
    invoke StdOut, addr szPrim

    invoke SetTextColor, 7, 0
    invoke StdOut, addr CRLF
    pop     ecx
    pop     ebx
    pop     eax
    dec     eax
    inc     ebx
    dec     ecx
    jnz     @B
    invoke SetTextColor, 7, 0
    inkey
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


When the foreground colour equals background colour
you can Not see your output thats why you don't see
line 8.

Regards herge
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: Neil on September 14, 2008, 01:44:29 PM
Thanks herge, I've got it sorted now & it works OK. I use it to highlight a character in the edit part of a program, instead of using a flashing cursor.
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: herge on September 14, 2008, 03:25:48 PM

Hi Neil:


    invoke SetTextColor, eax, ebx
    print "XXXXXXX "
    .if first == 8
    invoke SetTextColor, 15, 1
    .endif


If you want too see 8's you have to
do a little cheating and change text
colour attributes.

enjoy.

regards herge
Title: Re: HOW to set background and text colour using masm32?ANY suggestion?
Post by: Neil on October 20, 2008, 01:40:14 PM
I've added in another parameter to make the proc more versatile, n has been added which is the number of character positions in which the attributes are changed, this can be from 1 until the end of a row. If a block of characters need to be changed then the proc can be called in a loop changing the row (x) parameter each pass through. Remember, only the attributes are changed.

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

    LOCAL hOutPut:DWORD
    LOCAL noc    :DWORD
    LOCAL cord  :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 cord,eax
    mov ecx,n
    invoke FillConsoleOutputAttribute,hOutPut,edx,ecx,cord,ADDR noc
    ret

SetCellColor endp