News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

invoke SetTextColor issue

Started by dick_defuser, December 26, 2021, 07:44:18 PM

Previous topic - Next topic

dick_defuser

Hello, I'm studying MASM32 now and I'm complete newb. So I'm carrying out a task of translating a FASM program to MASM and I have an issue.
The program's about changing the custon button's color when it pressed.

.686
.model flat,stdcall
option casemap:none

include         \masm32\include\windows.inc
include         \masm32\include\user32.inc
include         \masm32\include\kernel32.inc
include         \masm32\include\gdi32.inc
include         \masm32\include\comdlg32.inc
include         \masm32\include\comctl32.inc
include         \masm32\include\shell32.inc

includelib         \masm32\lib\user32.lib
includelib         \masm32\lib\kernel32.lib
includelib         \masm32\lib\gdi32.lib
includelib         \masm32\lib\comdlg32.lib
includelib         \masm32\lib\comctl32.lib
includelib         \masm32\lib\shell32.lib

DialogProc PROTO :DWORD,:DWORD,:DWORD,:DWORD

ID_ST1 equ 101
ID_ST2 equ 102
ID_ED1 equ 103

; Text and background colors of elements
clrFore equ 00FF00FFh
clrBack equ 0012FFFFh

; Main window background color
clrMain equ 0012FFFFh

.data?

hBrushBack      dd ?
len dd ?
buff db 100h dup(?)
hPen dd ?
hBrush dd ?

.code

WinMain proc
    invoke  GetModuleHandle,0
    invoke  DialogBoxParam,eax,1,HWND_DESKTOP,DialogProc,0
    invoke  ExitProcess,0
WinMain endp

DialogProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

      push  ebx
      push  esi
      push  edi
      cmp [uMsg],WM_INITDIALOG
je wmINITDIALOG
cmp [uMsg],WM_COMMAND
je wmCOMMAND
cmp [uMsg],WM_CLOSE
je wmCLOSE
      cmp   [uMsg],WM_LBUTTONDOWN
      je    DRAG
      cmp   [uMsg],WM_DRAWITEM
      je    COLOR_BUTTON
      xor   eax,eax
      jmp   PROCESSED

COLOR_BUTTON:
      ; pointer to structure DRAWITEMSTRUCT
      mov ebx, [LPARAM]

      ; set color mixing mode
      invoke SetBkMode,[ebx+DRAWITEMSTRUCT.hdc],TRANSPARENT

      ; Is the button active?
      test [ebx+DRAWITEMSTRUCT.itemState],ODS_SELECTED
      jz @f

      invoke  GetSysColor,COLOR_ACTIVEBORDER
      invoke  CreatePen,PS_SOLID,1,eax
      invoke  SelectObject,[ebx+DRAWITEMSTRUCT.hdc],eax

      invoke  GetSysColor,COLOR_3DLIGHT
      invoke  CreateSolidBrush,eax
      invoke  SelectObject,[ebx+DRAWITEMSTRUCT.hdc],eax

      ; Filled rectangle
      invoke  Rectangle,[ebx+DRAWITEMSTRUCT.hdc],\
              [ebx+DRAWITEMSTRUCT.rcItem.left],\
              [ebx+DRAWITEMSTRUCT.rcItem.top],\
              [ebx+DRAWITEMSTRUCT.rcItem.right],\
              [ebx+DRAWITEMSTRUCT.rcItem.bottom]

      jmp     LOC_TEXT

@@:   
      ; Set frame color and style
      invoke  GetSysColor,COLOR_3DDKSHADOW
      invoke  CreatePen,PS_SOLID,3,eax
      invoke  SelectObject,[ebx+DRAWITEMSTRUCT.hdc],eax

      ; Set background color
      invoke  GetSysColor,COLOR_HIGHLIGHT
      invoke  CreateSolidBrush,eax
      invoke  SelectObject,[ebx+DRAWITEMSTRUCT.hdc],eax

      ; Filled rectangle
      invoke  Rectangle,[ebx+DRAWITEMSTRUCT.hdc],\
              [ebx+DRAWITEMSTRUCT.rcItem.left],\
              [ebx+DRAWITEMSTRUCT.rcItem.top],\
              [ebx+DRAWITEMSTRUCT.rcItem.right],\
              [ebx+DRAWITEMSTRUCT.rcItem.bottom]

