Centre Gui to desktop

Get help with using AutoHotkey and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 906
Joined: 29 Sep 2013, 22:18

Centre Gui to desktop

22 Oct 2013, 21:32

I hoped this function would centre the activewindow to the desktop but it seems to be offsetting for the height of the Taskbar. How can I get it to ignore the taskbar? Thanks.

Code: Select all

CentreWindow()
{	
	Wingetpos, ,, sizW, sizH, A
	
	offX := (%A_screenwidth%/2) - (sizW/2)
	offY := (%A_screenwidth%/2) - (sizH/2)

	Winmove, %offX%, %offY%
}
User avatar
MasterFocus
Posts: 146
Joined: 01 Oct 2013, 09:47
GitHub: MasterFocus
Location: Rio de Janeiro - RJ - Brasil
Contact:

Re: Centre Gui to desktop

22 Oct 2013, 22:21

MonitorWorkArea [, N]: Same as the above except the area is reduced to exclude the area occupied by the taskbar and other registered desktop toolbars.
http://www.autohotkey.com/docs/commands/SysGet.htm
Antonio França - git.io | github.com | ahk4.net | sites.google.com
Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.
Need help? Please post on the forum before sending me a PM.
User avatar
PuzzledGreatly
Posts: 906
Joined: 29 Sep 2013, 22:18

Re: Centre Gui to desktop

22 Oct 2013, 22:49

Thanks for the link, but my problem is the opposite. I don't want to reduce the area to exclude the taskbar - that is what the function I posted is already doing. Here is a screen shot. I have a fullscreen GUI (yellow) behind a second GUI - it is nearer the top of the screen than it should be.
Menu.png
Menu.png (19.59 KiB) Viewed 4330 times
User avatar
MasterFocus
Posts: 146
Joined: 01 Oct 2013, 09:47
GitHub: MasterFocus
Location: Rio de Janeiro - RJ - Brasil
Contact:

Re: Centre Gui to desktop

22 Oct 2013, 22:59

Use Window Spy to retireve the ahk_class of the Taskbar itself (I forgot it).
Then, retrieve its height via WinGet.
This way, you'll be able to calculate the correct position.
Antonio França - git.io | github.com | ahk4.net | sites.google.com
Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.
Need help? Please post on the forum before sending me a PM.
gregster
Posts: 4910
Joined: 30 Sep 2013, 06:48

Re: Centre Gui to desktop

22 Oct 2013, 23:06

You are looking for the complete screen height, including taskbar height, right?

Then use

Code: Select all

WinGetPos , , , Width, Height, Program Manager
or

Code: Select all

Sysget, var, Monitor
msgbox % varBottom
Edit: Actually, A_screenheight should give you the same result. You are currently using A_screenwidth for determing both offsets!? That can't work, anyway... Or am I understanding you totally wrong?
Last edited by gregster on 22 Oct 2013, 23:20, edited 1 time in total.
User avatar
MasterFocus
Posts: 146
Joined: 01 Oct 2013, 09:47
GitHub: MasterFocus
Location: Rio de Janeiro - RJ - Brasil
Contact:

Re: Centre Gui to desktop

22 Oct 2013, 23:20

gregster wrote:You are currently using A_screenwidth for determing both offsets!?
Indeed! I didn't notice that.
Antonio França - git.io | github.com | ahk4.net | sites.google.com
Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.
Need help? Please post on the forum before sending me a PM.
gregster
Posts: 4910
Joined: 30 Sep 2013, 06:48

Re: Centre Gui to desktop

22 Oct 2013, 23:35

Another thing: I think the %s are not right here:

Code: Select all

offX := (%A_screenwidth%/2) - (sizW/2)
Neither at the WinMove:
Try instead:

Code: Select all

CenterWindow(WinTitle)
{
    WinGetPos,,, Width, Height, A
    WinMove, A,, (A_ScreenWidth/2)-(Width/2), (A_ScreenHeight/2)-(Height/2)
}
(from the AHK help)
User avatar
MasterFocus
Posts: 146
Joined: 01 Oct 2013, 09:47
GitHub: MasterFocus
Location: Rio de Janeiro - RJ - Brasil
Contact:

