Can the text color of a radio button be changed? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
songdg
Posts: 616
Joined: 04 Oct 2017, 20:04

Can the text color of a radio button be changed?

13 May 2024, 21:14

I want to set the chosen radio button apart further in a radio group by changing its text color, is it even possible?
User avatar
mikeyww
Posts: 27214
Joined: 09 Sep 2014, 18:38

Re: Can the text color of a radio button be changed?

13 May 2024, 22:26

Code: Select all

#Requires AutoHotkey v2.0
g := Gui()
g.SetFont 'cRed'
g.AddRadio , 'abc'
g.SetFont 'cBlue'
g.AddRadio , 'def'
g.Show
songdg
Posts: 616
Joined: 04 Oct 2017, 20:04

Re: Can the text color of a radio button be changed?

13 May 2024, 22:39

mikeyww wrote:
13 May 2024, 22:26

Code: Select all

#Requires AutoHotkey v2.0
g := Gui()
g.SetFont 'cRed'
g.AddRadio , 'abc'
g.SetFont 'cBlue'
g.AddRadio , 'def'
g.Show
Thanks, I mean initially all the text color are the same, when I checked a radio button the text color will change.
User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Can the text color of a radio button be changed?

14 May 2024, 03:25

 I thought I could easily write it because I've done similar tasks in v1, but surprisingly, there were some grammar changes from v2, so it took me a bit of time.
This is more of a conceptual demonstration to show what you desire is possible. In reality, it needs a bit more refinement.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

g := gui()
g.setFont("C000000 S10 W400")
radio := g.addRadio(,"radio")
radio.onEvent("Click", radio_Click)
SetRadioColor.register(radio, 0xFF0000, 0x000000)
g.show()