LOC_TEXT:
      ; Button label length
      invoke  GetWindowTextLength,[ebx+DRAWITEMSTRUCT.hwndItem]
      inc     eax
      mov     [len],eax
      invoke  GetWindowText,[ebx+DRAWITEMSTRUCT.hwndItem],buff,[len]

      ; Set text color
      invoke  SetTextColor,[ebx+DRAWITEMSTRUCT.hdc],0x0000FFFF

      ; For the pressed button, move the text down-right
      test    [ebx+DRAWITEMSTRUCT.itemState],ODS_SELECTED
      jz      @f

      ; Set text color
      invoke  SetTextColor,[ebx+DRAWITEMSTRUCT.hdc],0x000000FF

      add     [ebx+DRAWITEMSTRUCT.rcItem.left],2
      add     [ebx+DRAWITEMSTRUCT.rcItem.top],2
@@:
      ; Button label
      lea     esi,[ebx+DRAWITEMSTRUCT.rcItem]
      invoke  DrawText,[ebx+DRAWITEMSTRUCT.hdc],buff,[len],esi,\
              DT_SINGLELINE+DT_VCENTER+DT_CENTER

      jmp     PROCESSED

DRAG: invoke ReleaseCapture
      invoke SendMessage, [hWnd], WM_SYSCOMMAND,61458,0
      jmp PROCESSED

wmINITDIALOG:
      jmp PROCESSED

wmCOMMAND:
      cmp [wParam], BN_CLICKED shl 16 + IDCANCEL
      je wmCLOSE
      jmp PROCESSED

wmCLOSE:
      invoke DeleteObject,[hBrushBack]
     
      invoke EndDialog, [hWnd], 0

PROCESSED:
      pop edi esi ebx
      ret

DialogProc endp

end WinMain


So assemblying and linking file throws some errors:

C:\Users\ilkov\Desktop\06\win22.asm(121) : error A2206: missing operator in expression
C:\Users\ilkov\Desktop\06\win22.asm(121) : error A2114: INVOKE argument type mismatch : argument : 2
C:\Users\ilkov\Desktop\06\win22.asm(128) : error A2206: missing operator in expression
C:\Users\ilkov\Desktop\06\win22.asm(128) : error A2114: INVOKE argument type mismatch : argument : 2
C:\Users\ilkov\Desktop\06\win22.asm(158) : error A2206: missing operator in expression

This refers to strings:

invoke  SetTextColor,[ebx+DRAWITEMSTRUCT.hdc],0x0000FFFF

and

invoke  SetTextColor,[ebx+DRAWITEMSTRUCT.hdc],0x000000FF

So anyone can help to find out what's whong here?

mineiro

hello sir;

Line bellow is wrong:
pop edi esi ebx
should be
pop edi
pop esi
pop ebx

Masm notation to hexadecimal numbers have a suffix h, you're inserting a C prefix 0x.
invoke  SetTextColor,[ebx+DRAWITEMSTRUCT.hdc],0x0000FFFF
invoke  SetTextColor,[ebx+DRAWITEMSTRUCT.hdc],0000FFFFh
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

dick_defuser

Thank you kindly for your reply  :wink2:

jj2007

Hi Dick,

Welcome to the Forum :thup:

Coming from FASM is somewhat a challenge. I've gone through your code and tried to use some of the Masm features that could make your life easier, see e.g. dis equ [ebx.DRAWITEMSTRUCT] ; saves a lot of writing ;-)
...
test dis.itemState, ODS_SELECTED
...
      invoke  Rectangle, dis.hdc,\
              dis.rcItem.left,\
              dis.rcItem.top,\
              dis.rcItem.right,\
              dis.rcItem.bottom


