Trying to change font options in GUI command link buttons

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
phyrburn
Posts: 13
Joined: 24 Jul 2019, 05:55

Trying to change font options in GUI command link buttons

Post by phyrburn » 12 Apr 2021, 06:32

I am trying to find out if anyone has managed to change the font, size and colour of the text in GUI command links. I am creating this somewhat small GUI as a "Do you want to continue?" type dialog box and going for a more "modern" feel rather than using standard buttons.

I am using szujeq's ControlColor function (an old version as I could not get 1.02 to work for me) to change the background colour of the command links, but it doesn't change the text. And the blue text on the coloured background is hard to read at times.

My guess is that DllCall is required here, but that's not something I am familiar with just yet. Any help would be greatly appreciated.
Image

Code: Select all

content :=
(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Nec ultrices dui sapien eget. Feugiat scelerisque
varius morbi enim nunc faucibus a."
)

Gui +hWndhWndMainWnd -Caption
Gui Color, 0xC0C0C0
Gui Font, s18 w600 q5 cBlack, Verdana
Gui Add, Text, hwndwhndTitle x8 y8 w370 h30 +0x200, Title
Gui Font, s12 Bold
Gui Add, Text, x8 y48 w100 h23 +0x200, Subtitle
Gui Font, s10 Norm
Gui Add, Text, x8 y80 w370 h46, % content
Gui Add, Custom, hWndhwndButton1 gclick x0 y152 w193 h58 ClassButton +0x200E, Option 1`nSelect option 1
ControlColor(hwndButton1, hWndMainWnd, 0xB53535, 0x7F7F7F)
Gui Add, Custom, hWndhwndButton2 gclick x193 y152 w193 h58 ClassButton +0x200E, Option 2`nSelect option 2
ControlColor(hwndButton2, hWndMainWnd, 0x81C94B, 0x7F7F7F)
Gui Show, w386 h210, InfoCOPY
OnMessage(0x201, "WM_LBUTTONDOWN")
Return

GuiEscape(GuiHwnd) {
    Gui, Destroy
}

WM_LBUTTONDOWN() {
    PostMessage, 0xA1, 2 ; WM_NCLBUTTONDOWN = 0xA1, HTCAPTION = 2
}

click(CtrlHwnd, GuiEvent, EventInfo, ErrLevel := "") {
    Gui, +OwnDialogs
    If (GuiEvent != "Normal")
        Return
}

; http://www.autohotkey.com/board/topic/104539-controlcol-set-background-and-text-color-gui-controls/

ControlColor(Control, Window, bc := "", tc := "", Redraw := 1) {
    a := {}
    a["c"] := Control
    a["g"] := Window
    a["bc"] := (bc = "") ? "" : (((bc & 255) << 16) + (((bc >> 8) & 255) << 8) + (bc >> 16))
    a["tc"] := (tc = "") ? "" : (((tc & 255) << 16) + (((tc >> 8) & 255) << 8) + (tc >> 16))
    CC_WindowProc("Set", a, "", "")
    If (Redraw) {
        WinSet Redraw,, ahk_id %Control%
    }
}

CC_WindowProc(hWnd, uMsg, wParam, lParam) {
    Static Win := {}

    If uMsg Between 0x132 And 0x138
    If (Win[hWnd].HasKey(lParam)) {
        If (tc := Win[hWnd, lParam, "tc"]) {
            DllCall("gdi32.dll\SetTextColor", "Ptr", wParam, "UInt", tc)
        }
        If (bc := Win[hWnd, lParam, "bc"]) {
            DllCall("gdi32.dll\SetBkColor", "Ptr", wParam, "UInt", bc)
        }
        Return Win[hWnd, lParam, "Brush"] ; Return the HBRUSH to notify the OS that we altered the HDC.
    }

    If (hWnd = "Set") {
        a := uMsg
        Win[a.g, a.c] := a
        If (Win[a.g, a.c, "tc"] == "") And (Win[a.g, a.c, "bc"] == "")
            Win[a.g].Remove(a.c, "")
        If Not Win[a.g, "WindowProcOld"]
            Win[a.g,"WindowProcOld"] := DllCall("SetWindowLong" . (A_PtrSize == 8 ? "Ptr" : "")
        , "Ptr", a.g, "Int", -4, "Ptr", RegisterCallback("CC_WindowProc", "", 4), "UPtr")
        If Win[a.g, a.c, "Brush"]
            DllCall("gdi32.dll\DeleteObject", "Ptr", Brush)
        If (Win[a.g, a.c, "bc"] != "")
            Win[a.g, a.c, "Brush"] := DllCall("gdi32.dll\CreateSolidBrush", "UInt", a.bc, "UPtr")
        Return
    }
    Return DllCall("CallWindowProc", "Ptr", Win[hWnd, "WindowProcOld"], "Ptr", hWnd, "UInt", uMsg, "Ptr", wParam, "Ptr", lParam, "Ptr")
}
garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Trying to change font options in GUI command link buttons

Post by garry » 12 Apr 2021, 16:31

another example with picture and color button
click on first picture with 4 buttons + 1 color button
I tried with 4K and full HD , don't know how to change the fontsize depending screen resolution

