How to add picture and different size text (like emojis) to a GUI button? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
CChaos
Posts: 2
Joined: 16 Sep 2021, 01:29

How to add picture and different size text (like emojis) to a GUI button?

Post by CChaos » 16 Sep 2021, 02:12

I'm trying to add icon to button in order to make them more distinguishable.

something like this:
Image

Code: Select all

a := Gui(,'test')
name := 'tester'
emo := '✔️'
pic := '%A_ScriptPath%\Compiler\Ahk2Exe.ico'
loop 6 {
    ty := (A_Index-1) // 2 + 1
    tx := Mod(A_Index-1, 2) + 1
    tPX := 100*(tx-1)
    tPY := 100*(ty-1)
    a.AddButton('0x800 0x8000 -Wrap x' tPX ' y' tPY ' w' 100 ' h' 100,name)
    if Mod(A_Index,3) = 0 {
        a.AddPicture('BackgroundTrans x' tPX+20//2 ' y' tPY ' w' 80 ' h' 80,pic)
    } else {
        a.AddText('BackgroundTrans Center x' tPX ' y' tPY ' w' 100 ' h' 80,emo).SetFont('S' 80//4*3)
    }
}
a.Show()




But there is a problem, when ever my mouse hover over a button, the icon will disappear and it will never come back.

Demonstration:
Image

Am I missing something here?

Are there a proper way to add icon which makes it always on top of button?

Or a way to "reAdd" the icon when ever it get cover by the button?

AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: How to add picture and different size text (like emojis) to a GUI button?  Topic is solved

Post by AHK_user » 16 Sep 2021, 17:40

Check out the library of this post, your mind will be blown away :lol:

viewtopic.php?f=83&t=86124

if you include this library, it gives the gui object extra methods like "AddPicButton"

CChaos
Posts: 2
Joined: 16 Sep 2021, 01:29

Re: How to add picture and different size text (like emojis) to a GUI button?

Post by CChaos » 18 Sep 2021, 03:30

Cool! I checked the topic you mentioned and the source code on github.

It is using SendMessage funtion to sent BM_SETIMAGE to the button control.

The code will be something like this:

Code: Select all

a := Gui(,'test')
name := 'tester'
pic := A_WorkingDir '\Compiler\Ahk2Exe.ico'
b := []
loop 6 {
    b.push(0)
    tPX := 100*(Mod(A_Index-1, 2))
    tPY := 100*((A_Index-1) // 2)
    b[A_Index] := a.AddButton('0x400 x' tPX ' y' tPY ' w' 100 ' h' 100,name)
    img := LoadPicture(pic,'w80 h80',&_type)
    SendMessage(0xF7, _type, img, b[A_Index].hwnd)
}
a.Show()
This works perfectly.

And I also find another way to achieve my goal,
that is to use .OnNotify to call a function when ever my mouse enter or leave the button's workzone.

And here is the code:

Code: Select all

a := Gui(,'test')
name := 'tester'
emo := '✔️'
pic := A_WorkingDir '\Compiler\Ahk2Exe.ico'
b := []
loop 6 {
    b.push(0)
    tPX := 100*(Mod(A_Index-1, 2))
    tPY := 100*((A_Index-1) // 2)
    b[A_Index] := a.AddButton('0x800 x' tPX ' y' tPY ' w' 100 ' h' 100,name)
    b[A_Index].OnNotify(-1249,ReDrawthisIcon)
    b[A_Index].icon := false
    if Mod(A_Index,3) = 0 {
        b[A_Index].icon := a.AddPicture('BackgroundTrans x' tPX+20//2 ' y' tPY ' w' 80 ' h' 80,pic)
    } else {
        b[A_Index].icon := a.AddText('BackgroundTrans Center x' tPX ' y' tPY ' w' 100 ' h' 80,emo)
        b[A_Index].icon.SetFont('S' 80//4*3)
    }
}
a.Show()

ReDrawthisIcon(tObj,*) {
    if tObj.icon {
        tObj.icon.Redraw()
    }
    return
}
The icon on the button will have some flickering, but I can use emoji on it instead of only picture.

Anyway thank you for your reply.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to add picture and different size text (like emojis) to a GUI button?

Post by swagfag » 18 Sep 2021, 13:21

u can subclass the control and draw something urself in WM_PAINT
downside is ud also have to pretty much do everything else urself, if u want anything remotely resembling a standard win32 control - hover/focus/tabbing/disabled/etc

Post Reply

Return to “Ask for Help (v2)”