AutoHotkey Community

It is currently May 27th, 2012, 11:46 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: June 21st, 2010, 8:54 pm 
Offline

Joined: December 17th, 2008, 5:36 am
Posts: 80
Function creates the ability to make a button out of any image of your choosing. Graphic button can have 3 different image states based on user input.

1. Up, no user interaction.
2. Hover, mouse cursor over image.
3. Down, left mouse button pushed on image.

Each created button has Two Gosub States. Gosub Down and Gosub Down_Up. Gosub Down is launched as soon as the left mouse button is pushed down over the image. Gosub Down_Up is launched as soon as the left mouse button transitions from down to up over the image. Which one you use to execute your code is up to you but to follow within normal button operations you would use the Down_Up Gsub. There may be something else better out there I honestly don't know, but it seems to do the trick.


Example Code Screen Shots:
Mouse hovering over Icon --------------- -----------------Mouse Clicking icon
ImageImage
Example Code:
Code:
; ********************************
; Basic Demo Script (Copy, Paste, and Run)
; ********************************
#SingleInstance
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


IfNotExist %A_WorkingDir%\Down.png
{
   SplashTextOn, 300, 30, !, Downloading images. Please wait...
   URLDownloadToFile, http://www.autohotkey.net/~kdoske/3State_Button_Function/Down.png, Down.png
   URLDownloadToFile, http://www.autohotkey.net/~kdoske/3State_Button_Function/Hover.png, Hover.png
   URLDownloadToFile, http://www.autohotkey.net/~kdoske/3State_Button_Function/Up.png, Up.png
    SplashTextOff
}

Start:
AddGraphicButton(1, "x10", "y10", "h49", "w224", "button1", a_ScriptDir . "\Up.png", a_ScriptDir . "\Hover.png", a_ScriptDir . "\Down.png")
AddGraphicButton(1, "x10", "y+10", "h49", "w224", "button2", a_ScriptDir . "\Up.png", a_ScriptDir . "\Hover.png", a_ScriptDir . "\Down.png")
AddGraphicButton(1, "x10", "y+40", "h49", "w224", "button3", a_ScriptDir . "\Up.png", a_ScriptDir . "\Hover.png", a_ScriptDir . "\Down.png")
AddGraphicButton(1, "x10", "y+0", "h49", "w224", "button4", a_ScriptDir . "\Up.png", a_ScriptDir . "\Hover.png", a_ScriptDir . "\Down.png")
OnMessage(0x200, "MouseMove")
OnMessage(0x201, "MouseLDown")
OnMessage(0x202, "MouseLUp")
Gui, Show,
return

Button1_down:
tooltip, Button1 down stroke gsub launched
return
Button1_down_up:
tooltip, Button1 down_up stroke gsub launched
return
Button2_down:
tooltip, Button2 down stroke gsub launched
return
Button2_down_up:
tooltip,Button2 down_up stroke gsub launched
return
Button3_down:
tooltip, Button3 down stroke gsub launched
return
Button3_down_up:
tooltip, Button3 down_up stroke gsub launched
return
Button4_down:
tooltip, Button4 down stroke gsub launched
return
Button4_down_up:
tooltip, Button4 down_up stroke gsub launched
return

GUIClose:
Gui, 1:Destroy
ExitApp

           
MouseMove(wParam, lParam, msg, hwnd)
{
   Global
   local Current_Hover_Image
   local Current_Main_Image
   local Current_GUI
   loop, parse, Graphic_Button_List, |
   {
      Current_GUI := %a_loopField%_GUI_Number
      If (hwnd = %a_loopField%_HWND) and (%a_loopField%LastButtonData1 != %a_loopField%_HWND)
      {
         Current_Hover_Image := %a_loopField%_Hover_Image
         guicontrol, %Current_GUI%:, %a_loopField%, %Current_Hover_Image%
         %a_loopField%LastButtonData1 := hwnd
      }
      else if(hwnd != %a_loopField%_HWND) and (%a_loopField%LastButtonData1 = %a_loopField%_HWND)
      {
         Current_Up_Image := %a_loopField%_Up_Image
         guicontrol, %Current_GUI%:, %a_loopField%, %Current_Up_Image%
         %a_loopField%LastButtonData1 := hwnd
         %a_loopField%LastButtonData2 =
       tooltip,
      }
   }
   Return
}