Code: Select all

;- click on first picture with 4 buttons & 2nd color button
#warn
setworkingdir,%a_scriptdir%
CoordMode, Mouse , Relative
wa:=A_screenwidth,ha:=A_screenHeight,xx:=100  ;- 4k= 3840*2160
filename1=TEST CLICK PICTURE-BUTTON
url=https://i.imgur.com/eGQLMcU.png
pic01=%a_scriptdir%\PictureClick.png
ifnotexist,%pic01%
  urldownloadtofile,%url%,%pic01%
Gui,3:default
Gui,3: -DPIScale -Caption +AlwaysOnTop
gui,3:color,black,black

content :=
(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Nec ultrices dui sapien eget. Feugiat scelerisque
varius morbi enim nunc faucibus a."
)
;-- maybe change the FontSize / not proportional to screen resolution
Gui,3: Font, s18 cYellow, Verdana
x:=(wa*1)/xx,y:=(ha*1.5)/xx
Gui,3: Add, Text,x%x% y%y% +0x200, Title
;-----------
Gui,3: Font, s12 Bold
y:=(ha*6)/xx
Gui,3: Add, Text, x%x% y%y% +0x200, Subtitle
;-----------
Gui,3: Font, s10 Norm
y:=(ha*10)/xx
Gui,3: Add, Text,x%x% y%y%, % content
;-----------
x:=(wa*.2)/xx,y:=(ha*21)/xx,w:=(wa*10)/xx,h:=(ha*5)/xx
Gui,3:add,Picture, x%x% y%y% w%w% h%h% gDnlX ,%pic01%
;-----------
x:=(wa*11)/xx,y:=(ha*21)/xx,w:=(wa*7)/xx,h:=(ha*2.8)/xx
Gui,3:Add,Progress,            x%x%    y%y%    w%w%   h%h%  Disabled BackgroundRed
Gui,3:Add,Text,                xp    yp     wp    hp   cYellow  BackgroundTrans Center 0x200 gA1,TEST
;-------------------
x:=(wa*10)/xx,y:=(ha*10)/xx,w:=(wa*35)/xx,h:=(ha*33)/xx
Gui,3:show,x%x% y%y% w%w% h%h%,%filename1%
OnMessage(0x201, "WM_LBUTTONDOWN")
return
;----------------------------------------------
esc::exitapp
;----------------------------------------------
3Guiclose:
exitapp
;----------------------------------------------
A1:
msgbox, 262208, ,Button-TEST,2
return
;----------------------------------------------
DNLx:
mousegetpos,x,y,,CL
;-----------------
wdf:=(wa*1.56)/xx
hdf:=(ha*2.55)/xx
;-----------------
y11:=(ha* 22 )/xx
;-----------------
x11:=(wa* 0.68)/xx
x12:=(wa* 3.15)/xx
x13:=(wa* 5.68)/xx
x14:=(wa* 8.20)/xx
;------------------
if (CL = "Static4")
  {
  if (x>x11) and x<(x11+wdf) and (y>y11) and y<(y11+hdf)
    msgbox, 262208, ,Button-1 Loudspeaker,2
  if (x>x12) and x<(x12+wdf) and (y>y11) and y<(y11+hdf)
    msgbox, 262208, ,Button-2 Microfon,2
  if (x>x13) and x<(x13+wdf) and (y>y11) and y<(y11+hdf)
    msgbox, 262208, ,Button-3 Camera,2
  if (x>x14) and x<(x14+wdf) and (y>y11) and y<(y11+hdf)
    msgbox, 262208, ,Button-4 Telephon,2
  }
return
;------------------- function drag & move the GUI -----------------
WM_LBUTTONDOWN(wParam, lParam, msg, hwnd)
{
Gui, +LastFound
Checkhwnd := WinExist()
if hwnd = %Checkhwnd%
   PostMessage, 0xA1, 2
;- 0x201 is the number for Windows Message WM_LBUTTONDOWN, which is the message Windows sends when the mouse clicks on our window.
;- 0xA1 is WM_NCLBUTTONDOWN, to make Windows think we clicked on the non-client area of the window (the border).
;- The "2" tells windows we clicked on caption at the top of the window, as if to drag it.
}
;================= END SCRIPT ============================
phyrburn
Posts: 13
Joined: 24 Jul 2019, 05:55

Re: Trying to change font options in GUI command link buttons

Post by phyrburn » 13 Apr 2021, 05:46

garry wrote:
12 Apr 2021, 16:31
another example with picture and color button
click on first picture with 4 buttons + 1 color button
I tried with 4K and full HD , don't know how to change the fontsize depending screen resolution

Code: Select all

;- click on first picture with 4 buttons & 2nd color button
#warn
setworkingdir,%a_scriptdir%
CoordMode, Mouse , Relative
wa:=A_screenwidth,ha:=A_screenHeight,xx:=100  ;- 4k= 3840*2160
filename1=TEST CLICK PICTURE-BUTTON
url=https i.imgur.com /eGQLMcU.png  Broken Link for safety
pic01=%a_scriptdir%\PictureClick.png
ifnotexist,%pic01%
  urldownloadtofile,%url%,%pic01%
