Jump to content

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

[Function] 3 State Graphic Button (Up, Down, Hover)


  • Please log in to reply
9 replies to this topic
kdoske
  • Members
  • 138 posts
  • Last active: Nov 06 2012 01:58 AM
  • Joined: 17 Dec 2008
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
Posted ImagePosted Image
Example 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, https://ahknet.autohotkey.com/~kdoske/3State_Button_Function/Down.png, Down.png
   URLDownloadToFile, https://ahknet.autohotkey.com/~kdoske/3State_Button_Function/Hover.png, Hover.png
   URLDownloadToFile, https://ahknet.autohotkey.com/~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:
; *******************************************************************
; 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
}


TomXIII
  • Members
  • 182 posts
  • Last active: Feb 28 2011 11:42 PM
  • Joined: 14 Apr 2009
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.

kdoske
  • Members
  • 138 posts
  • Last active: Nov 06 2012 01:58 AM
  • Joined: 17 Dec 2008

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.

  • Guests
  • Last active:
  • Joined: --
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.

kdoske
  • Members
  • 138 posts
  • Last active: Nov 06 2012 01:58 AM
  • Joined: 17 Dec 2008

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.

itjustwerks
  • Members
  • 7 posts
  • Last active: Nov 13 2015 05:19 AM
  • Joined: 18 Dec 2009
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).

; 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.

User1554
  • Guests
  • Last active:
  • Joined: --
Hi everyone,

I'm having issues with adding an additional GUI to my script. Can anyone show me what I did wrong?

I have a main file that looks like this:

#Include loader\buttonfunct.ahk

#Include ConnectSettings.ahk

; SET INITIAL VARIABLES
ButtonCount = 2
MouseOnButton = 0
ButtonMouseOn =
MidClick = 0
ServerLocation =
CurrentVersion =
LatestVersion =


Gui, 1:Add, Picture, x0 y0 w950 h500 hwndhPic, loader\Images\loading1.png
Gui, 1:Font, s18, Segoe UI
Gui, 1:Add, Text, vInfoMessage x0 y200 w950 h80 cFFFFFF BackgroundTrans +Center, Please Wait

; ADD BUTTONS
AddGraphicBtn(1, "x871", "y387", "h27", "w27", "Btn1", a_ScriptDir . "\loader\Images\settings-D.png", a_ScriptDir . "\loader\Images\settings-H.png", a_ScriptDir . "\loader\Images\settings-D.png")
AddGraphicBtn(1, "x908", "y387", "h27", "w27", "Btn2", a_ScriptDir . "\loader\Images\help-D.png", a_ScriptDir . "\loader\Images\help-H.png", a_ScriptDir . "\loader\Images\help-D.png")

; REGISTER MOUSE HOOKS
OnMessage(0x200, "MouseMove")
OnMessage(0x201, "MouseLDown")
OnMessage(0x202, "MouseLUp")
; SHOW GUI
Gui, 1:Show, w950 h500
; HIDE HOVER AND DOWN BUTTONS
GuiControl, 1:Hide, Btn1Hvr
GuiControl, 1:Hide, Btn1Dwn
GuiControl, 1:Hide, Btn2Hvr
GuiControl, 1:Hide, Btn2Dwn
; GET WINDOW ID
WinGet, id, ID, A

;Some macro code is here ( I removed it to make it more legible)
;---------------------------------
;---------------------------------
;---------------------------------
;---------------------------------


Return



Btn1_up:
DrawSettingsGui()
Return

Btn2_up:
Tooltip, Button 2 was clicked.
Return

DoNothing:
Return

1GUIClose:
Gui, 1:Destroy
ExitApp




CheckMouse:
MouseGetPos,,,WinID, HWND
If WinID = %id%
   SetTimer, CheckMouse, OFF
Else
   MouseMove("", "", "", %HWND%)
Return

I decided to put my buttons file in a separate file (I didn't change anything here)
buttonfunct.ahk
; 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
}

And here's my second GUI
ConnectSettings.ahk
DrawSettingsGui()

