Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Win7 GUI Dimensions Wrong


  • Please log in to reply
12 replies to this topic
TheDewd
  • Members
  • 842 posts
  • Last active: Jun 10 2016 06:55 PM
  • Joined: 28 Mar 2010
I'm trying to create a GUI in Windows 7 with a specific width and height, but Window Spy is reporting entirely different dimensions.

CODE
Gui, Show, w500 h300, Settings
return

Window Spy
>>>>>>>>>>( Window Title & Class )<<<<<<<<<<<
Settings
ahk_class AutoHotkeyGUI

>>>>>>>>>>>>( Mouse Position )<<<<<<<<<<<<<
On Screen:	777, 546  (less often used)
In Active Window:	70, 185

>>>>>>>>>( Now Under Mouse Cursor )<<<<<<<<

Color:	0xF0F0F0  (Blue=F0 Green=F0 Red=F0)

>>>>>>>>>>( Active Window Position )<<<<<<<<<<
left: 707     top: 361     width: 506     height: 328

>>>>>>>>>>>( Status Bar Text )<<<<<<<<<<

>>>>>>>>>>>( Visible Window Text )<<<<<<<<<<<

>>>>>>>>>>>( Hidden Window Text )<<<<<<<<<<<

>>>>( TitleMatchMode=slow Visible Text )<<<<

>>>>( TitleMatchMode=slow Hidden Text )<<<<

Even if I try using SmartGui Creator's "Gui Stealer" to copy the dimensions of another window, it will still have different dimensions when the code is executed. Why is this?

janopn
  • Members
  • 694 posts
  • Last active: Jun 12 2017 03:41 PM
  • Joined: 08 Jul 2011
I think window spy counts the margins and toolbars, and etc...
All in all is all we are ~

sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
IIRC, the dimensions you specify in Gui, Show are the dimensions for the client area of the window, not the dimensions of the GUI window itself.

  • Guests
  • Last active:
  • Joined: --
sinkfaze is right. The dimensions used in Gui, Show are for the gui's client area.

Use WinMove to give a gui window's actual size.
Gui +LastFound
Gui, Show, Hide
WinMove,,,,, 500, 300
Gui, Show, XCenter YCenter, Das GUI
Return
GuiClose:
Exitapp


TheDewd
  • Members
  • 842 posts
  • Last active: Jun 10 2016 06:55 PM
  • Joined: 28 Mar 2010

sinkfaze is right. The dimensions used in Gui, Show are for the gui's client area.

That's perfect! Thank you all so much!

Scoox
  • Members
  • 194 posts
  • Last active: Jun 09 2017 03:34 AM
  • Joined: 28 Nov 2010
However, AHK and Window Spy still get window sizes wrong some times. Two days ago I wrote a script to draw a red rectangular border around a window, WindowBorderDraw(WinTitle, Thickness=5, Color="Red"). For most windows, the red border outside dimensions are the same as the target window outside dimensions, but for certain windows, e.g. ahk_class #32770 the retrieved size is actually less than the actual size, resulting in the border being smaller than the target window. I tried to make an exception list to compensate, but later I learnt that not all ahk_class #32770 windows report the same size.

Initially I thought it may be a quirk of the OS in how it reports window information, but then I remembered that Sandboxie can draw a color border around any window and does so reliably every time, with border and window dimensions always perfectly matched, so what gives?

RegJump() - Jump to registry path in Regedit

HoverScroll() - HoverScroll() - Scroll controls without focus


Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
@ Scoox,

Please post an example of your script that we can test on different windows for this anomily that you describe.

Please give examples of windows that work properly and of those that fail (preferrably those that most windows installations have)

Scoox
  • Members
  • 194 posts
  • Last active: Jun 09 2017 03:34 AM
  • Joined: 28 Nov 2010
Alright, here it is. The code should be good to go. When you run it a color border will be drawn around the window currently under the mouse pointer. You can toggle the border on/off by pressing the Esc key.

The rectangle will match the size of most windows, but for certain windows the border will be bigger or smaller than the window. Examples are some Open/Save file dialogues, and system tray flyout windows e.g. wireless networks viewer, battery info, etc.

#NoEnv
#SingleInstance Force
#Persistent
SetBatchLines -1

BorderOn := False
GoSub ToggleBorder

Esc::GoSub ToggleBorder

;Toggle window border
ToggleBorder:
	If BorderOn
	{
		BorderOn := False
		Settimer, DrawBorder, Off
		WindowBorderKill()
	}
	Else
	{
		BorderOn := True
		WindowBorderInit(0xFF0000)
		SetTimer, DrawBorder, 15
	}
Return

DrawBorder:
	MouseGetPos,,, WinID
	WinTitle = ahk_id %WinID%
	WindowBorderDraw(WinTitle)
Return