Gui,3:default
Gui,3: -DPIScale -Caption +AlwaysOnTop
gui,3:color,black,black

content :=
(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Nec ultrices dui sapien eget. Feugiat scelerisque
varius morbi enim nunc faucibus a."
)
;-- maybe change the FontSize / not proportional to screen resolution
Gui,3: Font, s18 cYellow, Verdana
x:=(wa*1)/xx,y:=(ha*1.5)/xx
Gui,3: Add, Text,x%x% y%y% +0x200, Title
;-----------
Gui,3: Font, s12 Bold
y:=(ha*6)/xx
Gui,3: Add, Text, x%x% y%y% +0x200, Subtitle
;-----------
Gui,3: Font, s10 Norm
y:=(ha*10)/xx
Gui,3: Add, Text,x%x% y%y%, % content
;-----------
x:=(wa*.2)/xx,y:=(ha*21)/xx,w:=(wa*10)/xx,h:=(ha*5)/xx
Gui,3:add,Picture, x%x% y%y% w%w% h%h% gDnlX ,%pic01%
;-----------
x:=(wa*11)/xx,y:=(ha*21)/xx,w:=(wa*7)/xx,h:=(ha*2.8)/xx
Gui,3:Add,Progress,            x%x%    y%y%    w%w%   h%h%  Disabled BackgroundRed
Gui,3:Add,Text,                xp    yp     wp    hp   cYellow  BackgroundTrans Center 0x200 gA1,TEST
;-------------------
x:=(wa*10)/xx,y:=(ha*10)/xx,w:=(wa*35)/xx,h:=(ha*33)/xx
Gui,3:show,x%x% y%y% w%w% h%h%,%filename1%
OnMessage(0x201, "WM_LBUTTONDOWN")
return
;----------------------------------------------
esc::exitapp
;----------------------------------------------
3Guiclose:
exitapp
;----------------------------------------------
A1:
msgbox, 262208, ,Button-TEST,2
return
;----------------------------------------------
DNLx:
mousegetpos,x,y,,CL
;-----------------
wdf:=(wa*1.56)/xx
hdf:=(ha*2.55)/xx
;-----------------
y11:=(ha* 22 )/xx
;-----------------
x11:=(wa* 0.68)/xx
x12:=(wa* 3.15)/xx
x13:=(wa* 5.68)/xx
x14:=(wa* 8.20)/xx
;------------------
if (CL = "Static4")
  {
  if (x>x11) and x<(x11+wdf) and (y>y11) and y<(y11+hdf)
    msgbox, 262208, ,Button-1 Loudspeaker,2
  if (x>x12) and x<(x12+wdf) and (y>y11) and y<(y11+hdf)
    msgbox, 262208, ,Button-2 Microfon,2
  if (x>x13) and x<(x13+wdf) and (y>y11) and y<(y11+hdf)
    msgbox, 262208, ,Button-3 Camera,2
  if (x>x14) and x<(x14+wdf) and (y>y11) and y<(y11+hdf)
    msgbox, 262208, ,Button-4 Telephon,2
  }
return
;------------------- function drag & move the GUI -----------------
WM_LBUTTONDOWN(wParam, lParam, msg, hwnd)
{
Gui, +LastFound
Checkhwnd := WinExist()
if hwnd = %Checkhwnd%
   PostMessage, 0xA1, 2
;- 0x201 is the number for Windows Message WM_LBUTTONDOWN, which is the message Windows sends when the mouse clicks on our window.
;- 0xA1 is WM_NCLBUTTONDOWN, to make Windows think we clicked on the non-client area of the window (the border).
;- The "2" tells windows we clicked on caption at the top of the window, as if to drag it.
}
;================= END SCRIPT ============================
I would never have even dreamt about layering text onto a progress element for the effect of a "flat" solid button. Interesting way of doing it and although doesn't answer my original question, it gives me an alternate solution. Thank you for that. :)

For dynamic font size, you could check the current resolution before GUI creation and have a series of variables with font size values on a per resolution basis and just use the variable name when setting the font GUI options. But I guess if you are unsure of how varied the users resolutions are, this could be tricky to deal with.
garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Trying to change font options in GUI command link buttons

Post by garry » 13 Apr 2021, 07:17

@phyrburn thank you for answer about fontsize
I used this , but it's not the solution ( I use a small/slow/noiseless computer beelink with 4K 27" monitor LG , it's good for my eyes ... :) )

Code: Select all

;---------------------------------------------
wa:=A_screenwidth,ha:=A_screenHeight,xx:=100
if (wa=3840)
   FS1=18
else if (wa=1920)
   FS1=12
else
   FS1=7
;-------------------
if (wa=3840)
   FS2=14
else if (wa=1920)
   FS2=10
else
   FS2=7
Gui,1:Font,S%FS1% CYellow,Lucida Console
;--
Gui,1:Font,S%FS2% CYellow,Lucida Console

;---------------------------------------------
Post Reply

Return to “Ask for Help (v1)”