MouseLDown(wParam, lParam, msg, hwnd)
{
   Global
   Local Current_Down_Image
   Local Current_GUI
   loop, parse, Graphic_Button_List, |
   {
      If (hwnd = %a_loopField%_HWND) and (%a_loopField%LastButtonData2 != %a_loopField%_HWND)
      {
         Current_GUI := %a_loopField%_GUI_Number
         Current_Down_Image := %a_loopField%_Down_Image
         guicontrol, %Current_GUI%:, %a_loopField%, %Current_Down_Image%
         %a_loopField%LastButtonData2 := hwnd
         break
      }
   }
   Return
}

MouseLUp(wParam, lParam, msg, hwnd)
{
   Global
   local Current_Main_Image
   Local Current_GUI
   loop, parse, Graphic_Button_List, |
   {
      If (hwnd = %a_loopField%_HWND) and (%a_loopField%LastButtonData2 = %a_loopField%_HWND)
      {
         Current_GUI := %a_loopField%_GUI_Number
         Current_Hover_Image := %a_loopField%_Hover_Image
         guicontrol, %Current_GUI%:, %a_loopField%, %Current_Hover_Image%
         %a_loopField%LastButtonData2 =
         GOSUB % a_loopField . "_Down_Up"
         break
      }
   }
   Return
}

AddGraphicButton(GUI_Number, Button_X, Button_Y, Button_H, Button_W, Button_Identifier, Button_Up, Button_Hover, Button_Down)
{
   Global
   if(Graphic_Button_List = "")
      Graphic_Button_List .= Button_Identifier
   else
      Graphic_Button_List .= "|" . Button_Identifier
   current_Button_HWND := Button_Identifier . "_hwnd"
   %Button_Identifier%_Up_Image := Button_Up
   %Button_Identifier%_Hover_Image := Button_Hover
   %Button_Identifier%_Down_Image := Button_Down
   %Button_Identifier%_GUI_Number := GUI_Number   
   Gui, %GUI_Number%:Add, Picture, +altsubmit %Button_X% %Button_Y% %Button_H% %Button_W% g%Button_Identifier%_Down v%Button_Identifier% hwnd%current_Button_HWND%, %Button_Up%
}


Function:
Code:
; *******************************************************************
; AddGraphicButton.ahk
; *******************************************************************
; Version: 1.0 Updated: Jun 21, 2010
; by Kdoske
; http://www.autohotkey.com/forum/viewtopic.php?p=364153#364153
;*******************************************************************
;GUI_Number: The GUI number
;Button_X: X Position of button - "x10"
;Button_Y: Y Position of button - "y+10"
;Button_H: Height of button - "h50"
;Button_w: Width of button - "W50"
;Button_Identifier: This will be used as the Variable Name, HWND, and Gusubs.
;Button_Variable - %Button_Identifier%
;Button_HWND - %Button_Identifier%_hwnd
;Button_Gosubs: There are two gosubs generated for each button. Gosub Down and Gosub Down_Up. Gosub Down is launched when the image is clicked on the downward stroke. Gosub Down_up is launched only when the image is clicked down and the mouse button is released over the image. (When you want the button to execute code is up to you)
;Image Gosub down = %Button_Identifier%_Down
;Image Gosub down_up = %Button_Identifier%_Down_up
;Button_Up: Image location of button in up state
;Button_Hover: Image location of button in hover state
;Button_Down: Image location of button in Downstate
; *******************************************************************

AddGraphicButton(GUI_Number, Button_X, Button_Y, Button_H, Button_W, Button_Identifier, Button_Up, Button_Hover, Button_Down)
{
   Global
   if(Graphic_Button_List = "")
      Graphic_Button_List .= Button_Identifier
   else
      Graphic_Button_List .= "|" . Button_Identifier
   current_Button_HWND := Button_Identifier . "_hwnd"
   %Button_Identifier%_Up_Image := Button_Up
   %Button_Identifier%_Hover_Image := Button_Hover
   %Button_Identifier%_Down_Image := Button_Down
   %Button_Identifier%_GUI_Number := GUI_Number   
   Gui, %GUI_Number%:Add, Picture, +altsubmit %Button_X% %Button_Y% %Button_H% %Button_W% g%Button_Identifier%_Down v%Button_Identifier% hwnd%current_Button_HWND%, %Button_Up%
}