;Creates a rectangle around target window
;WinTitle: Window to draw border around. Use standard AHK WinTitle conventions e.g. "ahk_id #32770"
;Thickness: Thickness of border in pixels
WindowBorderDraw(WinTitle, Thickness=5)
{
	SetTitleMatchMode, 2
	;Avoid unnecessary problems
	IfWinNotExist %WinTitle%
		Return

	;If target window is border itself, do nothing
	WinGetTitle, WinName, %WinTitle% ;Get target window title
	If InStr(WinName, "WinBorder")
		Return

	;Get position target window details
	WinGetPos, WinX, WinY, WinW, WinH, %WinTitle%
	WinGetClass, WinClass, %WinTitle%
	WinGet, MinMaxStatus, MinMax, %WinTitle%

	If(WinW < 0)
		WinX += WinW, WinW *= -1
	If(WinH < 0)
		WinY += WinH, WinH *= -1

	;Correct incorrect window sizes reported by maximized windows
	If (MinMaxStatus = 1) ;Maximized window
		WinX += 8, WinY += 8, WinW -= 16, WinH -= 16

	;Calculate border coords
	X0 := WinX, X1 := WinX + WinW - Thickness
	Y0 := WinY, Y1 := WinY + WinH - Thickness

	/*
		X0,Y0	 X1,Y0
		 +----------+
		 |      |
		 |      |
		 +----------+
	    X0,Y1	 X1,Y1
	*/

	;Display border
	Gui, 96: Show, x%X0% y%Y0% w%WinW% h%Thickness% NA, WinBorder96
	Gui, 97: Show, x%X1% y%Y0% w%Thickness% h%WinH% NA, WinBorder97
	Gui, 98: Show, x%X0% y%Y1% w%WinW% h%Thickness% NA, WinBorder98
	Gui, 99: Show, x%X0% y%Y0% w%Thickness% h%WinH% NA, WinBorder99
}

;Color: Border color, can be RGB value or a color name e.g. 0xFF0000 (no quotes) or "Red"
;Opacity: a value in the range 0-255
WindowBorderInit(Color="Red", Opacity=255)
{
	Loop, 4
	{
		Name := A_Index + 95
		Gui, %Name%: +ToolWindow -Caption +AlwaysOnTop
		Gui, %Name%: Color, %Color%
		Gui, WinBorder: +LastFound ;Make the GUI window the last found window for use by WinSet
		WinSet, Transparent, %Opacity%
	}
}

;Removes border created by WindowBorderDraw()
WindowBorderKill()
{
	Loop, 4
	{
		Name := A_Index + 95
		Gui, %Name%: Destroy
	}
}

RegJump() - Jump to registry path in Regedit

HoverScroll() - HoverScroll() - Scroll controls without focus


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

However, AHK and Window Spy still get window sizes wrong some times.


AHK returns the window size as it is told by the OS. What you're seeing is not the window itself, but the window's client area plus glassy frames rendered by the desktop window manager. You can use the following to test and compare:

Gui Add, Button, gToggleOne, Toggle DWM for this window
Gui Add, Button, gToggleAll, Toggle DWM for all windows
Gui Show
return

ToggleOne:
Gui +LastFound
DllCall("dwmapi\DwmSetWindowAttribute", "ptr", WinExist(), "uint", 2
        , "int*", disabled := !disabled, "uint", 4)
return

ToggleAll:  ; Won't work on Windows 8.
DllCall("dwmapi\DwmEnableComposition", "uint", !(enable_dwm := !enable_dwm))
return

GuiClose:
GuiEscape:
ExitApp

Sandboxie can draw a color border around any window and does so reliably every time,


Sandboxie probably calculates the apparent window size rather than using the actual dimensions reported by the OS.

Scoox
  • Members
  • 194 posts
  • Last active: Jun 09 2017 03:34 AM
  • Joined: 28 Nov 2010
Thanks Lexikos. After turning off desktop window manager border and apparent window dimensions match. However, I am quite in love with the glossy eyecandy so I wonder if there's a simple way to calculate apparent window sizes a la Sandboxie. Another app I use called WindowSpace is able to snap windows to screen edges and to other windows, regardless of whether DWM is on or off, and again the windows are never off by x pixels meaning that somehow it knows where the edges are. I don't know how they do it but there has to be a proper way to retrieve the apparent window size.

RegJump() - Jump to registry path in Regedit

HoverScroll() - HoverScroll() - Scroll controls without focus


lblb
  • Members
  • 120 posts
  • Last active: Dec 02 2015 08:05 AM
  • Joined: 22 May 2012

Hey Scoox,

 

I just want to say thanks for your code in post #8. It works beautifully (with some minor modifications) in a function where I drag and drop gui icons onto the highlighted window:

http://www.autohotke...05-hover-icons/



guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011

here was my bug report which afaict relates to the issues in this thread

http://www.autohotke...and-aero-theme/



guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
AHK returns the window size as it is told by the OS. What you're seeing is not the window itself, but the window's client area plus glassy frames rendered by the desktop window manager.

 

-----------------------------------------------------------------------------

 

here was my bug report which afaict relates to the issues in this thread

http://www.autohotke...and-aero-theme/

 

my guess was that AHK was setting a wrong style somewhere for the gui and that is why the OS/AHK is reporting the wrong size for the gui whereas it works correctly for other windows even if DWM/Aero is on