Center Image

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Center Image

02 Nov 2013, 10:51

I've found the next script to center an image on the screen

Code: Select all

Gui, Margin, 0,0
Gui 1:-owner -toolwindow -caption +LastFound
Gui, Add, Picture, vMyPic, C:\Documents and Settings\Administrador\Escritorio\Letras\A.png-1.jpg
Gui, Show, % "W" . A_ScreenWidth . " H" . A_ScreenHeight . " X0 Y0", My GUI
GuiControlGet, Pic, Pos, MyPic
xx := (A_ScreenWidth-picw)/2
yy := (A_ScreenHeight-pich)/2
GuiControl, MoveDraw, MyPic, x%xx% y%yy%
WinSet,Transparent,0 ,My GUI
Return

ESC::
GuiClose:
Gui, Destroy
;ExitApp 
I want to convert in a function,

Code: Select all

SetWorkingDir %A_ScriptDir% 

;imagen = Teclado-Spectrum.png
CentrarImagen(Teclado-Spectrum.png)
return

CentrarImagen(nombreImagen)
{
Gui, Margin, 0,0
Gui 1:-owner -toolwindow -caption +LastFound
Gui, Add, Picture, vMyPic, nombreImagen
Gui, Show, % "W" . A_ScreenWidth . " H" . A_ScreenHeight . " X0 Y0", My GUI
GuiControlGet, Pic, Pos, MyPic
xx := (A_ScreenWidth-picw)/2
yy := (A_ScreenHeight-pich)/2
GuiControl, MoveDraw, MyPic, x%xx% y%yy%
WinSet,Transparent,0 ,My GUI
Return
}



ESC::

GuiClose:
Gui, Destroy
;ExitApp 
What is the wrong? I get Error: A control's variable must be global or static in the line
Gui, Add, Picture, vMyPic, nombreImagen

I don't know where is my problem.

Thanks!
Everything is possible!
User avatar
LinearSpoon
Posts: 156
Joined: 29 Sep 2013, 22:55

Re: Center Image

02 Nov 2013, 11:46

Inside the function, a newly declared variable (MyPic) is considered "local". The easiest fix would be to use 'global MyPic' as the first line of the function. Also nombreImagen should be in %% when you add the picture.

Code: Select all

CentrarImagen(nombreImagen)
{
Global MyPic
Gui, Margin, 0,0
Gui 1:-owner -toolwindow -caption +LastFound
Gui, Add, Picture, vMyPic, %nombreImagen%
Gui, Show, % "W" . A_ScreenWidth . " H" . A_ScreenHeight . " X0 Y0", My GUI
GuiControlGet, Pic, Pos, MyPic
xx := (A_ScreenWidth-picw)/2
yy := (A_ScreenHeight-pich)/2
GuiControl, MoveDraw, MyPic, x%xx% y%yy%
WinSet,Transparent,0 ,My GUI
Return
}
Also, in the line CentrarImagen(Teclado-Spectrum.png), the picture file should be in quotations like so:

Code: Select all

CentrarImagen("Teclado-Spectrum.png")
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Center Image

02 Nov 2013, 12:02

That should be the only combination I had not tried ... :D
Do not quite understand but now works fine.

Thanks very much!
Everything is possible!
Alibaba
Posts: 480
Joined: 29 Sep 2013, 16:15
Location: Germany

Re: Center Image

02 Nov 2013, 19:30

Have you read this?
http://l.autohotkey.net/docs/Functions.htm#Locals

Maybe it'll help you to understand the different variable modes.
"Nothing is quieter than a loaded gun." - Heinrich Heine

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: RandomBoy, Rohwedder, ruespe and 379 guests