MouseMove(wParam, lParam, msg, hwnd)
{
   Global
   local Current_Hover_Image
   local Current_Main_Image
   local Current_GUI
   loop, parse, Graphic_Button_List, |
   {
      Current_GUI := %a_loopField%_GUI_Number
      If (hwnd = %a_loopField%_HWND) and (%a_loopField%LastButtonData1 != %a_loopField%_HWND)
      {
         Current_Hover_Image := %a_loopField%_Hover_Image
         guicontrol, %Current_GUI%:, %a_loopField%, %Current_Hover_Image%
         %a_loopField%LastButtonData1 := hwnd
      }
      else if(hwnd != %a_loopField%_HWND) and (%a_loopField%LastButtonData1 = %a_loopField%_HWND)
      {
         Current_Up_Image := %a_loopField%_Up_Image
         guicontrol, %Current_GUI%:, %a_loopField%, %Current_Up_Image%
         %a_loopField%LastButtonData1 := hwnd
         %a_loopField%LastButtonData2 =
      }
   }
   Return
}

MouseLDown(wParam, lParam, msg, hwnd)
{
   Global
   Local Current_Down_Image
   Local Current_GUI
   loop, parse, Graphic_Button_List, |
   {
      If (hwnd = %a_loopField%_HWND) and (%a_loopField%LastButtonData2 != %a_loopField%_HWND)
      {
         Current_GUI := %a_loopField%_GUI_Number
         Current_Down_Image := %a_loopField%_Down_Image
         guicontrol, %Current_GUI%:, %a_loopField%, %Current_Down_Image%
         %a_loopField%LastButtonData2 := hwnd
         break
      }
   }
   Return
}

