News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Radio Button's label color

Started by Mr Earl, October 18, 2007, 11:53:25 AM

Previous topic - Next topic

Mr Earl

How can I change the color of the LABEL of a radio button.  If the radio button is ON I would like the text to display in GREEN, off in BLACK?

MichaelW

I was expecting something like this to work, but it seems that the checked state hasn't been updated when the WM_CTLCOLORSTATIC message is sent, so the color change and checked state end up out of sync. I cannot see any way to solve the problem short of owner-draw buttons.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
      hInstance dd 0
      hRadio1   dd 0
      hRadio2   dd 0
      hRadio3   dd 0
      hRadio4   dd 0
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
DlgProc proc hDlg:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD

    Switch uMsg

      Case WM_INITDIALOG

        invoke CheckDlgButton,hDlg,101,BST_CHECKED
        return 1

      Case WM_CTLCOLORSTATIC

        invoke GetDlgCtrlID,lParam
        invoke IsDlgButtonChecked,hDlg,eax
        .IF eax == BST_CHECKED
          invoke SetTextColor, wParam, 00ff00h
        .ELSE
          invoke SetTextColor, wParam, 0
        .ENDIF
        invoke SetBkMode,wParam,TRANSPARENT
        return FUNC(GetSysColorBrush,COLOR_BTNFACE)

      Case WM_COMMAND

        .IF wParam == IDCANCEL
          invoke EndDialog,hDlg,0
        .ENDIF

      Case WM_CLOSE

        invoke EndDialog,hDlg,0

    EndSw

    return 0

DlgProc endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    mov hInstance, FUNC(GetModuleHandle,NULL)
    Dialog "Blank Dialog", \                ; caption
           "MS Sans Serif",10, \            ; font,pointsize
            WS_OVERLAPPED or \              ; styles for
            WS_SYSMENU or DS_CENTER, \      ; dialog window
            4, \                            ; number of controls
            50,50,140,95, \                 ; x y co-ordinates
            1024                            ; memory buffer size

    DlgRadio "Radio Button 1",WS_TABSTOP,40,15,60,10,101
    DlgRadio "Radio Button 2",WS_TABSTOP,40,30,60,10,102
    DlgRadio "Radio Button 3",WS_TABSTOP,40,45,60,10,103
    DlgRadio "Radio Button 4",WS_TABSTOP,40,60,60,10,104

    CallModalDialog hInstance,0,DlgProc,NULL

    invoke ExitProcess,eax

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start



eschew obfuscation

Mr Earl

Thanks Michael.  I was hoping there was a technique that might do it without having to use the Owner Draw process.

jj2007

Tried WM_CTLCOLOR? Below an excerpt from a Basic callback; the CTLCOLOR_STATIC message is the HIWORD of _lParam...

PROCEDURE CB_CTLCOLOR(h&,cbMess&,cbWd&,cbLg%)     'CB Background colours
  SWITCH HIWORD(cbLg%)
  CASE CTLCOLOR_STATIC
    SetBkMode(cbWd&,TRANSPARENT)
    SetTextColor(cbWd&,cc%(Red&)) 'set user-defined RGB colour
    RETVAL Br_D& 'returns a brush handle
  CASE CTLCOLOR_EDIT
    SetTextColor(cbWd&,cc%(Green&))
    SetBkMode(cbWd&,TRANSPARENT)
...

MichaelW

WM_CTLCOLOR is for 16-bit Windows. The notifications for the 32-bit versions of Windows include WM_CTLCOLORSTATIC and WM_CTLCOLORBTN, but WM_CTLCOLORBTN is for owner-drawn buttons.
eschew obfuscation

Mr Earl

I got radio button labels to change color basically using Michael's solution (above), however instead of using "IsDlgButtonChecked" I had to maintain my own switches to keep track of which of the radio buttons was "ON".  So, instead of checking:  ".if eax==BST_CHECKED", I checked: ".if RadioButton1==TRUE", etc.

Why do I want the label of the selected Radio Button in GREEN?   Well, it seems that users of my program were not paying attention to which button was "ON" and this made them think the program wasn't working, when in fact it was a "user" problem.  The color may not entirely solve the problem, but it should help.