radio_Click(guiCtrlObj, info)    {
    static v := false
    guiCtrlObj.Value := (v:=!v)
    SetRadioColor.onClick(guiCtrlObj)
}
;==========================================================================================
class SetRadioColor
{
    static _guiCntlObjs := map()
    static register(guiCtrlObj, checkedColor, uncheckedColor)    {
        this._guiCntlObjs[guiCtrlObj] := {logFont:this._guiControlGetLogFont(guiCtrlObj)
            ,checkedColor:checkedColor
            ,uncheckedColor:uncheckedColor}
    }
    static onClick(guiCtrlObj, *)    {
        /*
        guiObj := guiCtrlObj.Gui
        guiLogFont := this._guiControlGetLogFont(guiObj)
        this._getColorStatic(guiObj,, &crefText)
        guiTextColor := (((crefText&0xFF)<<16)|(crefText&0xFF00)|((crefText&0xFF0000)>>16))&0xFFFFFF
        prevGuiFontOptions := this._getFontOptionsFromLogFont(guiLogFont, guiTextColor)
        prevGuiFontName := guiLogFont.FaceName
        */       
        guiCtrlLogFont := this._guiCntlObjs[guiCtrlObj].logFont
        guiCtrlTextColor := this._guiCntlObjs[guiCtrlObj].%(guiCtrlObj.Value?"checkedColor":"uncheckedColor")%
        newGuiCtrlFontOptions := this._getFontOptionsFromLogFont(guiCtrlLogFont, guiCtrlTextColor)
        newGuiCtrlFontName := guiCtrlLogFont.FaceName
        guiCtrlObj.setFont(newGuiCtrlFontOptions, newGuiCtrlFontName)
    }
    static _getFontOptionsFromLogFont(logFont, fontColor?)    {
        return "norm" (logFont.Italic?" italic":"") (logFont.StrikeOut?" strike":"") (logFont.Underline?" underline":"") (isSet(fontColor)?" C" format("{:06X}",fontColor):"") " S" round(-logFont.Height*72/A_ScreenDPI) " W" logFont.Weight " Q" logFont.Quality
    }
    /*
    static _getColorStatic(guiObj, &crefBK?, &crefText?)    {
        static WM_CTLCOLORSTATIC:=0x0138, SMTO_NORMAL:=0x0000
        crefBK:= crefText:= 0
        if !(hDC:=dllCall("User32.dll\GetDC", "Ptr",guiObj.Hwnd, "Ptr"))
            return
        temp := guiObj.addText("xp yp wp hp Hidden")
        if (hRet:=dllCall("User32.dll\SendMessageTimeout"
            ,"Ptr",guiObj.Hwnd
            ,"UInt",WM_CTLCOLORSTATIC
            ,"Ptr",hDC
            ,"Ptr",temp.Hwnd
            ,"UInt",SMTO_NORMAL
            ,"UInt",50
            ,"Ptr*",lpdwResult:=0
            ,"Ptr"))    {
            crefBK:=dllCall("Gdi32.dll\GetBkColor", "Ptr",hDC)
            crefText:=dllCall("Gdi32.dll\GetTextColor", "Ptr",hDC)
        }
        dllCall("User32.dll\ReleaseDC", "Ptr",guiObj.Hwnd, "Ptr",hDC)
        dllCall("User32.dll\DestroyWindow", "Ptr",temp.Hwnd)
    }
    */
    static _guiControlGetLogFont(hWnd_or_guiObj, addTextTemporarily:=true)    {
        static WM_GETFONT:=0x31, SMTO_NORMAL:=0x0000
        logFont:={Height:""
            ,Width:""
            ,Escapement:""
            ,Orientation:""
            ,Weight:""
            ,Italic:""
            ,Underline:""
            ,StrikeOut:""
            ,CharSet:""
            ,OutPrecision:""
            ,ClipPrecision:""
            ,Quality:""
            ,PitchAndFamily:""
            ,FaceName:""}
        hWnd:=0
        switch
        {
            case (hWnd_or_guiObj is gui):               hWnd:=hWnd_or_guiObj.Hwnd
            case (hWnd_or_guiObj is gui.control):       hWnd:=hWnd_or_guiObj.Hwnd
            default:
                if (hWnd_or_guiObj~="D)^-?(?:[[:digit:]]+|0[Xx][[:xdigit:]]+)$"
                && dllCall("User32.dll\IsWindow", "Ptr",hWnd_or_guiObj))
                    hWnd:=hWnd_or_guiObj
        }
        if (!hWnd)
            return logFont
        if (!dllCall("User32.dll\SendMessageTimeout", "Ptr",hWnd, "UInt",WM_GETFONT, "Ptr",0, "Ptr",0, "UInt",SMTO_NORMAL, "UInt",50, "Ptr*",&hFont:=0, "Ptr"))
            return logFont
        if (!hFont)    {
            if (!addTextTemporarily)
                return logFont
            if !(hWnd_or_guiObj is gui)
                return logFont
            guiCntl:=hWnd_or_guiObj.add("Text","xp yp wp hp Hidden")
            if (!dllCall("User32.dll\SendMessageTimeout", "Ptr",hText:=guiCntl.hWnd, "UInt",WM_GETFONT, "Ptr",0, "Ptr",0, "UInt",SMTO_NORMAL, "UInt",50, "Ptr*",&hFont:=0, "Ptr"))
                return logFont
            dllCall("User32.dll\DestroyWindow", "Ptr",hText)
        }
        sizeLF:=dllCall("Gdi32.dll\GetObject", "Ptr",hFont, "Int",0, "Ptr",0)
        buf:=buffer(sizeLF, 0)
        dllCall("Gdi32.dll\GetObject", "Ptr",hFont, "Int",sizeLF, "Ptr",buf.Ptr)
         logFont.Height             := numGet(buf, 0,"Int")
        ,logFont.Width              := numGet(buf, 4,"Int")
        ,logFont.Escapement         := numGet(buf, 8,"Int")
        ,logFont.Orientation        := numGet(buf, 12,"Int")
        ,logFont.Weight             := numGet(buf, 16,"Int")
        ,logFont.Italic             := numGet(buf, 20,"Char")
        ,logFont.Underline          := numGet(buf, 21,"Char")
        ,logFont.StrikeOut          := numGet(buf, 22,"Char")
        ,logFont.CharSet            := numGet(buf, 23,"Char")
        ,logFont.OutPrecision       := numGet(buf, 24,"Char")
        ,logFont.ClipPrecision      := numGet(buf, 25,"Char")
        ,logFont.Quality            := numGet(buf, 26,"Char")
        ,logFont.PitchAndFamily     := numGet(buf, 27,"Char")
        ,logFont.FaceName           := strGet(buf.Ptr+28)
        return logFont
    }
    ;   Re: How to get the current Gui font w/o creating a control?
    ;  https://www.autohotkey.com/boards/viewtopic.php?t=161#p309740
    ;   Re: Get Gui Color
    ;  https://www.autohotkey.com/boards/viewtopic.php?t=13683#p257565
}
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
XMCQCX
Posts: 251
Joined: 14 Oct 2020, 23:44