MouseLUp(wParam, lParam, msg, hwnd)
{
   Global
   local Current_Main_Image
   Local Current_GUI
   loop, parse, Graphic_Button_List, |
   {
      If (hwnd = %a_loopField%_HWND) and (%a_loopField%LastButtonData2 = %a_loopField%_HWND)
      {
         Current_GUI := %a_loopField%_GUI_Number
         Current_Hover_Image := %a_loopField%_Hover_Image
         guicontrol, %Current_GUI%:, %a_loopField%, %Current_Hover_Image%
         %a_loopField%LastButtonData2 =
         GOSUB % a_loopField . "_Down_Up"
         break
      }
   }
   Return
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 22nd, 2010, 11:52 am 
Offline

Joined: April 14th, 2009, 10:40 am
Posts: 182
Nice script but I've already done a similar script and had the same problem!
If you move the cursor to quickly over a button, it may stay on "Mouse Over" statement.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 22nd, 2010, 3:08 pm 
Offline

Joined: December 17th, 2008, 5:36 am
Posts: 80
TomXIII wrote:
Nice script but I've already done a similar script and had the same problem!
If you move the cursor to quickly over a button, it may stay on "Mouse Over" statement.


Yes if your button is near the edge of the window and you jettison your cursor quickly off the scripts window the button will remain in the 'Hover' State. However as soon as you place your cursor back over the scripts window all is returned to the correct state. It really shouldn't even be an issue, I doubt any user would even realize because if they are moving there cursor of the screen that quickly they are obviously navigating somewhere else. If its still a big issue for you then you can fix it with a settimer in the Else If of MouseMove. Have the timer check mousegetpos, HWND every 500 ms or so and compare it against %a_loopField%LastbuttonData1. If they don't match then change back to the up state. I'm not gonna write the code because in my eyes its a non issue but its deffinately preventable.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: very nice
PostPosted: June 24th, 2010, 4:49 am 
very nice script, works really smoooth.

I would request a few more demo examples so the no0b like me can understand to use it better in their scripts.


Report this post
Top
  
Reply with quote  
 Post subject: Re: very nice
PostPosted: June 26th, 2010, 7:07 pm 
Offline

Joined: December 17th, 2008, 5:36 am
Posts: 80
Anonymous wrote:
very nice script, works really smoooth.

I would request a few more demo examples so the no0b like me can understand to use it better in their scripts.


Any other example I post will look exactly the same really, less buttons maybe? Not sure how else to show an example of it.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Thanks!
PostPosted: April 1st, 2011, 5:34 pm 
Offline

Joined: December 18th, 2009, 4:12 pm
Posts: 6
Location: USA
I know this is an old post, but thank you for posting it. I noticed that when loading new images to the picture objects, there can be a flicker as it is updated. I needed something very smooth, so I changed the behavior to first create all three button states on top of one another, hiding the Hover and Down images. Turned out to be a pretty thorough re-write, but I wanted to post it here because this was the inspiration, and my code may help someone else apply either method. The mouse hook function unhides/hides the buttons as necessary. This eliminates the flicker. Here's my code. You'll need to add an "Images" folder with 9 32x32px images to test. The images are labeled buttonx.png, buttonx-Hover.png, and buttonx-Down.png (where x = 1,2,3).

Code:
; SET INITIAL VARIABLES
ButtonCount = 3
MouseOnButton = 0
ButtonMouseOn =
MidClick = 0
GUIHeight = 166
GUIWidth = 158

; CREATE GUI INTERFACE
; ADD BUTTONS
AddGraphicBtn(1, "x12", "y12", "h32", "w32", "Btn1", a_ScriptDir . "\Images\button1.png", a_ScriptDir . "\Images\button1-Hover.png", a_ScriptDir . "\Images\button1-Down.png")
AddGraphicBtn(1, "x62", "y12", "h32", "w32", "Btn2", a_ScriptDir . "\Images\button2.png", a_ScriptDir . "\Images\button2-Hover.png", a_ScriptDir . "\Images\button2-Down.png")
AddGraphicBtn(1, "x112", "y12", "h32", "w32", "Btn3", a_ScriptDir . "\Images\button3.png", a_ScriptDir . "\Images\button3-Hover.png", a_ScriptDir . "\Images\button3-Down.png")
; REGISTER MOUSE HOOKS
OnMessage(0x200, "MouseMove")
OnMessage(0x201, "MouseLDown")
OnMessage(0x202, "MouseLUp")
; SHOW GUI
Gui, 1:Show, w%GUIWidth% h%GUIHeight%
; HIDE HOVER AND DOWN BUTTONS
GuiControl, 1:Hide, Btn1Hvr
GuiControl, 1:Hide, Btn2Hvr
GuiControl, 1:Hide, Btn3Hvr
GuiControl, 1:Hide, Btn1Dwn
GuiControl, 1:Hide, Btn2Dwn
GuiControl, 1:Hide, Btn3Dwn
; GET WINDOW ID
WinGet, id, ID, A
Return

; LABELS FOR THE MOUSE UP EVENT WHEN THE CURSOR IS OVER A BUTTON. THIS IS THE ACTUAL EVENT THAT PRESSING AND RELEASING A BUTTON WILL EXECUTE.
Btn1_up:
; Add event here
Tooltip, Button 1 was clicked.
Return

Btn2_up:
; Add event here
Tooltip, Button 2 was clicked.
Return

Btn3_up:
; Add event here
Tooltip, Button 3 was clicked.
Return

; DEFAULT G LABEL FOR PICTURE OBJECTS.
DoNothing:
Return

1GUIClose:
Gui, 1:Destroy
ExitApp

CheckMouse:
; IF YOU MOVE YOUR MOUSE REALLY FAST OFF THE GUI, IT WON'T CHECK THE MOUSEHOOK AGAIN AND CAN LEAVE A BUTTON IN THE HOVER STATE.
; THIS LABEL GETS CALLED 250 MS AFTER THE MOUSE MOVES ON THE GUI WINDOW. IF IT'S STILL ON THE GUI, GREAT, OTHERWISE, CALL THE MOUSEMOVE LABEL TO CLEAR THE HOVER STATE.
MouseGetPos,,,WinID, HWND
If WinID = %id%
   SetTimer, CheckMouse, OFF
Else
   MouseMove("", "", "", %HWND%)
Return

; ADD PICTURE BUTTONS TO GUI.
AddGraphicBtn(GUI_Number, Btn_X, Btn_Y, Btn_H, Btn_W, Btn_Identifier, Btn_Up, Btn_Hvr, Btn_Dwn)
{
   Global
   Gui, %GUI_Number%:Add, Picture, +altsubmit %Btn_X% %Btn_Y% %Btn_H% %Btn_W% gDoNothing v%Btn_Identifier%Hvr hwnd%Btn_Identifier%Hvr_hwnd, %Btn_Hvr%
   Gui, %GUI_Number%:Add, Picture, +altsubmit %Btn_X% %Btn_Y% %Btn_H% %Btn_W% gDoNothing v%Btn_Identifier%Dwn hwnd%Btn_Identifier%Dwn_hwnd, %Btn_Dwn%
   Gui, %GUI_Number%:Add, Picture, +altsubmit %Btn_X% %Btn_Y% %Btn_H% %Btn_W% gDoNothing v%Btn_Identifier% hwnd%Btn_Identifier%_hwnd, %Btn_Up%
}

; WHEN THE MOUSE MOVES ON THE GUI (I THINK IT POLLS EVERY 50 MS OR SO), THIS IS CALLED.
; IT CHECKS THE UNIQUE ID (HWND) OF THE ITEM CURRENTLY UNDER THE MOUSE, AGAINST THE STORED HWND'S OF EACH BUTTON.
; IF A MATCH IS FOUND, IT KNOWS IT'S OVER A BUTTON. IT STORES THE BUTTON IT'S OVER, AND SETS A FLAG SO IT DOESN'T KEEP RE-SHOWING THE HOVER IMAGE.
; IF THE MOUSE GOES FROM BEING OVER A BUTTON (MOUSEONBUTTON = 1) TO BEING OVER THE GUI WINDOW (HWND = ID), IT REMOVES THE HOVER EFFECT.
MouseMove(wParam, lParam, msg, hwnd)
{
   Global
   Loop, %ButtonCount%
   {
      If MouseOnButton = 0
      {
         If (hwnd = Btn%A_Index%_HWND)  ; MOUSE IS OVER BUTTON [A_INDEX]. SHOW HOVER IMAGE.
         {
            GuiControl, Hide, Btn%A_Index%
            GuiControl, Show, Btn%A_Index%Hvr
            GuiControl, Hide, Btn%A_Index%Dwn
            MouseOnButton = 1
            ButtonMouseOn = Btn%A_Index%
            Break
         }
      }
   }
   If (MouseOnButton = 1) and (hwnd = id) and (MidClick = 0)  ; MOUSE MOVED FROM A BUTTON TO THE GUI.
   {
      GuiControl, Hide, %ButtonMouseOn%Dwn
      GuiControl, Hide, %ButtonMouseOn%Hvr
      GuiControl, Show, %ButtonMouseOn%
      MouseOnButton = 0
      ButtonMouseOn =
      tooltip,
   }
   If (hwnd != %ButtonMouseOn%_HWND) and (hwnd != %ButtonMouseOn%Hvr_HWND) and (hwnd != %ButtonMouseOn%Dwn_HWND) and (hwnd != id)  ; THE MOUSE IS NOT ON THE GUI OR A BUTTON. THIS CONDITION WILL BE SATISFIED WHEN THE CHECKMOUSE TIMER CALLS MOUSEMOVE
   {
      GuiControl, Hide, %ButtonMouseOn%Dwn
      GuiControl, Hide, %ButtonMouseOn%Hvr
      GuiControl, Show, %ButtonMouseOn%
      MouseOnButton = 0
      ButtonMouseOn =
      tooltip,
   }
   ; HERE'S THAT TIMER CALL TO SEE IF THE MOUSE FLEW OFF THE GUI...
   SetTimer, CheckMouse, 250
   Return
}

; WHEN THE MOUSE BUTTON IS PRESSED, CHANGE THE BUTTON IMAGE AGAIN.
MouseLDown(wParam, lParam, msg, hwnd)
{
   Global
   ; SET "MIDCLICK" FLAG TO AVOID THE MOUSEMOVE LABEL FROM THINKING THE MOUSE LEFT THE BUTTON BETWEEN THE HIDE AND SHOW OF THE BUTTONS.
   MidClick = 1
   If (hwnd = %ButtonMouseOn%Hvr_HWND)
   {
      GuiControl, Hide, %ButtonMouseOn%Hvr
      GuiControl, Show, %ButtonMouseOn%Dwn
      GuiControl, Hide, %ButtonMouseOn%
   }
   MidClick = 0
   GetKeyState, MouseLBtnState, LButton, P
   If MouseLBtnState = U
      MouseLUp("", "", "", %hwnd%)
   Return
}

; WHEN THE MOUSE IS RELEASED, IF IT'S STILL OVER A BUTTON, CHANGE THE IMAGE AGAIN AND CALL THE "[BTNNAME]_UP" LABEL.
MouseLUp(wParam, lParam, msg, hwnd)
{
   Global
   MidClick = 1
   If (hwnd = %ButtonMouseOn%Dwn_HWND)
   {
      GuiControl, Hide, %ButtonMouseOn%Dwn
      GuiControl, Show, %ButtonMouseOn%Hvr
      GuiControl, Hide, %ButtonMouseOn%
      GoSub % ButtonMouseOn . "_Up"
   }
   MidClick = 0
   Return
}


There ya go. Two ways to skin a cat.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: rrhuffy and 54 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group