Search code examples
autohotkey

In a Gui - How to know when a Progress Control is clicked on?


Gui Main:New,+AlwaysOnTop +ToolWindow -Caption +HwndMainHwnd, Swatches
Gui Main:Font,s14,Consolas

Gui Main:Add,Progress, ConSwatch1 cblue x04 y04 w35 h35, 100
Gui Main:Add,Progress, ConSwatch2 cred x04 y+4 w35 h35, 100
Gui Main:Add,Progress, ConSwatch3 cgreen x04 y+4 w35 h35, 100

Gui Main:Show, x300 y300 w84 h356 NA 

I have a simple GUI like the above, it consists of various colour swatches. I want to be notified when the user clicks on one of them, so I can do something, I tried:

Gui Main:New,+AlwaysOnTop +ToolWindow -Caption +HwndMainHwnd, Swatches
Gui Main:Font,s14,Consolas

Gui Main:Add,Progress, ConSwatch1 cblue gDoBlue x04 y04 w35 h35, 100
Gui Main:Add,Progress, ConSwatch2 cred gDoRed x04 y04 w35 h35, 100
Gui Main:Add,Progress, ConSwatch3 cgreen gDoGreen x04 y04 w35 h35, 100

Gui Main:Show, x300 y300 w84 h356 NA 

gDoBlue
MsgBox, Doing blue...

But running it I get an error, saying I cant have a label/subroutine with this control.

This was a neat trick I picked up from a user on the AHK Reddit forum, its the only control that can be used to get a series of colour swatches on a GUI. But I really want to "Apply" the clicked on colour.


Solution

  • You could use something like this:

    Gui Main:New,+AlwaysOnTop +ToolWindow -Caption +HwndMainHwnd, Swatches
    Gui Main:Font,s14,Consolas
    
    Gui Main:Add,Text,  x04 y04 w35 h35 gblue
    Gui Main:Add,Progress, ConSwatch1 cblue xp yp w35 h35, 100
    
    Gui Main:Add,Text, x04 y+4 w35 h35 gred
    Gui Main:Add,Progress, ConSwatch2 cred xp yp w35 h35, 100
    
    Gui Main:Add,Text, x04 y+4 w35 h35 ggreen
    Gui Main:Add,Progress, ConSwatch3 cgreen xp yp w35 h35, 100
    
    Gui Main:Show, x300 y300 w84 h356 NA 
    Return
    
    blue:
        Msgbox  %   "blue"
    Return
    
    red:
        Msgbox  %   "red"
    Return
    
    green:
        Msgbox  %   "green"
    Return