Easiest way to offset a picture vertically from center? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Jose Hidalgo
Posts: 222
Joined: 07 Mar 2021, 07:44

Easiest way to offset a picture vertically from center?

Post by Jose Hidalgo » 27 Mar 2023, 10:24

Hi everybody,

I'm currently displaying a picture + a border, with this bit of code (using Imagen for high-quality purposes):

Code: Select all

Gui, %ImgGuiName%: Add, Picture, x%BorderWidth% y%BorderWidth% h%WantedHeight% w%WantedWidth% HwndhPic
Imagen(ImgLoc, "ahk_id" . hPic)
The picture and border are of course perfectly centered. This made me wonder: what if I wanted to slightly offset them vertically, so they become centered relative to the screen height minus the taskbar?
My taskbar being 40 pixels high, I'd need to offset the picture and border 20 pixels up.

What would be the easiest way to achieve that? Thank you.

[Mod edit: Moved topic to AHK v1 help.]

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Easiest way to offset a picture vertically from center?

Post by mikeyww » 27 Mar 2023, 10:59

Code: Select all

#Requires AutoHotkey v1.1.33
SysGet mon, MonitorWorkArea
monHeight       := monBottom - monTop + 1
guiClientHeight := monHeight - 60
Gui -DPIScale
Gui Add, Text,, Test
Gui Show, w400 h%guiClientHeight%


User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Easiest way to offset a picture vertically from center?

Post by mikeyww » 27 Mar 2023, 12:03

You are welcome.

Concepts of this script:
1. The GUI height configuration refers to the window's client area.
2. The -DPIScale option can be helpful for certain kinds of positioning, as this might otherwise occur with scaling.
3. Gui, Show automatically centers the GUI unless directed otherwise.
4. SysGet can be used to get the working area of the monitor. This area excludes the taskbar and other registered desktop toolbars.

Some of these features are not needed in the posted script, but you may find them useful in other scripts.

garry
Posts: 3720
Joined: 22 Dec 2013, 12:50

Re: Easiest way to offset a picture vertically from center?

Post by garry » 27 Mar 2023, 15:05

in my case , 4K win11 , seems the variable hx correct

Code: Select all

;-- Example 4K = 3840 * 2160
#Requires AutoHotkey v1.1
Gui, -DPIScale -caption                                ;- height hx is ok with -caption otherwise must be smaller 
WinGetPos,,,, TaskBarHeight, ahk_class Shell_TrayWnd
hx:=a_screenheight-TaskBarHeight                       ;- hx=2076  is screenheight-Taskbarheight ( if taskbar at bottom )
;-
SysGet mon, MonitorWorkArea
monHeight       := monBottom - monTop
msgbox,hx=%hx%`nmonheight=%monheight%`nmontop=%montop%`nmonbottom=%monbottom%  ;- monheight amd monbotton is 2160
;-
Gui Add, Text,, USE ESC to quit
Gui Show,x400 y0 w600 h%hx%
return
esc::exitapp
;=====================================================

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Easiest way to offset a picture vertically from center?

Post by mikeyww » 27 Mar 2023, 16:01

Use whichever is more fitting for your need!

garry
Posts: 3720
Joined: 22 Dec 2013, 12:50

Re: Easiest way to offset a picture vertically from center?

Post by garry » 28 Mar 2023, 05:20

sorry, I didn't see what happens with sysget , because > A_screenheight and monHeight and monBottom is same = 2160

Code: Select all

;-- Example 4K = 3840 * 2160
#Requires AutoHotkey v1.1
SysGet mon, MonitorWorkArea
monHeight:= monBottom - monTop  ;- 2160-0=2160
msgbox,A_screenheight=%A_screenheight%`nmonheight=%monheight%`nmontop=%montop%`nmonbottom=%monbottom% 

Jose Hidalgo
Posts: 222
Joined: 07 Mar 2021, 07:44

Re: Easiest way to offset a picture vertically from center?

Post by Jose Hidalgo » 28 Mar 2023, 19:00

Here's the thing I don't understand:

In the first example, the Gui is automatically centered vertically to the working area (screen height - taskbar height), instead of being centered vertically to the whole screen height.
I don't see any command for that specific purpose, and yet it happens.

And in my script, with a similar Gui command, the Gui is automatically centered vertically to the whole screen height. Which is not what I want, obviously.
The only difference that I see is that I'm displaying a picture instead of text.

What's happening?

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Easiest way to offset a picture vertically from center?

Post by mikeyww » 30 Mar 2023, 08:39

Perhaps the answer is in the function that you have not posted.

Here is a GUI with a picture, no problems here.

Code: Select all

#Requires AutoHotkey v1.1.33
SysGet mon, MonitorWorkArea
monHeight       := monBottom - monTop + 1
guiClientHeight := monHeight - 60
image           := A_ScriptDir "\test.png"
Gui -DPIScale -Caption
Gui Margin, 0, 0
Gui Add, Pic, h%guiClientHeight%, % image
Gui Show
What "similar Gui command" do you mean? Your script does not even include a Show subcommand.

Adding a picture does not position the GUI. The Show subcommand positions the GUI.

garry
Posts: 3720
Joined: 22 Dec 2013, 12:50

Re: Easiest way to offset a picture vertically from center?

Post by garry » 30 Mar 2023, 13:10

also an example as slideshow , define a folder for test

Code: Select all