Re: Can the text color of a radio button be changed?  Topic is solved

14 May 2024, 04:10

The SetFont method does not appear to work to change radio buttons text color. I'm not sure. One workaround is to associate text controls to the radio buttons.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance

g := Gui()
g.OnEvent('Close', (*) => ExitApp())
g.Add('Radio', 'w15 h15').OnEvent('Click', SetRadioColor.Bind('btn1'))
g.Add('Radio', 'xm ym+30 w15 h15').OnEvent('Click', SetRadioColor.Bind('btn2'))
g.Add('Radio', 'xm ym+60 w15 h15').OnEvent('Click', SetRadioColor.Bind('btn3'))
g.txtRadio_btn1 := g.Add('Text', 'xm+20 ym', 'Button 1')
g.txtRadio_btn2 := g.Add('Text', 'xp ym+30', 'Button 2')
g.txtRadio_btn3 := g.Add('Text', 'xp ym+60', 'Button 3')
g.Show()

SetRadioColor(btn, *)
{
    Loop 3
        g.txtRadio_btn%A_Index%.SetFont('cDefault')

    g.txtRadio_%btn%.SetFont('cGreen')
}
User avatar
mikeyww
Posts: 27214
Joined: 09 Sep 2014, 18:38

Re: Can the text color of a radio button be changed?

14 May 2024, 06:53

This works and seems to demonstrate an AutoHotkey bug whereby the font must first be set to a non-default color.

Code: Select all

#Requires AutoHotkey v2.0
buttons      := ['abc', 'def', 'ghi']
g            := Gui(, 'Buttons')
rad          := []
defaultColor := 'Black'
fontSize     := 10
g.SetFont 's' fontSize ' c' defaultColor
For radio in buttons {
 rad.Push g.AddRadio('w200', radio)
 rad[rad.Length].OnEvent('Click', radio_Click)
}
g.Show

radio_Click(btn, info) {
 For radio in rad
  radio.SetFont 'c' defaultColor
 btn.SetFont 'cRed'
}
songdg
Posts: 616
Joined: 04 Oct 2017, 20:04

Re: Can the text color of a radio button be changed?

15 May 2024, 03:49

Seven0528 wrote:
14 May 2024, 03:25
 I thought I could easily write it because I've done similar tasks in v1, but surprisingly, there were some grammar changes from v2, so it took me a bit of time.
This is more of a conceptual demonstration to show what you desire is possible. In reality, it needs a bit more refinement.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

g := gui()
g.setFont("C000000 S10 W400")
radio := g.addRadio(,"radio")
radio.onEvent("Click", radio_Click)
SetRadioColor.register(radio, 0xFF0000, 0x000000)
g.show()