{


; SET INITIAL VARIABLES
ButtonCount = ButtonCount + 1

; CREATE GUI INTERFACE
;Add Stuff
Gui, 2:Font, cb0b0b0 s10, MS Shell Dlg
Gui, 2:Add, Picture, x0 y0 w360 h316, Images\SettingsBackground.png
Gui, 2:Color, 313131, 354657
Gui, 2:Add, Edit, x78 y213 w200 h24,
; ADD BUTTONS
AddGraphicBtn(2, "x80", "y260", "h20", "w89", "Btn10", a_ScriptDir . "\Images\OkButton.png", a_ScriptDir . "\Images\OkButton.png", a_ScriptDir . "\Images\OkButtonDown.png")

; SHOW GUI
Gui, 2: Color, 313131
Gui, 2:Show, w360 h316
; HIDE HOVER AND DOWN BUTTONS
GuiControl, 2:Hide, Btn10Hvr
GuiControl, 2:Hide, Btn10Dwn
; GET WINDOW ID
WinGet, id, ID, A
Return


Btn10_up:
; Add event here
Tooltip, Button 1 was clicked.
Return


}

Now, what happens is that when I click the button (in the first GUI), my new GUi opens. Everything is okay.
The hover functions work, however any button I click after I open the GUI, I get this error instead of the button working:

Posted Image

woonsik
  • Members
  • 15 posts
  • Last active: Apr 12 2013 08:32 AM
  • Joined: 15 Jan 2013
#Singleinstance Force
 
 
 
Gui,Color, ffffff
;//image Button
Gui_Picture("50,60", "var" , "_Label", A_Workingdir . "\hover.png")
gui,show, w500 h400 , test
return
 
guiescape:
guiclose:
exitApp
 
 
_Label:
_IconBtn(A_guicontrol)
sleep, 100
return
 
 
 
 
Gui_Picture(xy,v,g,ico){
global
stringsplit, p_xy , xy , `, 
gui,add,picture,% "x" . (p_xy1) . " y" . (p_xy2) . " w122 h42 e0x1   v_Gui" . v . " g" . g,   %ICO%
gui,add,picture,% "x" . (p_xy1) .     " y" . (p_xy2) . " w122 h42 e0x200 v_Gui" . v . "B +hidden",%ICO%
}
 
_IconBtn(_GuiVar){
  guicontrol,hide,%_GuiVar%
  guicontrol,show,% _GuiVar . "B"
  loop {
   if ! getkeystate("lbutton","p")
    break
   sleep 30
  }
  guicontrol,hide,% _GuiVar . "B"
  guicontrol,show,%_GuiVar%  
 }


woonsik
  • Members
  • 15 posts
  • Last active: Apr 12 2013 08:32 AM
  • Joined: 15 Jan 2013

#Singleinstance Force
 
 
 
Gui,Color, ffffff
;//image Button
Gui_Picture("50,60,142,42", "var" , "_Label", A_Workingdir . "\hover.png")
gui,show, w500 h400 , test
return
 
guiescape:
guiclose:
exitApp
 
 
_Label:
_IconBtn(A_guicontrol)
sleep, 100
return
 
 
 
 
Gui_Picture(xywh,v,g,ico){
global
stringsplit, p_xywh , xywh , `, 
gui,add,picture,% "x" . (p_xywh1) . " y" . (p_xywh2) . "w" . (p_xywh3) . "h" . (p_xywh4) . " e0x1   v_Gui" . v . " g" . g,   %ICO%
gui,add,picture,% "x" . (p_xywh1) . " y" . (p_xywh2) . "w" . (p_xywh3) . "h" . (p_xywh4) . " e0x200 v_Gui" . v . "B +hidden",%ICO%
}
 
_IconBtn(_GuiVar){
  guicontrol,hide,%_GuiVar%
  guicontrol,show,% _GuiVar . "B"
  loop {
   if ! getkeystate("lbutton","p")
    break
   sleep 30
  }
  guicontrol,hide,% _GuiVar . "B"
  guicontrol,show,%_GuiVar%  
 }


nemezisx
  • Members
  • 6 posts
  • Last active: Apr 07 2017 12:18 PM
  • Joined: 28 May 2011

for : 

User1554

 

 

Hi everyone,

