Updating a picture from another script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Axetar
Posts: 11
Joined: 11 Mar 2023, 00:45
Contact:

Updating a picture from another script

10 Aug 2023, 16:00

Hello,
I'm familiar with using Control and ControlSetText functions. However, I'm uncertain if you can use them to modify or refresh an image. I have an ongoing infinite loop in a separate script to update the image. Strangely, when I switch the GUI element from an image to text, the modification works perfectly. While I'm inclined to believe it might not be possible, I'm open to any potential workarounds you might suggest. Your insights would be greatly appreciated! :bravo:

Example:
Script1

Code: Select all

#SingleInstance, Force
SetWorkingDir, A_ScriptDir
Gui, CleverGUI: Add, Picture, x5 y30 h35 w175 vPictures , %A_ScriptDir%\Round103_1366.png
Gui, CleverGUI: Show
Script2

Code: Select all

F3::
    Control, Picture, %A_ScriptDir%\Round103_1920.png, Static1, CleverGUI
Return
User avatar
Hellbent
Posts: 2114
Joined: 23 Sep 2017, 13:34

Re: Updating a picture from another script

10 Aug 2023, 18:35

Here is one way you can do it.

.
select image.gif
select image.gif (88.87 KiB) Viewed 308 times
.

Script 1 ( With the gui )

Code: Select all

#SingleInstance, Force
SetWorkingDir, % A_ScriptDir

Gui, New, +AlwaysOnTop
Gui, Add, Picture, xm ym w200 h200 hwndPicHwnd, C:\Users\CivRe\OneDrive\Desktop\AHK Tools\Color Picker Mini\Screen Shots\20210117095023.png
Gui, Show,, My Image gui title

ImageList := ""
Loop, C:\Users\CivRe\OneDrive\Desktop\AHK Tools\Color Picker Mini\Screen Shots\*.*
	ImageList .= A_LoopFileFullPath "|"
  
Gui, Add, ListBox, x-100 y0 w10 h10 gUpdatePicture hwndListHwnd Disabled, % ImageList

return
GuiClose:
*ESC:: ExitApp

UpdatePicture:
	GuiControlGet, out , , % ListHwnd
	GuiControl, , % PicHwnd , % out
	return
Script 2 ( With the hotkey that changes the image in the gui in script 1 )

Code: Select all

#SingleInstance, Force

Index := 1
return
*ESC::ExitApp

;&&&&&&&&&&&&&&&&&&&&&	

F1::
	Control, Choose , % Index++ , ListBox1 , My Image gui title 
	return
	
;&&&&&&&&&&&&&&&&&&&&&	
**Edit**
Added Disabled to the listbox so that you can't interact with it via normal means.
Axetar
Posts: 11
Joined: 11 Mar 2023, 00:45
Contact:

Re: Updating a picture from another script

10 Aug 2023, 19:58

What a fantastic idea! I absolutely love it—thank you so much. I'm not sure why I didn't think of it earlier LOL. :superhappy:
User avatar
Hellbent
Posts: 2114
Joined: 23 Sep 2017, 13:34

Re: Updating a picture from another script

11 Aug 2023, 01:27

I'm not really sure of the purpose of your script but I know that the use case likely will require you to make some changes to better suit your particular needs. So here are a few examples that use the same basic concept of causing an event in a window to get it to run a routine.

This shows how you can load something into your clipboard and then trigger the gui routine to update the picture with the contents of the clipboard.

Code: Select all

;*****************************************************************************************************
;*****************************************************************************************************
;*****************************************************************************************************
;Script 1

#SingleInstance, Force

Gui, New, +AlwaysOnTop
Gui, Add, Picture, xm ym w200 h200 hwndPicHwnd, ImagePath.png
Gui, Show,, My Image gui title

Gui, Add, Checkbox, x-100 y0 w10 h10 gUpdatePicture hwndCheckboxHwnd Disabled, 

return
GuiClose:
*ESC:: ExitApp

UpdatePicture:
	GuiControl, , % PicHwnd , % Clipboard
	return

;*****************************************************************************************************
;*****************************************************************************************************
;*****************************************************************************************************
;Script 2