radio_Click(guiCtrlObj, info)    {
    static v := false
    guiCtrlObj.Value := (v:=!v)
    SetRadioColor.onClick(guiCtrlObj)
}
;==========================================================================================
class SetRadioColor
{
    static _guiCntlObjs := map()
    static register(guiCtrlObj, checkedColor, uncheckedColor)    {
        this._guiCntlObjs[guiCtrlObj] := {logFont:this._guiControlGetLogFont(guiCtrlObj)
            ,checkedColor:checkedColor
            ,uncheckedColor:uncheckedColor}
    }
    static onClick(guiCtrlObj, *)    {
        /*
        guiObj := guiCtrlObj.Gui
        guiLogFont := this._guiControlGetLogFont(guiObj)
        this._getColorStatic(guiObj,, &crefText)
        guiTextColor := (((crefText&0xFF)<<16)|(crefText&0xFF00)|((crefText&0xFF0000)>>16))&0xFFFFFF
        prevGuiFontOptions := this._getFontOptionsFromLogFont(guiLogFont, guiTextColor)
        prevGuiFontName := guiLogFont.FaceName
        */       
        guiCtrlLogFont := this._guiCntlObjs[guiCtrlObj].logFont
        guiCtrlTextColor := this._guiCntlObjs[guiCtrlObj].%(guiCtrlObj.Value?"checkedColor":"uncheckedColor")%
        newGuiCtrlFontOptions := this._getFontOptionsFromLogFont(guiCtrlLogFont, guiCtrlTextColor)
        newGuiCtrlFontName := guiCtrlLogFont.FaceName
        guiCtrlObj.setFont(newGuiCtrlFontOptions, newGuiCtrlFontName)
    }
    static _getFontOptionsFromLogFont(logFont, fontColor?)    {
        return "norm" (logFont.Italic?" italic":"") (logFont.StrikeOut?" strike":"") (logFont.Underline?" underline":"") (isSet(fontColor)?" C" format("{:06X}",fontColor):"") " S" round(-logFont.Height*72/A_ScreenDPI) " W" logFont.Weight " Q" logFont.Quality
    }
    /*
    static _getColorStatic(guiObj, &crefBK?, &crefText?)    {
        static WM_CTLCOLORSTATIC:=0x0138, SMTO_NORMAL:=0x0000
        crefBK:= crefText:= 0
        if !(hDC:=dllCall("User32.dll\GetDC", "Ptr",guiObj.Hwnd, "Ptr"))
            return
        temp := guiObj.addText("xp yp wp hp Hidden")
        if (hRet:=dllCall("User32.dll\SendMessageTimeout"
            ,"Ptr",guiObj.Hwnd
            ,"UInt",WM_CTLCOLORSTATIC
            ,"Ptr",hDC
            ,"Ptr",temp.Hwnd
            ,"UInt",SMTO_NORMAL
            ,"UInt",50
            ,"Ptr*",lpdwResult:=0
            ,"Ptr"))    {
            crefBK:=dllCall("Gdi32.dll\GetBkColor", "Ptr",hDC)
            crefText:=dllCall("Gdi32.dll\GetTextColor", "Ptr",hDC)
        }
        dllCall("User32.dll\ReleaseDC", "Ptr",guiObj.Hwnd, "Ptr",hDC)
        dllCall("User32.dll\DestroyWindow", "Ptr",temp.Hwnd)
    }
    */
    static _guiControlGetLogFont(hWnd_or_guiObj, addTextTemporarily:=true)    {
        static WM_GETFONT:=0x31, SMTO_NORMAL:=0x0000
        logFont:={Height:""
            ,Width:""
            ,Escapement:""
            ,Orientation:""
            ,Weight:""
            ,Italic:""
            ,Underline:""
            ,StrikeOut:""
            ,CharSet:""
            ,OutPrecision:""
            ,ClipPrecision:""
            ,Quality:""
            ,PitchAndFamily:""
            ,FaceName:""}
        hWnd:=0
        switch
        {
            case (hWnd_or_guiObj is gui):               hWnd:=hWnd_or_guiObj.Hwnd
            case (hWnd_or_guiObj is gui.control):       hWnd:=hWnd_or_guiObj.Hwnd
            default:
                if (hWnd_or_guiObj~="D)^-?(?:[[:digit:]]+|0[Xx][[:xdigit:]]+)$"
                && dllCall("User32.dll\IsWindow", "Ptr",hWnd_or_guiObj))
                    hWnd:=hWnd_or_guiObj
        }
        if (!hWnd)
            return logFont
        if (!dllCall("User32.dll\SendMessageTimeout", "Ptr",hWnd, "UInt",WM_GETFONT, "Ptr",0, "Ptr",0, "UInt",SMTO_NORMAL, "UInt",50, "Ptr*",&hFont:=0, "Ptr"))
            return logFont
        if (!hFont)    {
            if (!addTextTemporarily)
                return logFont
            if !(hWnd_or_guiObj is gui)
                return logFont
            guiCntl:=hWnd_or_guiObj.add("Text","xp yp wp hp Hidden")
            if (!dllCall("User32.dll\SendMessageTimeout", "Ptr",hText:=guiCntl.hWnd, "UInt",WM_GETFONT, "Ptr",0, "Ptr",0, "UInt",SMTO_NORMAL, "UInt",50, "Ptr*",&hFont:=0, "Ptr"))
                return logFont
            dllCall("User32.dll\DestroyWindow", "Ptr",hText)
        }
        sizeLF:=dllCall("Gdi32.dll\GetObject", "Ptr",hFont, "Int",0, "Ptr",0)
        buf:=buffer(sizeLF, 0)
        dllCall("Gdi32.dll\GetObject", "Ptr",hFont, "Int",sizeLF, "Ptr",buf.Ptr)
         logFont.Height             := numGet(buf, 0,"Int")
        ,logFont.Width              := numGet(buf, 4,"Int")
        ,logFont.Escapement         := numGet(buf, 8,"Int")
        ,logFont.Orientation        := numGet(buf, 12,"Int")
        ,logFont.Weight             := numGet(buf, 16,"Int")
        ,logFont.Italic             := numGet(buf, 20,"Char")
        ,logFont.Underline          := numGet(buf, 21,"Char")
        ,logFont.StrikeOut          := numGet(buf, 22,"Char")
        ,logFont.CharSet            := numGet(buf, 23,"Char")
        ,logFont.OutPrecision       := numGet(buf, 24,"Char")
        ,logFont.ClipPrecision      := numGet(buf, 25,"Char")
        ,logFont.Quality            := numGet(buf, 26,"Char")
        ,logFont.PitchAndFamily     := numGet(buf, 27,"Char")
        ,logFont.FaceName           := strGet(buf.Ptr+28)
        return logFont
    }
    ;   Re: How to get the current Gui font w/o creating a control?
    ;  https://www.autohotkey.com/boards/viewtopic.php?t=161#p309740
    ;   Re: Get Gui Color
    ;  https://www.autohotkey.com/boards/viewtopic.php?t=13683#p257565
}
Thanks, With some refinement your code can demonstrate on Scripts and Functions :thumbup:
songdg
Posts: 616
Joined: 04 Oct 2017, 20:04