How does your resource file look like?

Your WndProc is unnecessarily complicated (and thus, error-prone). Have a look at the Switch ... Case sequence in the attached MiniWinMasm32 template.

dick_defuser

Hi, jj2007, thank you for your reply and advices! The resorse code is my next challenge now, cause it compiling with errors:

rsrc.rc (16): error RC2116 : expecting number for ID
rsrc.rc (16): error RC2111 : invalid control type
rsrc.rc (16): error RC2113 : END expected in dialog

So this is how my .rc file looks:

#include "resource.h"

#define IDC_DIALOG 200
#define ID_ST1 101
#define ID_ST2 102
#define ID_ED1 103
#define clrFore 00FF00FFh
#define clrBack 0012FFFFh
#define clrMain 0012FFFFh

IDC_DIALOG DIALOG 0,0,190,55
STYLE WS_CAPTION | WS_SYSMENU | DS_CENTER | DS_SYSMODAL
CAPTION "Press the button!"
FONT 10,"Arial"
BEGIN
    CONTROL "","BUTTON", WS_VISIBLE,BS_GROUPBOX,-1, 2, -1, 185, 35
    CONTROL "Press!","BUTTON",WS_VISIBLE,WS_TABSTOP,BS_PUSHBUTTON,BS_OWNERDRAW,-1,80,37,50,15
    CONTROL "Exit",IDCANCEL,"BUTTON",WS_VISIBLE,WS_TABSTOP,BS_PUSHBUTTON,135,37,50,15
END

dick_defuser

By the way, here's the original FASM code, if you want to see it:

format PE GUI 4.0
entry start

include 'win32a.inc'

ID_ST1 = 101
ID_ST2 = 102
ID_ED1 = 103

; Цвета текста и фона элементов
clrFore = 00FF00FFh
clrBack = 0012FFFFh

; Цвет фона главного окна
clrMain = 0012FFFFh

;---------------------------------------------

section '.data' data readable writeable

hBrushBack      dd ?
len dd ?
buff rb 100h
hPen dd ?
hBrush dd ?

; Item LPDRAWITEMSTRUCT

;---------------------------------------------

section '.code' code readable executable

  start:
        invoke  GetModuleHandle,0
        invoke  DialogBoxParam,eax,1,HWND_DESKTOP,DialogProc,0

        invoke  ExitProcess,0

proc DialogProc hwnddlg,msg,wparam,lparam
        push    ebx esi edi
        cmp     [msg],WM_INITDIALOG
        je      wminitdialog
        cmp     [msg],WM_COMMAND
        je      wmcommand
        cmp     [msg],WM_CLOSE
        je      wmclose
        cmp     [msg],WM_LBUTTONDOWN
        je      drag

        cmp     [msg],WM_DRAWITEM
        je      color_button

        xor     eax,eax
        jmp     processed

color_button:
        ; Указатель на структуру DRAWITEMSTRUCT
        mov     ebx,[lparam]

        ; Установить режим смешивания фоновых цветов
        invoke  SetBkMode,[ebx+DRAWITEMSTRUCT.hDC],TRANSPARENT

        ; Кнопка активна?
        test    [ebx+DRAWITEMSTRUCT.itemState],ODS_SELECTED
        jz      @f

        invoke  GetSysColor,COLOR_ACTIVEBORDER
        invoke  CreatePen,PS_SOLID,1,eax
        invoke  SelectObject,[ebx+DRAWITEMSTRUCT.hDC],eax

        invoke  GetSysColor,COLOR_3DLIGHT
        invoke  CreateSolidBrush,eax
        invoke  SelectObject,[ebx+DRAWITEMSTRUCT.hDC],eax

        ; Прямоугольник с заливкой
        invoke  Rectangle,[ebx+DRAWITEMSTRUCT.hDC],\
                [ebx+DRAWITEMSTRUCT.rcItem.left],\
                [ebx+DRAWITEMSTRUCT.rcItem.top],\
                [ebx+DRAWITEMSTRUCT.rcItem.right],\
                [ebx+DRAWITEMSTRUCT.rcItem.bottom]

        jmp     loc_text
