http://www.autohotke...pic.php?t=86426
Foreword: This post is NOT about adding a Picture to a GUI Button.
If you are interested and want to know How to Create a Picture Button,
you may visit corrupt's post: Graphic Buttons
PART 1 : Efficiently using picture(s) to Simulate the Effect of Button Press.
A regular GUI Button is a 3D object. That is, the Button is displayed projected (in default state) and looks sunkenwhen depressed. The GUI Button remains sunken until you release the Mouse Button ( gLabel will not be executed unless the mouse button is released).
A Button has two states - ON ( Sunken ) / OFF ( Projected ) . Its obvious that we need two semi identical pictures to display the sunken & projected state of a button.
Do we always need two pictures to simulate a Button Press?
The answer is NO! In most cases a single picture would suffice.
Instead of the Projected & Sunken states of a Button I have attempted to simulate Flat & Sunken
states using a single ICON with the following code:
Gui, -Sysmenu +Toolwindow Gui, Add , Picture, x13 y13 w32 h32 E0x200 vState1 icon1 , %A_AhkPath% Gui, Add , Picture, x+10 y13 w34 h34 Border vState0 icon1 , %A_AhkPath% Gui, Add, Text , x13 ,E0x200 Border Gui, Show, x50 y50 AutoSize, Picture Options Return GuiEscape: ExitApp ReturnSnapshot: [*:1zcyg4tm]Have used Border in the Picture's option for drawing a thin border around the Flat Button
[*:1zcyg4tm]Have used an Extend Style E0x200 in the Picture's option for simulating the Sunken Button effect.
[*:1zcyg4tm]The default size of Icon is w32 h32, but Extend Style E0x200 increases the size of the Sunken Button
[*:1zcyg4tm]Flat button has been sized two pixels extra to match the size of Sunken Button.
The following code overlaps the two Styles (of the same Icon) to simulate a Graphical Checkbox
; Toggle.ahk - Readymade code for a Graphical Checkbox Gui, +Sysmenu +ToolWindow Gui, Margin,12,12 Gui, Add , Picture, x13 y13 w32 h32 E0x200 vState1 icon1 gSubRoutine1, %A_AhkPath% Gui, Add , Picture, x13 y13 w34 h34 Border vState0 icon1 gSubRoutine1, %A_AhkPath% GoSub, Toggle_Switch Gui, Show, x50 y50 AutoSize, Toggle Return Toggle_Switch: If Toggle=0 { GuiControl, Hide, State0 GuiControl, Show, State1 Toggle=1 } Else { GuiControl, Hide, State1 GuiControl, Show, State0 Toggle=0 } Return SubRoutine1: GoSub,Toggle_Switch ; < - - - - - - - - P U T Y O U R C O D E H E R E Return GuiEscape: GuiClose: ExitApp ReturnSnapshots: <-OFF/ON-> [/list]
The following code overlaps the two Styles (of the same Icon) to simulate a Graphical Button.
;RegularButton.ahk - Readymade Code testing a Graphical Button Gui, +Sysmenu +ToolWindow Gui, Margin,12,12 Gui, Add , Picture, x13 y13 w34 h34 E0x200 vState1 icon1 gSubRoutine1, %A_AhkPath% Gui, Add , Picture, x13 y13 w32 h32 0x400000 vState0 icon1 gSubRoutine1, %A_AhkPath% Gui, Show, x50 y50 AutoSize, Button Return SubRoutine1: GuiControl, Hide, State0 Sleep 250 GuiControl, Show, State0 Msgbox, < - - - - - - - - P U T T H E M A I N C O D E H E R E Return GuiEscape: GuiClose: ExitApp ReturnNote: Unlike in a Graphical Checkbox, I have used 0x400000 to display the Graphical Button projected.
How to Simulate a Graphical Button in a GUI?
PART 2 : An Advanced Technique - MPPS type Button
A regular GUI Button is a 3D object. That is, the Button is displayed projected (in default state) and looks sunken when depressed. The GUI Button remains sunken until you release the Mouse Button ( gLabel will not be executed unless the mouse button is released).
PART 1 dealt with the techniques in creating a Graphical Button / Graphical Checkbox and
we know that a Push button has two states : ON and OFF. Both the examples posted were
calling Subroutine1 which we could use as a Toggle switch.
In this PART, I am posting the resultant code of my experiment to simulate a third state.
The code demonstrates the detection of Button-Press duration of a Graphical button.
In simple terms:
[*:1zcyg4tm]a Normal Mouse click on the Graphical button will be used to toggle a variable ( calls Subroutine1 ) , and
[*:1zcyg4tm]a Long Mouse click on the Graphical button will trigger a special routine ( calls Subroutine2 ).
; MPPS_Type_Button.ahk ;#InstallMouseHook /* - A Normal Click on the Graphical button will call Subroutine0 - A Long Click (Keep the "Left Mouse Button" down on the graphical button for 1 second) will trigger Subroutine1 */ Gui, +Sysmenu +ToolWindow +AlwaysOnTop Gui, Margin,12,12 Gui, Add , Picture, x13 y13 w32 h32 E0x200 vState1 icon1 gMPPS_Type_Button, %A_AhkPath% Gui, Add , Picture, x13 y13 w34 h34 Border vState0 icon1 gMPPS_Type_Button, %A_AhkPath% Gui, Show, x50 y50 AutoSize, MPPS Return MPPS_Type_Button: GuiControl Hide, State0 TC := A_TickCount Loop, { MouseDown:=GetKeyState("LButton","P") If (!MouseDown) { LongClick=0 Break } If (A_TickCount-TC) > 1000 { LongClick=1 Break } } GuiControl Show, State0 IfEqual,LongClick,1,GoSub,SubRoutine1 else GoSub, SubRoutine0 Return SubRoutine1: Msgbox,64, Subroutine1 ,That was a Long Click..`n`nThis Subroutine may be used to execute a Special task, 10 Return SubRoutine0: Msgbox,64, Subroutine0 ,That was a Normal Click..`n`nThis Subroutine may be used to Toggle a Variable, 10 Return GuiEscape: GuiClose: ExitApp Return[*:1zcyg4tm] MPPS type button requires Mouse Hook (Windows NT/2000/XP or later)[/list]
MPPS in MPPS type button stands for
Mobile Phone Power Switch
The Acronym that came to my mind when I applied the concept to a Graphical Button.
I do not know about international preferences for Mobiles,
but in India, 80% of the Mobiles are from Nokia and the function of its Power button is like follows:-
[*:1zcyg4tm]A Long press switches it ON
and when ON
[*:1zcyg4tm]A Short press will show a Confirmation menu to switch it off
[*:1zcyg4tm]A Long press will switch it Off without confirmation.I have mimicked this functionality for the Graphic Button.
Visit the following post for a Utility based on this concept.
Crazy Scripting : Muter v1.0 ( MPPS type Button )
Credits: Post by PhiLho @ Ask for Help Topic: Picture buttons.... by jsmain
Regards,
PS: Feedback please!