#SingleInstance, Force

return
*ESC::ExitApp

;&&&&&&&&&&&&&&&&&&&&&	

F1::
	Backup := ClipboardAll
	sleep, 30
	Clipboard := "Image\Path.png"
	sleep, 30
	Control, % ( Tog := !Tog ) ? ( "Check" ) : ( "UnCheck" ) , , Button1 , My Image gui title 
	Sleep, 100
	Clipboard := Backup
	return
	
;&&&&&&&&&&&&&&&&&&&&&	

;*****************************************************************************************************
;*****************************************************************************************************
;*****************************************************************************************************

This second example takes the original code and has the window resized based on the new images size. [ Ratio can also be used ]

Code: Select all

;*****************************************************************************************************
;*****************************************************************************************************
;*****************************************************************************************************
;Script 1

#SingleInstance, Force

Gui, New, +AlwaysOnTop
Gui, Add, Picture, xm ym w200 h200 hwndPicHwnd, Image.png
Gui, Show,, My Image gui title

ImageList := ""
Loop, Path\*.png
	ImageList .= A_LoopFileFullPath "|"
  
Gui, Add, ListBox, x-100 y0 w10 h10 gUpdatePicture hwndListHwnd Disabled, % ImageList

return
GuiClose:
*ESC:: ExitApp

UpdatePicture: 
	
	;Get the currently selected item in the listbox
	GuiControlGet, out , , % ListHwnd 
	
	;add a picture control to a non-existing gui and set it with the new image so that the size of the image can be obtained.
	Gui, FakeGui:Add, Picture, +hwndFakePicHwnd, % out
	
	;get the size of the control ( "pos" becomes: posX, posY, posW, posH )
	GuiControlGet, pos , FakeGui:pos , % FakePicHwnd
	
	;Destroy the fake gui without ever having shown it. Free memory.
	Gui, FakeGui:Destroy
	
	;Change the width and height of the original picture control ( or use the new ratio between width/height and a fixed max/min size )
	GuiControl, MoveDraw, % PicHwnd , % " w" posW  " h" posH 
	
	;Update the picture control with the new image
	GuiControl, , % PicHwnd , % out
	
	;resize the window to adjust for the new picture size ( adding a position i.e. x/y is an option )
	Gui, Show, AutoSize
	
	return

;*****************************************************************************************************
;*****************************************************************************************************
;*****************************************************************************************************
;Script 2

#SingleInstance, Force

Index := 1
return
*ESC::ExitApp

;&&&&&&&&&&&&&&&&&&&&&	

F1::
	Control, Choose , % Index++ , ListBox1 , My Image gui title 
	return
	
;&&&&&&&&&&&&&&&&&&&&&	

;*****************************************************************************************************
;*****************************************************************************************************
;*****************************************************************************************************


Related to resizing the picture control is how to move additional controls around based on the new image size.
So here is where you can find an example of storing the data for your controls in a structure that allows for easier manipulation of their values and positions.

Reposition Gui Controls

.
Image
.


Here is a more complex example that uses a relative relationship between different controls.
So for example, the text control is related to the 0/0 position of the window and the button is positioned relative to the text controls position
and the checkbox is relative to the button, and on and on.
Once any value is changed, the controls can be looped and their positions updated based on the relationships.

Relative Positioning

.
Image
.

This is an example of what a typical relationship looks like.

Code: Select all

Button.X := Text.X + Text.W + Margin
and that is written out as a string:

Code: Select all

;The "X", "W", etc. are based off the control that is partnered with this one.
... , X: "X + W + 10" , ...
The expressions that can currently be used are limited but the use of new expressions can be added in by following the same concept as the others use.

That's likely way more than you will ever need to know but someone else might find it useful.
Axetar
Posts: 11
Joined: 11 Mar 2023, 00:45
Contact:

Re: Updating a picture from another script

09 Sep 2023, 23:50

I'm not sure of why I didn't think of this earlier. I ended up using PostMessage, OnMessage and GuiControl. :superhappy:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, macromint, peter_ahk and 321 guests