@@:
        ; Установить цвет и стиль рамки
        invoke  GetSysColor,COLOR_3DDKSHADOW
        invoke  CreatePen,PS_SOLID,3,eax
        invoke  SelectObject,[ebx+DRAWITEMSTRUCT.hDC],eax

        ; Установить цвет фона
        invoke  GetSysColor,COLOR_HIGHLIGHT
        invoke  CreateSolidBrush,eax
        invoke  SelectObject,[ebx+DRAWITEMSTRUCT.hDC],eax

        ; Прямоугольник с заливкой
        invoke  Rectangle,[ebx+DRAWITEMSTRUCT.hDC],\
                [ebx+DRAWITEMSTRUCT.rcItem.left],\
                [ebx+DRAWITEMSTRUCT.rcItem.top],\
                [ebx+DRAWITEMSTRUCT.rcItem.right],\
                [ebx+DRAWITEMSTRUCT.rcItem.bottom]

loc_text:
        ; Длина надписи на кнопке
        invoke  GetWindowTextLength,[ebx+DRAWITEMSTRUCT.hwndItem]
        inc     eax
        mov     [len],eax
        invoke  GetWindowText,[ebx+DRAWITEMSTRUCT.hwndItem],buff,[len]

        ; Установить цвет текста
        invoke  SetTextColor,[ebx+DRAWITEMSTRUCT.hDC],0x0000FFFF

        ; Для нажатой кнопки сдвинуть текст вниз-вправо
        test    [ebx+DRAWITEMSTRUCT.itemState],ODS_SELECTED
        jz      @f

        ; Установить цвет текста
        invoke  SetTextColor,[ebx+DRAWITEMSTRUCT.hDC],0x000000FF

        add     [ebx+DRAWITEMSTRUCT.rcItem.left],2
        add     [ebx+DRAWITEMSTRUCT.rcItem.top],2
@@:
        ; Надпись на кнопке
        lea     esi,[ebx+DRAWITEMSTRUCT.rcItem]
        invoke  DrawText,[ebx+DRAWITEMSTRUCT.hDC],buff,[len],esi,\
                DT_SINGLELINE+DT_VCENTER+DT_CENTER

        jmp     processed

drag:
        invoke  ReleaseCapture
        invoke  SendMessage,[hwnddlg],WM_SYSCOMMAND,61458,0
        jmp     processed

wminitdialog:
        jmp     processed

wmcommand:
        cmp     [wparam],BN_CLICKED shl 16 + IDCANCEL
        je      wmclose
        jmp     processed

wmclose:
        invoke  DeleteObject,[hBrushBack]

        invoke  EndDialog,[hwnddlg],0
processed:
        pop     edi esi ebx
        ret
endp

;---------------------------------------------

section '.idata' import data readable writeable

  library kernel32,'kernel32.dll',\
          user32,'user32.dll',\
          gdi32,'gdi32.dll'

  include 'apia\kernel32.inc'
  include 'apia\user32.inc'
  include 'apia\gdi32.inc'

;---------------------------------------------

section '.rsrc' resource data readable

  directory RT_DIALOG,dialogs

  resource dialogs,\
           1,LANG_ENGLISH+SUBLANG_DEFAULT,demonstration

  dialog demonstration,'Color Button Demo',0,0,190,55,WS_CAPTION+WS_SYSMENU+DS_CENTER+DS_SYSMODAL
    dialogitem 'BUTTON','',-1, 2, -1, 185, 35,WS_VISIBLE+BS_GROUPBOX
    dialogitem 'BUTTON','Button',-1,80,37,50,15,WS_VISIBLE+WS_TABSTOP+BS_PUSHBUTTON+BS_OWNERDRAW
    dialogitem 'BUTTON','Exit',IDCANCEL,135,37,50,15,WS_VISIBLE+WS_TABSTOP+BS_PUSHBUTTON
  enddialog