I'm having issues with adding an additional GUI to my script. Can anyone show me what I did wrong?

I have a main file that looks like this:
 

#Include loader\buttonfunct.ahk

#Include ConnectSettings.ahk

; SET INITIAL VARIABLES
ButtonCount = 2
MouseOnButton = 0
ButtonMouseOn =
MidClick = 0
ServerLocation =
CurrentVersion =
LatestVersion =


Gui, 1:Add, Picture, x0 y0 w950 h500 hwndhPic, loader\Images\loading1.png
Gui, 1:Font, s18, Segoe UI
Gui, 1:Add, Text, vInfoMessage x0 y200 w950 h80 cFFFFFF BackgroundTrans +Center, Please Wait

; ADD BUTTONS
AddGraphicBtn(1, "x871", "y387", "h27", "w27", "Btn1", a_ScriptDir . "\loader\Images\settings-D.png", a_ScriptDir . "\loader\Images\settings-H.png", a_ScriptDir . "\loader\Images\settings-D.png")
AddGraphicBtn(1, "x908", "y387", "h27", "w27", "Btn2", a_ScriptDir . "\loader\Images\help-D.png", a_ScriptDir . "\loader\Images\help-H.png", a_ScriptDir . "\loader\Images\help-D.png")

; REGISTER MOUSE HOOKS
OnMessage(0x200, "MouseMove")
OnMessage(0x201, "MouseLDown")
OnMessage(0x202, "MouseLUp")
; SHOW GUI
Gui, 1:Show, w950 h500
; HIDE HOVER AND DOWN BUTTONS
GuiControl, 1:Hide, Btn1Hvr
GuiControl, 1:Hide, Btn1Dwn
GuiControl, 1:Hide, Btn2Hvr
GuiControl, 1:Hide, Btn2Dwn
; GET WINDOW ID
WinGet, id, ID, A

;Some macro code is here ( I removed it to make it more legible)
;---------------------------------
;---------------------------------
;---------------------------------
;---------------------------------


Return



Btn1_up:
DrawSettingsGui()
Return

Btn2_up:
Tooltip, Button 2 was clicked.
Return

DoNothing:
Return

1GUIClose:
Gui, 1:Destroy
ExitApp




CheckMouse:
MouseGetPos,,,WinID, HWND
If WinID = %id%
   SetTimer, CheckMouse, OFF
Else
   MouseMove("", "", "", %HWND%)
Return
I decided to put my buttons file in a separate file (I didn't change anything here)
buttonfunct.ahk
; 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
}
And here's my second GUI
ConnectSettings.ahk
DrawSettingsGui()

{


; SET INITIAL VARIABLES
ButtonCount = ButtonCount + 1

; CREATE GUI INTERFACE
;Add Stuff
Gui, 2:Font, cb0b0b0 s10, MS Shell Dlg
Gui, 2:Add, Picture, x0 y0 w360 h316, Images\SettingsBackground.png
Gui, 2:Color, 313131, 354657
Gui, 2:Add, Edit, x78 y213 w200 h24,
; ADD BUTTONS
AddGraphicBtn(2, "x80", "y260", "h20", "w89", "Btn10", a_ScriptDir . "\Images\OkButton.png", a_ScriptDir . "\Images\OkButton.png", a_ScriptDir . "\Images\OkButtonDown.png")

; SHOW GUI
Gui, 2: Color, 313131
Gui, 2:Show, w360 h316
; HIDE HOVER AND DOWN BUTTONS
GuiControl, 2:Hide, Btn10Hvr
GuiControl, 2:Hide, Btn10Dwn
; GET WINDOW ID
WinGet, id, ID, A
Return


Btn10_up:
; Add event here
Tooltip, Button 1 was clicked.
Return


}
Now, what happens is that when I click the button (in the first GUI), my new GUi opens. Everything is okay.
The hover functions work, however any button I click after I open the GUI, I get this error instead of the button working:

fQd0j.png

 

 

try changing the value of the ButtonCount = 2 with ButtonCount = 10 in the SET INITIAL VARIABLES.

I use about 29 Buttons on my Gui, then I replace ButtonCount with 29. and it worked.