/*
------------------------------------
script   =Foto_SlideShow
modified =20230330 
created  =20190809
define your variable-folder > MF
use ESC to quit
;============ GUISIZEx DPIx 4Kx 3840*2160 is 100% ============
*/

#Requires AutoHotkey v1.1
#Warn
setworkingdir,%a_scriptdir%
SetBatchLines, -1
WinGetPos,,,, TaskBarHeight, ahk_class Shell_TrayWnd
hx:=a_screenheight-TaskBarHeight
;------ your variables -------------
mf=D:\m_media\m_foto                   ;- <<<< your PATH here
extensions :="jpg,bmp,png"
seconds    := 1
;-----------------------------------
wa:=A_screenwidth,ha:=A_screenHeight,xx:=100
y:=(ha*0)/xx, w:=(wa*67)/xx,h:=hx              ;- height = y0 -- until taskbar
e:="" , seconds:=(seconds*1000)
loop,%mf%\*.*,0,1
  {
  SplitPath,a_loopfilefullpath, name, dir, ext, name_no_ext, drive
  if ext in %extensions%
    e .= a_loopfilefullpath . "`r`n"
  }
if e=
{
msgbox, 262208,FILES ,No pictures found`nDefine your pictures-folder in this script
exitapp
}
;---------------------
Loop,parse,e,`n,`r
 {
 xc:=a_loopfield
 if xc=
   continue
 imgSize(xc,imgw,imgh)
 sleep,100
 Gui,2:new
 Gui,2: -dpiscale +AlwaysOnTop -Caption
 Gui,2: Margin, 0, 0
 Gui,2:Color,Black,Black
 if (imgw>imgh)
   gui,2:add, Picture, x0 y0 h-1 w%w%,%xc%
 else if (imgh>imgw)
   gui,2:add, Picture, x0 y0 h%h% w-1 ,%xc%
 Gui,2: Show,y%y% h%h% ,Test2
 sleep,%seconds%
 ;keywait,space,D                      ;- <<< or instead sleep seconds use SPACE-key to continue
}
return
;---------------------
esc::exitapp
;---------------------
imgSize(img, ByRef width , ByRef height) { ; Get image's dimensions
 If FileExist(img) {
  GUI, Add, Picture, hwndpic, %img%
  ControlGetPos,,, width, height,, ahk_id %pic%
  Gui, Destroy
 } Else height := width := 0
}
;================================================================

Jose Hidalgo
Posts: 222
Joined: 07 Mar 2021, 07:44

Re: Easiest way to offset a picture vertically from center?

Post by Jose Hidalgo » 30 Mar 2023, 20:28

mikeyww wrote:
30 Mar 2023, 08:39
What "similar Gui command" do you mean? Your script does not even include a Show subcommand.

Adding a picture does not position the GUI. The Show subcommand positions the GUI.
Agreed. Here's the complete script excerpt:

Code: Select all

global DPF := A_ScreenDPI / 96				; Windows DPI factor of the user's own screen
global WantedHeight := Round(A_ScreenHeight * HeightFactor / DPF, 0)
global CustomWidth := 1000						; Width  of all the custom jpg images
global CustomHeight := 1370						; Height of all the custom jpg images
global BorderWidth := Round(5 / DPF, 0)			; Border thickness around the image GUIs

; Later in the script...

Width  := CustomWidth
Height := CustomHeight
WantedWidth := WantedHeight * (Width / Height)
wtemp := WantedWidth + BorderWidth * 2
htemp := WantedHeight + BorderWidth * 2
xLeft := %Listener%xCenter - wtemp / 2 * DPF
yLeft := A_ScreenHeight / 2 - htemp / 2 * DPF

; Later in the script...

Gui, %ImgGuiName%: Add, Picture, x%BorderWidth% y%BorderWidth% h%WantedHeight% w%WantedWidth% HwndhPic
Imagen(ImgLoc, "ahk_id" . hPic)				; Imagen is here: https://www.autohotkey.com/boards/viewtopic.php?t=82005
Gui, %ImgGuiName%: Font, w400 s%SizeSmall%, %TextFont%
Gui, %ImgGuiName%: Show, x%xLeft% y%yLeft% w%wtemp% h%htemp% NA

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Easiest way to offset a picture vertically from center?

Post by mikeyww » 30 Mar 2023, 20:45

Suggestions:
1. When you post a script, post something that the forum reader can run to demonstrate the problem that you are experiencing. Run the actual script that you are posting, to see if it runs.
2. Since you state that the y-value is wrong, take it apart: examine the variables used to compute your y-value. See which of them is the wrong or undesired value. Understand how your final y-value is computed. Determine the values of the contributing variables. Use the script to show you those values, or use a debugging tool. What is the actual y-value used by the script? What is the desired y-value? Why isn't the desired value the computed value? Do you really need the screen DPI, or would -DPIScale work just as well?

Jose Hidalgo
Posts: 222
Joined: 07 Mar 2021, 07:44

Re: Easiest way to offset a picture vertically from center?  Topic is solved

Post by Jose Hidalgo » 31 Mar 2023, 10:58

It's OK, I sorted it out.
Like said, it was just the y value. I just had to write something like "y value := y value - taskbar height / 2". I should have thought about it from the start.

Next time I'll post my 3.000 line script. That will work.

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Easiest way to offset a picture vertically from center?

Post by mikeyww » 31 Mar 2023, 12:32

If GUI automatically centers the window, then why do you need to include any specific y-value?

Post Reply

Return to “Ask for Help (v1)”