Re: Can the text color of a radio button be changed?

15 May 2024, 03:54

XMCQCX wrote:
14 May 2024, 04:10
The SetFont method does not appear to work to change radio buttons text color. I'm not sure. One workaround is to associate text controls to the radio buttons.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance

g := Gui()
g.OnEvent('Close', (*) => ExitApp())
g.Add('Radio', 'w15 h15').OnEvent('Click', SetRadioColor.Bind('btn1'))
g.Add('Radio', 'xm ym+30 w15 h15').OnEvent('Click', SetRadioColor.Bind('btn2'))
g.Add('Radio', 'xm ym+60 w15 h15').OnEvent('Click', SetRadioColor.Bind('btn3'))
g.txtRadio_btn1 := g.Add('Text', 'xm+20 ym', 'Button 1')
g.txtRadio_btn2 := g.Add('Text', 'xp ym+30', 'Button 2')
g.txtRadio_btn3 := g.Add('Text', 'xp ym+60', 'Button 3')
g.Show()

SetRadioColor(btn, *)
{
    Loop 3
        g.txtRadio_btn%A_Index%.SetFont('cDefault')

    g.txtRadio_%btn%.SetFont('cGreen')
}
What a clever way, thank you so much :bravo:
songdg
Posts: 616
Joined: 04 Oct 2017, 20:04

Re: Can the text color of a radio button be changed?

15 May 2024, 03:55

mikeyww wrote:
14 May 2024, 06:53
This works and seems to demonstrate an AutoHotkey bug whereby the font must first be set to a non-default color.

Code: Select all

#Requires AutoHotkey v2.0
buttons      := ['abc', 'def', 'ghi']
g            := Gui(, 'Buttons')
rad          := []
defaultColor := 'Black'
fontSize     := 10
g.SetFont 's' fontSize ' c' defaultColor
For radio in buttons {
 rad.Push g.AddRadio('w200', radio)
 rad[rad.Length].OnEvent('Click', radio_Click)
}
g.Show

radio_Click(btn, info) {
 For radio in rad
  radio.SetFont 'c' defaultColor
 btn.SetFont 'cRed'
}
Thanks for providing another solution :dance:

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada, teadrinker and 36 guests