GUI it works, but I don't know why Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

GUI it works, but I don't know why

14 Sep 2021, 18:44

Hi.

I'm trying to simulate a lighting effect when the mouse goes over a button. I'm open to all possibilities. I've looked a lot, but I haven't found a way to do it.

The following script is a hybrid of two scripts and one feature. The main simulates a state change when passing through a control or exiting: (viewtopic.php?t=30932). The second is from "teadrinker" and is found in this post (viewtopic.php?f=76&t=69559&p=420401#p420401) and the function is to be able to add icons or images found here (viewtopic.php?t=1985)

I have separated the three controls of interest, on lines 16 to 18.

The problem is that it was started with an empty space in the gui, by the control of line 17, which is created by means of a "SendMessage," but which is nested within the behaviors of "OnThe Leave" and "The Move."
I fixed it with a "patch," adding line 18 that makes an image equal to the control of line 16 (linked with the function of displaying a .png on a button).

Code: Select all

pngFile := "AbajoDchaF.png"
BS_ICON      := 0x40
BS_BITMAP    := 0x80
IMAGE_BITMAP := 0
IMAGE_ICON   := 1
BM_SETIMAGE  := 0xF7

^F4::
Gui, OptionMenu: new 
Gui, OptionMenu: -Caption +alwaysontop +ToolWindow
Gui, OptionMenu: Add, Picture, x0 y0 w600 h167, Clipboard Utility_p.png
Gui, OptionMenu: Add, Picture, x553 y0 w48 h35 gExitCopy, ArribaDcha.png
Gui, OptionMenu: Add, Text, x0 y0 w552 h35 GuiMove, `&& Drag
Gui, OptionMenu: Add, Picture, x0 y0 w552 h35, ArribaIzq.png

Gui, OptionMenu: Add, Button, x16 y36 w141 h115 hwndIcon10 gPrintPDF vPrint
Gui, OptionMenu: Add, Button,  x16 y36 w141 h115 %BS_BITMAP% hwndIcon11 hwndhImageButton ; Válido
Gui, OptionMenu: Add, Picture, x16 y36 w141 h115, AbajoDcha.png

;Gui, OptionMenu: Add, Picture, x158 y36 w141 h115, AbajoDcha2.png
;Gui, OptionMenu: Add, Picture, x444 y36 w141 h115, AbajoDcha4.png
GuiButtonIcon(Icon10, "AbajoDcha.png", 1, "w213 h174")
MouseGetPos, xpos2, ypos2
VarSetCapacity(tme,16,0)
NumPut(16,tme,0), NumPut(2,tme,4), NumPut(hwnd,tme,8)
OnMessage(0x2A3,"OnMouseLeave")
OnMessage(0x200,"OnMouseMove")
Gui, OptionMenu: Show, x%xpos2% y%ypos2% W600 h167
Return

OnMouseMove( wParam, lParam, Msg,hwnd ) {
global status_Print ,tme
DllCall( "TrackMouseEvent","uint",&tme )
Status_Print:=""
if instr(a_guicontrol,"Print")
      {
      status_Print:=a_guicontrol
      SetTimer, Printalert, -20
      }
}

OnMouseLeave(){
global
status_print_old:=""
}

Printalert:
if (status_Print=status_print_old)
return
/*
if !hBitmap := LoadPicture(pngFile, "w213 h174", IMAGE_BITMAP)
   MsgBox, Can't load image
if !(hBitmap)
   ExitApp
*/
hBitmap := LoadPicture(pngFile, "w213 h174", IMAGE_BITMAP)
SendMessage, BM_SETIMAGE, IMAGE_BITMAP, hBitmap,, ahk_id %hImageButton%
;status_print_old:=status_print
Return

RefreshGUI:
WinSet Redraw
Gui, show
Return

ExitCopy:
Gui, OptionMenu:Hide
Return

uiMove:
PostMessage, 0xA1, 2,,, A
Return

PrintPDF:
Return

GuiButtonIcon(Handle, File, Index := 1, Options := "")
{
	RegExMatch(Options, "i)w\K\d+", W), (W="") ? W := 16 :
	RegExMatch(Options, "i)h\K\d+", H), (H="") ? H := 16 :
	RegExMatch(Options, "i)s\K\d+", S), S ? W := H := S :
	RegExMatch(Options, "i)l\K\d+", L), (L="") ? L := 0 :
	RegExMatch(Options, "i)t\K\d+", T), (T="") ? T := 0 :
	RegExMatch(Options, "i)r\K\d+", R), (R="") ? R := 0 :
	RegExMatch(Options, "i)b\K\d+", B), (B="") ? B := 0 :
	RegExMatch(Options, "i)a\K\d+", A), (A="") ? A := 4 :
	Psz := A_PtrSize = "" ? 4 : A_PtrSize, DW := "UInt", Ptr := A_PtrSize = "" ? DW : "Ptr"
	VarSetCapacity( button_il, 20 + Psz, 0 )
	NumPut( normal_il := DllCall( "ImageList_Create", DW, W, DW, H, DW, 0x21, DW, 1, DW, 1 ), button_il, 0, Ptr )	; Width & Height
	NumPut( L, button_il, 0 + Psz, DW )		; Left Margin
	NumPut( T, button_il, 4 + Psz, DW )		; Top Margin
	NumPut( R, button_il, 8 + Psz, DW )		; Right Margin
	NumPut( B, button_il, 12 + Psz, DW )	; Bottom Margin	
	NumPut( A, button_il, 16 + Psz, DW )	; Alignment
	SendMessage, BCM_SETIMAGELIST := 5634, 0, &button_il,, AHK_ID %Handle%
	return IL_Add( normal_il, File, Index )
}
Although I have achieved what I wanted, I know that there are more things I ignore than I know. That's why I want to know if you can do better, or know what you fail in or how you can do better.

It also has a small failure: sometimes it does not do well the change of control or takes time to make the effect (it switches between the two controls of line 16 and 17 (control 18 only emerges at the beginning, being an image stops operating).

I would have preferred to do the same with images and not with buttons, because the button leaves a halo that shaves the interface. Is there the possibility of doing the same with images and not with buttons. If you don't. How to fix this little "Frankenstein." check if a file exists

If the position of the three controls is changed, it does not work ... crazy! :crazy:

Images needed: https://mega.nz/file/xdAE3ZoC#vnwrJkXjI2CF9GUP528c7L3KzQ6HrPIBWX_w6V_P9YI

Explanatory video and how "works" and the script fails:

User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: GUI it works, but I don't know why

14 Sep 2021, 21:19

This is the basics

Code: Select all

#SingleInstance, Force

global Active := "" , Pressed := ""
, Handles := [] , Controls := {}

Gui, +AlwaysOnTop

;************************************************************************************************
;Replace the progress bar with a picture control.
Gui, Add, Progress, xm ym w200 h30 BackgroundBlack c336699 hwndhwnd, 100
Handles[ hwnd ] := "Button1"
Controls.Button1 := { Label: "DemoLabel"}
;************************************************************************************************

Gui, Show,,demo

OnMessage( 0x200, "Hover" ) ;If you move your cursor over the GUI
OnMessage( 0x201, "Press" ) ;If you click on the GUI

return
GuiClose:
*ESC::ExitApp

DemoLabel:
	SoundBeep, 900
	MsgBox, 262144, , You Pressed The Button
	return

Hover(  w , l , msg , hwnd ){
	if( !Active && Handles[ hwnd ] && !Pressed ){
		Active := hwnd
		SetTimer, WatchLeave, 30
		GuiControl, +c3399FF, % hwnd ;Change the picture, not the progress bar
	}
}

WatchLeave:
	if(Pressed)
		return
	MouseGetPos,,,, ctrl, 2
	if( Active != ctrl ){
		GuiControl, +c336699, % Active ;Change the picture, not the progress bar
		SetTimer, WatchLeave, Off
		Active := ""
	}
	return

Press( w , l , msg , hwnd ){
	if( !Pressed && Handles[ hwnd ] ){
		Pressed := hwnd
		GuiControl, +cLime , % Pressed ;Change the picture, not the progress bar
		While( GetKeyState( "LButton" ) )
			sleep, 30
		MouseGetPos,,,, ctrl, 2
		if( ctrl = Pressed ){
			GuiControl, +c3399FF , % Pressed ;Change the picture, not the progress bar
			try
				gosub, % Controls[ Handles[ Pressed ] ].Label
			Pressed := ""
		}else{
			GuiControl, +c336699 , % Pressed ;Change the picture, not the progress bar
			Pressed := ""
		}
	}
}
Temp (1).gif
Temp (1).gif (142.43 KiB) Viewed 1174 times
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: GUI it works, but I don't know why

15 Sep 2021, 05:53

Thank you Hellbent...

But it does not happen absolutely nothing.

Code: Select all

#SingleInstance, Force

global Active := "" , Pressed := ""
, Handles := [] , Controls := {}

Gui, +AlwaysOnTop

;************************************************************************************************
;Replace the progress bar with a picture control.
;Gui, Add, Progress, xm ym w200 h30 BackgroundBlack c336699 hwndhwnd, 100
Gui, Add, Picture, x16 y36 w141 h115 hwndhwnd, AbajoDcha.png
Handles[ hwnd ] := "Button1"
Controls.Button1 := { Label: "DemoLabel"}
;************************************************************************************************

Gui, Show,,demo

OnMessage( 0x200, "Hover" ) ;If you move your cursor over the GUI
OnMessage( 0x201, "Press" ) ;If you click on the GUI

return
GuiClose:
*ESC::ExitApp


DemoLabel:
	SoundBeep, 900
	MsgBox, 262144, , You Pressed The Button
	return

Hover(  w , l , msg , hwnd ){
	if( !Active && Handles[ hwnd ] && !Pressed ){
		Active := hwnd
		SetTimer, WatchLeave, 30
		GuiControl,, AbajoDchaF.png, % hwnd
		;GuiControl, +c3399FF, % hwnd ;Change the picture, not the progress bar
	}
}

WatchLeave:
	if(Pressed)
		return
	MouseGetPos,,,, ctrl, 2
	if( Active != ctrl ){
		;GuiControl, +c336699, % Active ;Change the picture, not the progress bar
		GuiControl,, AbajoDcha.png, % Active
		SetTimer, WatchLeave, Off
		Active := ""
	}
	return

Press( w , l , msg , hwnd ){
	if( !Pressed && Handles[ hwnd ] ){
		Pressed := hwnd
		GuiControl,, AbajoDchaF.png, % Pressed
		;GuiControl, +cLime , % Pressed ;Change the picture, not the progress bar
		While( GetKeyState( "LButton" ) )
			sleep, 30
		MouseGetPos,,,, ctrl, 2
		if( ctrl = Pressed ){
			GuiControl,, AbajoDchaF.png , % Pressed 
			;GuiControl, +c3399FF , % Pressed ;Change the picture, not the progress bar
			try
				gosub, % Controls[ Handles[ Pressed ] ].Label
			Pressed := ""
		}else{
			GuiControl,, AbajoDcha.png , % Pressed
			;GuiControl, +c336699 , % Pressed ;Change the picture, not the progress bar
			Pressed := ""
		}
	}
}
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: GUI it works, but I don't know why

15 Sep 2021, 07:07

I added a button to see what really happened and it worked if it added a variable to the control. The% HWND parameter, which must be a variable, does not operate. You have to take it off.

Strangely does not work if you do not put a subroutine, although there is nothing (line 28). :crazy:

Code: Select all

#SingleInstance, Force

global Active := "" , Pressed := ""
, Handles := [] , Controls := {}

Gui, +AlwaysOnTop

;************************************************************************************************
;Replace the progress bar with a picture control.
;Gui, Add, Progress, xm ym w200 h30 BackgroundBlack c336699 hwndhwnd, 100
Gui, Add, Picture, x16 y36 w141 h115 gChange vChance hwndhwnd, AbajoDcha.png ; Added variable and subroutine
;Gui, add, button, x16 y160 w130 h20 gChange, Change Image ;Added to test
Handles[ hwnd ] := "Button1"
Controls.Button1 := { Label: "DemoLabel"}
;************************************************************************************************

Gui, Show,,demo

OnMessage( 0x200, "Hover" ) ;If you move your cursor over the GUI
OnMessage( 0x201, "Press" ) ;If you click on the GUI

return

GuiClose:
*ESC::ExitApp

Change:
;GuiControl,, Chance, AbajoDchaF.png
Return

DemoLabel:
	SoundBeep, 900
	MsgBox, 262144, , You Pressed The Button
	return

Hover(  w , l , msg , hwnd ){
	if( !Active && Handles[ hwnd ] && !Pressed ){
		Active := hwnd
		SetTimer, WatchLeave, 30
		GuiControl,,Chance, AbajoDchaF.png ; , % hwnd (<- Remove this parameter)
		;GuiControl, +c3399FF, % hwnd ;Change the picture, not the progress bar
	}
}

WatchLeave:
	if(Pressed)
		return
	MouseGetPos,,,, ctrl, 2
	if( Active != ctrl ){
		;GuiControl, +c336699, % Active ;Change the picture, not the progress bar
		GuiControl,, Chance, AbajoDcha.png ;, % Active (<- Remove this parameter)
		SetTimer, WatchLeave, Off
		Active := ""
	}
	return

Press( w , l , msg , hwnd ){
	if( !Pressed && Handles[ hwnd ] ){
		Pressed := hwnd
		GuiControl,, Chance, AbajoDchaF.png ;, % Pressed (<- Remove this parameter)
		;GuiControl, +cLime , % Pressed ;Change the picture, not the progress bar
		While( GetKeyState( "LButton" ) )
			sleep, 30
		MouseGetPos,,,, ctrl, 2
		if( ctrl = Pressed ){
			GuiControl,, Chance, AbajoDchaF.png ;, % Pressed (<- Remove this parameter)
			;GuiControl, +c3399FF , % Pressed ;Change the picture, not the progress bar
			try
				gosub, % Controls[ Handles[ Pressed ] ].Label
			Pressed := ""
		}else{
			GuiControl,, Chance, AbajoDcha.png ;, % Pressed (<- Remove this parameter)
			;GuiControl, +c336699 , % Pressed ;Change the picture, not the progress bar
			Pressed := ""
		}
	}
}
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: GUI it works, but I don't know why

15 Sep 2021, 09:25

Hi.

The script works the first time, but if you are assigned a keyboard shortcut it does not work when you are called a second time. He says that "Error: The same variable cannot be used for more than one control," since I imagine that "vChange" and "hwndhwnd" are two variables and a control must only have one.

Why does it work the first time and the second time not, if there is the same problem? Do you have to zero any more variables? Any suggestions?

Summary:
  • The script doesn't work for images, as it was.
  • By mistakenly adding a variable to it works (where the function goes well), it only works the first time.
I don't know how to alter the function, I still don't get to that level of knowledge.

Code: Select all

#SingleInstance, Force

!F4:: ; Alt+F4
global Active := "" , Pressed := ""
, Handles := [] , Controls := {}

;Gui, +AlwaysOnTop
Gui, -Caption +alwaysontop
;************************************************************************************************
;Replace the progress bar with a picture control.
;Gui, Add, Progress, xm ym w200 h30 BackgroundBlack c336699 hwndhwnd, 100
Gui, Add, Picture, x16 y36 w141 h115 gChange vChance hwndhwnd, AbajoDcha.png ; Added variable and subroutine
Gui, add, button, x16 y160 w130 h20 gChange, Exit ;Added to test
Handles[ hwnd ] := "Button1"
Controls.Button1 := { Label: "DemoLabel"}
;************************************************************************************************

Gui, Show,,demo

OnMessage( 0x200, "Hover" ) ;If you move your cursor over the GUI
OnMessage( 0x201, "Press" ) ;If you click on the GUI

return

GuiClose:
*ESC::ExitApp

Change:
Gui, hide 
;GuiControl,, Chance, AbajoDchaF.png
Return

DemoLabel:
	SoundBeep, 900
	MsgBox, 262144, , You Pressed The Button
	return

Hover(  w , l , msg , hwnd ){
	if( !Active && Handles[ hwnd ] && !Pressed ){
		Active := hwnd
		SetTimer, WatchLeave, 30
		GuiControl,,Chance, AbajoDchaF.png ; , % hwnd (<- Remove this parameter)
		;GuiControl, +c3399FF, % hwnd ;Change the picture, not the progress bar
	}
}

WatchLeave:
	if(Pressed)
		return
	MouseGetPos,,,, ctrl, 2
	if( Active != ctrl ){
		;GuiControl, +c336699, % Active ;Change the picture, not the progress bar
		GuiControl,, Chance, AbajoDcha.png ;, % Active (<- Remove this parameter)
		SetTimer, WatchLeave, Off
		Active := ""
	}
	return

Press( w , l , msg , hwnd ){
	if( !Pressed && Handles[ hwnd ] ){
		Pressed := hwnd
		GuiControl,, Chance, AbajoDchaF.png ;, % Pressed (<- Remove this parameter)
		;GuiControl, +cLime , % Pressed ;Change the picture, not the progress bar
		While( GetKeyState( "LButton" ) )
			sleep, 30
		MouseGetPos,,,, ctrl, 2
		if( ctrl = Pressed ){
			GuiControl,, Chance, AbajoDchaF.png ;, % Pressed (<- Remove this parameter)
			;GuiControl, +c3399FF , % Pressed ;Change the picture, not the progress bar
			try
				gosub, % Controls[ Handles[ Pressed ] ].Label
			Pressed := ""
		}else{
			GuiControl,, Chance, AbajoDcha.png ;, % Pressed (<- Remove this parameter)
			;GuiControl, +c336699 , % Pressed ;Change the picture, not the progress bar
			Pressed := ""
		}
	}
}
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: GUI it works, but I don't know why  Topic is solved

15 Sep 2021, 11:52

@wetware05

See if this makes it a bit easier for you to follow.

Code: Select all


#SingleInstance, Force


global Gui1 := {} ;Main object for everything to do with this gui.
Gui1.Handles := [] ;an array that uses the hwnd of a control as its index position. (this has the name of the controls main object as its values)
Gui1.Active := "" ;The hwnd of the currently hovered button
Gui1.Pressed := "" ;The hwnd of the currently pressed button


Gui, +AlwaysOnTop HwndHwnd
Gui1.Hwnd := hwnd

;BUTTON 1
;************************************************************************************************************************
Gui1.Button1 := {} ;Button 1 
Gui1.Button1.DefaultImage := "C:\Users\Hellbent\Desktop\AHK Tools\Color Picker Mini\Screen Shots\20210915124512.png"
Gui1.Button1.HoverImage := "C:\Users\Hellbent\Desktop\AHK Tools\Color Picker Mini\Screen Shots\20210915124519.png"
Gui1.Button1.PressedImage := "C:\Users\Hellbent\Desktop\AHK Tools\Color Picker Mini\Screen Shots\20210915124525.png"
Gui1.Button1.Label := "SavePDF" 

Gui, Add, Picture, xm ym w150 h150 hwndhwnd, % Gui1.Button1.DefaultImage

Gui1.Handles[ hwnd ] := "Button1" ;The name of the control to point at.
Gui1.Button1.Hwnd := hwnd 
;************************************************************************************************************************


;BUTTON 2
;************************************************************************************************************************
Gui1.Button2 := {} ;Button 2 
Gui1.Button2.DefaultImage := GUI1.Button1.DefaultImage
Gui1.Button2.HoverImage := Gui1.Button1.HoverImage
Gui1.Button2.PressedImage := Gui1.Button1.PressedImage
Gui1.Button2.Label := "SavePDF" 

Gui, Add, Picture, x+10 w150 h150 hwndhwnd, % Gui1.Button2.DefaultImage

Gui1.Handles[ hwnd ] := "Button2" ;The name of the control to point at.
Gui1.Button2.Hwnd := hwnd 
;************************************************************************************************************************

Gui, Show,, Hover Demo

OnMessage( 0x200 , "ButtonHover" )
OnMessage( 0x201 , "ButtonPress")

return
GuiClose:
GuiContextMenu:
*ESC::ExitApp


;*******************************************************************************************
SavePDF: ;The label for a control
	SoundBeep, 500
	return
;********************************************************************************************************************	

;********************************************************************************************************************	
ButtonHover(){
	
	MouseGetPos,,,, ctrl, 2 ;Get the hwnd of the control under the cursor
	
	if( !Gui1.Active && !Gui1.Pressed && Gui1.Handles[ ctrl ] ){ ;If a button isn't already active (Hover) and if no buttons are being pressed, and there is a match for the control in the Handles array.
		
		Gui1.Active := ctrl ;Assign this control as being the active control
		
		GuiControl,, % Gui1.Active , % Gui1[ Gui1.Handles[ Gui1.Active ] ].HoverImage ;Set the picture to the hover image
		
		SetTimer, WatchLeaveHover, 30 ;Set a timer to watch for the cursor leaving this control (turn off hover)
		
	}
}

WatchLeaveHover: ;checks to see if the control that was being hovered is still being hovered.

	MouseGetPos,,,, ctrl, 2 ;Get the hwnd of the control under the cursor
	
	if( Gui1.Pressed ){ ;If a button is being pressed, skip for another 30ms
		return
	}else if( ctrl != Gui1.Active ){ ;if the control under the cursor isn't the same as the control that is being hovered. (if you move your cursor away from the control)
		GuiControl,, % Gui1.Active , % Gui1[ Gui1.Handles[ Gui1.Active ] ].DefaultImage ;Set the picture back to the default image
		SetTimer, WatchLeaveHover, Off ;Turn off the timer
		Gui1.Active := "" ;empty this so that a new control can be hovered.
	}
	return
;********************************************************************************************************************	


ButtonPress(){
	MouseGetPos,,,, ctrl, 2 ;Get the hwnd of the control under the cursor
	
	if( Gui1.Handles[ctrl] ){ ; if the control under the cursor is a valid control.
		Gui1.Pressed := ctrl ;Assign this control as being the pressed control
		GuiControl,, % Gui1.Pressed , % Gui1[ Gui1.Handles[ Gui1.Pressed ] ].PressedImage ;Set the picture to the pressed image
		
		While( GetKeyState( "LButton" ) ) ;While you continue to hold the mouse down, wait a few ms
			Sleep, 30
		
		MouseGetPos,,,, ctrl, 2 ;Get the hwnd of the control under the cursor again 
		
		if( ctrl = Gui1.Pressed ){ ;Test to see if it is still the same as the control you clicked on.
			GuiControl,, % Gui1.Pressed , % Gui1[ Gui1.Handles[ Gui1.Pressed ] ].HoverImage ;Set the picture to the Hover image
			Gui1.Pressed := "" ;empty
			try 
				gosub, % Gui1[ Gui1.Handles[ ctrl ] ].Label ;if the control has a label, run it.
			
		}else{ ;if the control is not the same. Cancel running that controls label and set the control back to its default image.
			GuiControl,, % Gui1.Pressed , % Gui1[ Gui1.Handles[ Gui1.Pressed ] ].DefaultImage ;Set the picture to the Default image
			Gui1.Pressed := "" ;empty
		}
		
	}
	
}
Temp (1).gif
Temp (1).gif (214.34 KiB) Viewed 1083 times
if you have any questions just ask.
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: GUI it works, but I don't know why

15 Sep 2021, 14:15

Thank you very much Hellbent.

I found this other development, through a function, but it changes both the way to work that I was missing too much: https://autohotkey.com/board/topic/55034-function-3-state-graphic-button-up-down-hover/

I'm going to see the script you've put posted. :thumbup:

I still do not understand why the variable is called "Hwndhwnd" (2 times HWND). Can you explain it to me?
User avatar
boiler
Posts: 16949
Joined: 21 Dec 2014, 02:44

Re: GUI it works, but I don't know why

15 Sep 2021, 14:39

wetware05 wrote:
I still do not understand why the variable is called "Hwndhwnd" (2 times HWND). Can you explain it to me?
The variable is not called Hwndhwnd. It’s called hwnd. You’ll see it’s referenced only as hwnd. The Hwndhwnd you see is the Gui option Hwnd, and whatever follows it is the variable that the GUI window’s HWND will be stored in. He could have made it HwndID, for another example, then the variable containing the HWND would be called ID.

See the Hwnd option in the documentation under the Options and styles for a [GUI] window.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: GUI it works, but I don't know why

15 Sep 2021, 15:01

To expand on what Boiler said.

When the script reads the line starting with the command "Gui" it would call up a function and pass it the values you used split at the commas.

The options part of the values you used is parsed (split by spaces) and it uses key words (and chars) to determine how to assign and use values/

For example if while parsing the options it see something that starts with the letter "x" it knows that anything that follows it should be used as the "X" position for the control.

Likewise with strings that start with these keywords. (and more [see boilers link])

Code: Select all

g = if a string starts with a "g" assign the rest of the string as the label for this control
v =  if a string starts with a "v" assign the rest of the string as the variable for this control
hwnd = if a string starts with a "hwnd" assign the rest of the string as the variable for this controls handle.
etc.


In the case of the example I posted I just use a generic name for the hwnd variable (hwndhwnd) because as soon as I create it I transfer the value to a more useful object and thus I don't need to keep the var "hwnd" around anymore and it is free to be used when the next control is added.

If I could assign the handle to my object directly, there would be no need to first create the temp "hwnd" var.
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: GUI it works, but I don't know why

15 Sep 2021, 16:01

Hi, Hellbent.

There still seems to be something wrong. I have added a second button and it does not work. Only the first works.

Hello Boiler. I understand now. I have already used a variable in other cases next to an ID. The variable is always the same and changes the id. The variable is like the first surname of a family, the ID is the name.

Code: Select all

#SingleInstance, Force


global Gui1 := {} ;Main object for everything to do with this gui.
Gui1.Handles := [] ;an array that uses the hwnd of a control as its index position. (this has the name of the controls main object as its values)
Gui1.Active := "" ;The hwnd of the currently hovered button
Gui1.Pressed := "" ;The hwnd of the currently pressed button

Gui, -Caption +AlwaysOnTop HwndHwnd
Gui1.Hwnd := hwnd

;Gui, -Caption +alwaysontop +ToolWindow
Gui, Add, Picture, x0 y0 w600 h167, Clipboard Utility_p.png
Gui, Add, Picture, x553 y0 w48 h35 gExitCopy, ArribaDcha.png
Gui, Add, Text, x0 y0 w552 h35 GuiMove, `&& Drag
Gui, Add, Picture, x0 y0 w552 h35, ArribaIzq.png

;BUTTON 1
;************************************************************************************************************************
Gui1.Button1 := {} ;Button 1 
Gui1.Button1.DefaultImage := "up.png"
Gui1.Button1.HoverImage := "hover.png"
Gui1.Button1.PressedImage := "DownPDF.png"
Gui1.Button1.Label := "SavePDF" 

Gui, Add, Picture, x16 y36 w141 h115 hwndhwnd, % Gui1.Button1.DefaultImage

Gui1.Handles[ hwnd ] := "Button1" ;The name of the control to point at.
Gui1.Button1.Hwnd := hwnd 
;************************************************************************************************************************


;BUTTON 2
;************************************************************************************************************************
Gui1.Button2 := {} ;Button 2 
Gui1.Button2.DefaultImage := "PasteUp.png"
Gui1.Button2.HoverImage := "PasteHover.png"
Gui1.Button2.PressedImage := "PasteDown.png"
Gui1.Button2.Label := "SavePDF" 

Gui, Add, Picture, x158 w36 w141 h115 hwndhwnd, % Gui1.Button2.DefaultImage

Gui1.Handles[ hwnd ] := "Button2" ;The name of the control to point at.
Gui1.Button2.Hwnd := hwnd 
;************************************************************************************************************************

MouseGetPos, xpos2, ypos2
Gui, Show, x%xpos2% y%ypos2% W600 h167
OnMessage( 0x200 , "ButtonHover" )
OnMessage( 0x201 , "ButtonPress" )
Return

ExitCopy:
Gui, Hide
Return

uiMove:
PostMessage, 0xA1, 2,,, A
Return

PrintPDF:
Return

GuiClose:
GuiContextMenu:
*ESC::ExitApp


;*******************************************************************************************
SavePDF: ;The label for a control
	SoundBeep, 500
	return
;********************************************************************************************************************	

;********************************************************************************************************************	
ButtonHover(){
	
	MouseGetPos,,,, ctrl, 2 ;Get the hwnd of the control under the cursor
	
	if( !Gui1.Active && !Gui1.Pressed && Gui1.Handles[ ctrl ] ){ ;If a button isn't already active (Hover) and if no buttons are being pressed, and there is a match for the control in the Handles array.
		
		Gui1.Active := ctrl ;Assign this control as being the active control
		
		GuiControl,, % Gui1.Active , % Gui1[ Gui1.Handles[ Gui1.Active ] ].HoverImage ;Set the picture to the hover image
		
		SetTimer, WatchLeaveHover, 30 ;Set a timer to watch for the cursor leaving this control (turn off hover)
		
	}
}

WatchLeaveHover: ;checks to see if the control that was being hovered is still being hovered.

	MouseGetPos,,,, ctrl, 2 ;Get the hwnd of the control under the cursor
	
	if( Gui1.Pressed ){ ;If a button is being pressed, skip for another 30ms
		return
	}else if( ctrl != Gui1.Active ){ ;if the control under the cursor isn't the same as the control that is being hovered. (if you move your cursor away from the control)
		GuiControl,, % Gui1.Active , % Gui1[ Gui1.Handles[ Gui1.Active ] ].DefaultImage ;Set the picture back to the default image
		SetTimer, WatchLeaveHover, Off ;Turn off the timer
		Gui1.Active := "" ;empty this so that a new control can be hovered.
	}
	return
;********************************************************************************************************************	


ButtonPress(){
	MouseGetPos,,,, ctrl, 2 ;Get the hwnd of the control under the cursor
	
	if( Gui1.Handles[ctrl] ){ ; if the control under the cursor is a valid control.
		Gui1.Pressed := ctrl ;Assign this control as being the pressed control
		GuiControl,, % Gui1.Pressed , % Gui1[ Gui1.Handles[ Gui1.Pressed ] ].PressedImage ;Set the picture to the pressed image
		
		While( GetKeyState( "LButton" ) ) ;While you continue to hold the mouse down, wait a few ms
			Sleep, 30
		
		MouseGetPos,,,, ctrl, 2 ;Get the hwnd of the control under the cursor again 
		
		if( ctrl = Gui1.Pressed ){ ;Test to see if it is still the same as the control you clicked on.
			GuiControl,, % Gui1.Pressed , % Gui1[ Gui1.Handles[ Gui1.Pressed ] ].HoverImage ;Set the picture to the Hover image
			Gui1.Pressed := "" ;empty
			try 
				gosub, % Gui1[ Gui1.Handles[ ctrl ] ].Label ;if the control has a label, run it.
			
		}else{ ;if the control is not the same. Cancel running that controls label and set the control back to its default image.
			GuiControl,, % Gui1.Pressed , % Gui1[ Gui1.Handles[ Gui1.Pressed ] ].DefaultImage ;Set the picture to the Default image
			Gui1.Pressed := "" ;empty
		}
		
	}
	
}
Sorry I was displaced almost outside the GUI. I'm tired. An error when putting the position "Y", typed "W"
Last edited by wetware05 on 15 Sep 2021, 16:25, edited 1 time in total.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: GUI it works, but I don't know why

15 Sep 2021, 16:22

Sorry I was displaced almost outside the GUI. I'm tired.
Was just about to post that.

You just need to add "yp" to the second buttons options so that it is at the same y position as the first button.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: GUI it works, but I don't know why

15 Sep 2021, 21:10

@wetware05 In regards to

viewtopic.php?f=76&t=92963&start=20#p420588
(the unraveling here: viewtopic.php?f=76&t=94703&p=420573. I just wanted two states, but there are three that cost me more to make the graphics.
You don't need to use different graphics for all the states, you can for example use two states by using the same image for two of the states.


It is also possible to use something like GDI+ to alter your images dynamically.
So for example you create a simple design, then use gdip to draw a border or add a semi-transparent layer on top or something else and then change the color of that border for each state.

All of your buttons are the same except for the little file image in the center. Gdip could easily have handled the rest of the design.

For simple visual reference.

Image
viewtopic.php?f=6&t=88153

Image
viewtopic.php?f=6&t=68814

Funny enough, that second one is somewhat related to your other topic.
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: GUI it works, but I don't know why

16 Sep 2021, 04:50

Great, :dance: I'll use it for other menus or developments. In the current menu I keep the "artistic" line that I have followed.

Thanks very much you for all your help. :D I was stuck.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mebelantikjaya and 295 guests