Re: Centre Gui to desktop

23 Oct 2013, 01:05

Antonio França - git.io | github.com | ahk4.net | sites.google.com
Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.
Need help? Please post on the forum before sending me a PM.
User avatar
PuzzledGreatly
Posts: 906
Joined: 29 Sep 2013, 22:18

Re: Centre Gui to desktop

23 Oct 2013, 05:44

Doh! I thought I was half a sleep when I wrote that code - turned out I was completely comatose! Thanks for the replies and for spotting what I shouldn't have missed.
User avatar
PuzzledGreatly
Posts: 906
Joined: 29 Sep 2013, 22:18

Re: Centre Gui to desktop

23 Oct 2013, 15:05

I've been looking at the function by MasterFocus and I've been trying to add the option to offset the X and Y position. This is what I tried:

Code: Select all

WinMovePos(p_Pos="",offX=0,offY=0,p_WinTtl="A",p_WinTxt="",p_ExcTtl="",p_ExcTxt="")
{
	; adapted from MasterFocus's WinMoveSpecial
	; First parameter: TOP | TOPRIGHT | RIGHT | BOTTOMRIGHT | BOTTOM | BOTTOMLEFT | LEFT | TOPLEFT | CENTER
	; (anything else will result in CENTER)
	; The other parameters: WinTitle, WinText, ExcludeTitle, ExcludeText
	
	WinGetPos,,,l_W,l_H,%p_WinTtl%,%p_WinTxt%,%p_ExcTtl%,%p_ExcTxt%

	l_X:=InStr(p_Pos,"LEFT",0) ? 0 : InStr(p_Pos,"RIGHT",0) ? A_ScreenWidth-l_W+offX : (A_ScreenWidth-l_W)/2 
	l_Y:=InStr(p_Pos,"TOP",0) ? 0 : InStr(p_Pos,"BOTTOM",0) ? A_ScreenHeight-l_H+offY : (A_ScreenHeight-l_H)/2

	WinMove,%p_WinTtl%,%p_WinTxt%,%l_X%,%l_Y%,,,%p_ExcTtl%,%p_ExcTxt%
}
But this doesn't work for all positions. For example, the offset for TOPRIGHT doesn't work on the Y axis and BOTTOMLEFT doesn't work on the X axis. I don't fully understand the code to work out what's wrong. Can someone suggest a fix. Thanks.
User avatar
MasterFocus
Posts: 146
Joined: 01 Oct 2013, 09:47
GitHub: MasterFocus
Location: Rio de Janeiro - RJ - Brasil
Contact:

Re: Centre Gui to desktop

26 Oct 2013, 02:27

This is what you missed:
l_X:=InStr(p_Pos,"LEFT",0) ? 0 : InStr(p_Pos,"RIGHT",0) ? A_ScreenWidth-l_W+offX : (A_ScreenWidth-l_W)/2
l_Y:=InStr(p_Pos,"TOP",0) ? 0 : InStr(p_Pos,"BOTTOM",0) ? A_ScreenHeight-l_H+offY : (A_ScreenHeight-l_H)/2
The colored parts are the possible values for each variable. Notice they can both be 0.
l_X is set to 0 when LEFT is specified. l_Y is set to 0 when TOP is specified.
You have to add your offsets there as well.
Antonio França - git.io | github.com | ahk4.net | sites.google.com
Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.
Need help? Please post on the forum before sending me a PM.
User avatar
PuzzledGreatly
Posts: 906
Joined: 29 Sep 2013, 22:18

Re: Centre Gui to desktop

26 Oct 2013, 07:33

Thanks very much, the function works correctly now and I think I've a better understanding of your code.

Return to “Ask For Help”

Who is online

Users browsing this forum: boiler, Dario91, dw0rd1337, mikeyww, patilizer, PlumpSamurai, SSQUAD and 52 guests