Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[How to] Simulate a Graphical Button in a GUI


  • Please log in to reply
7 replies to this topic
SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
How to Simulate a Graphical Button in a GUI?
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
Return
Snapshot: Posted Image[*: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
Return
Snapshots: Posted Image <-OFF/ON-> Posted Image[/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 
Return
Note: 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!
kWo4Lk1.png

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
What is MPPS?

I tested the code in the first part, it is excellent! I like the sunken effect done without using two pictures. Very good idea!
The second part I will test later, because I think it won't work on Win98 (I am on my old computer right now).
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear PhiLho, :)

I tested the code in the first part, it is excellent! I like the sunken effect done without using two pictures. Very good idea!


Thanks! :D. I did refer your post http://www.autohotke...p?p=52707#52707
and forgot to give credit. :( (It took me more than 5 hours to compile this post, and it slipped off my mind!).
Now, I have edited the post & have given the credit.

The second part I will test later, because I think it won't work on Win98 (I am on my old computer right now).


I await your valuable opinion on the MPPS type Button. :)

Regards, :)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
You can simulate button up/down effects also with GroupBoxes.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

I await your valuable opinion on the MPPS type Button. :)

Not much to say, it offers an interesting functionnality, and it works well. I still don't know what is MPPS...

Note on lines: you can make them with a Picture element, using a one pixel image (or 2/3 pixels for shadow effects). I still have to study how to use an image defined in the script without writing it to disk.

Side-note: I think this topic belongs more to the Scripts & Functions section, this one is for topics slightly outside AHK's realm (external utilities that can be used with AutoHotkey). What do you think?
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear PhiLho, :)

Not much to say, it offers an interesting functionnality, and it works well. I still don't know what is MPPS...


:D. I have particularly not replied it - means there is an Unread PM lying in your inbox :D

Note on lines: you can make them with a Picture element, using a one pixel image (or 2/3 pixels for shadow effects). I still have to study how to use an image defined in the script without writing it to disk.


I am aware of that technique.

I think this topic belongs more to the Scripts & Functions section, this one is for topics slightly outside AHK's realm (external utilities that can be used with AutoHotkey). What do you think?


Ask for Help
Ask questions and (hopefully) get answers. Post helpful tips and tricks.


Or maybe in the above section? :D
I will have to bump the topic two/three times a day to keep it in the first page.

I am still learning AHK & whenever I come out with interesting things, I want to post it under one topic. And I want this topic to be easily accessible so that it does not get lost in this ocean of posts.

Not everybody can search successfully. Especially, when they cannot put in words of what they have in mind. Don`t you agree?

I leave it to Mr.Chris to decide where this topic should be.

Regards, :)

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

Not much to say, it offers an interesting functionnality, and it works well. I still don't know what is MPPS...


:D. I have particularly not replied it - means there is an Unread PM lying in your inbox :D

? Oh, you sent a Private Message. I have no access to my private mailbox during the day, and I fail to see the "You have 1 new message" link in the forum. :-P
OK, you made up this abbreviation. But it is fine, you can "disclose it", I think, it is a smart and useful use of graphic buttons.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

maestrith
  • Members
  • 786 posts
  • Last active: Apr 10 2019 01:28 PM
  • Joined: 17 Sep 2005

I really like this script!

I made a small change.  Let me know what you think.

; 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
while,GetKeyState("LButton","P"){
	longclick:=A_TickCount-TC>1000?1:0
	if longclick
	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