Jump to content

Get size of GUI


erha
 Share

Recommended Posts

I try to get the size of a gui in the function "WM_SIZE". I want to resize some controls depending on the gui size.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $hGUI = GUICreate("Example", 200, 200, -1, -1, BitOR($WS_SIZEBOX, $WS_SYSMENU, $WS_CAPTION, $WS_MINIMIZEBOX, $WS_MAXIMIZEBOX))

Global  $DummyMenuEntry = GUICtrlCreateMenu("DummyMenuEntry", -1, $hGUI)

GUIRegisterMsg($WM_SIZE, 'WM_SIZE')
GUISetState(@SW_SHOW, $hGUI)

While True
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd
GUIDelete($hGUI)

Func WM_SIZE($hWnd, $iMsg, $iWParam, $iLParam)
    #forceref $hWnd, $iMsg, $iWParam
    Local $iGUIWidth1 = BitAND($iLParam, 0xFFFF)
    Local $iGUIHeight1 = BitShift($iLParam, 16)
    local $iGUIWidth2 = WinGetPos($hGUI)[2]
    local $iGUIHeight2 = WinGetPos($hGUI)[3]
    ConsoleWrite("GUIWidth : " & $iGUIWidth1 & "  " & $iGUIWidth2 & "  " & $iGUIWidth1 - $iGUIWidth2  & @CRLF )
    ConsoleWrite("GUIHeight: " & $iGUIHeight1 & "  " & $iGUIHeight2 & "  " & $iGUIHeight1 - $iGUIHeight2  & @CRLF  & @CRLF )
    Return ($GUI_RUNDEFMSG)
EndFunc   ;==>WM_SIZE

First I have used "local $iGUIWidth2 = WinGetPos($hGUI)[2]" - but did not work es expected. After some research I found "Local $iGUIWidth1 = BitAND($iLParam, 0xFFFF)" ( s. URL ). Running the scripts produces the following output

GUIWidth : 200  216  -16
GUIHeight: 180  239  -59

My assumption - the diffference in height is caused by the gui title, menu, .... In a second run I deleted the line
Global  $DummyMenuEntry = GUICtrlCreateMenu("DummyMenuEntry", -1, $hGUI)

GUIWidth : 200  216  -16
GUIHeight: 200  239  -39

The gui height is now 20 units less which is the height of the now missing menu. The height difference is caused by the gui title  ( my assumption ).

In case my assumption is correct - why is there a difference of 16 units in the gui width ? There is nothing ( title, menu, taskbar, ... ) which increases the gui width.

Using "$iGUIWidth = BitAND($iLParam, 0xFFFF)" works fine for me - but I like to know why my first approach using "WinGetPos" did not work as assumed.

Edited by erha
typo correction
Link to comment
Share on other sites

WM_SIZE gives the size of the client area of the window, while WinGetPos returns the size of the window itself. Client area excludes title bar, menu, sides and bottom borders.

Link to comment
Share on other sites

@NineThank you for the answer.

I tested with
    local $iGUIWidth3 = WinGetClientSize ( $hGUI )[0]
    local $iGUIHeight3 = WinGetClientSize ( $hGUI )[1]
WinGetClientSize returns the same values as BitAND($iLParam, 0xFFFF) resp. BitShift($iLParam, 16) .

WinGetClientSize is the solution for my needs.

But I still do not understand why WinGetClientSize  and WinGetPos do not return the same width.  You mention "Client area excludes title bar, menu, sides and bottom borders". The width shall not be affected by title bar, menu and bottom borders. What are "sides" ?  The gui left/right borders ?

Edited by erha
Link to comment
Share on other sites

there is something more . look and at  https://www.autoitscript.com/forum/topic/209363-_systemmetricsparameterstoarray/ 

; https://www.autoitscript.com/forum/topic/209707-get-size-of-gui/?do=findComment&comment=1513637

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $hGUI = GUICreate("Example", 200, 200, -1, -1, BitOR($WS_SIZEBOX, $WS_SYSMENU, $WS_CAPTION, $WS_MINIMIZEBOX, $WS_MAXIMIZEBOX))

Global  $DummyMenuEntry = GUICtrlCreateMenu("DummyMenuEntry", -1, $hGUI)

GUIRegisterMsg($WM_SIZE, 'WM_SIZE')
GUISetState(@SW_SHOW, $hGUI)

While True
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd
GUIDelete($hGUI)

Func WM_SIZE($hWnd, $iMsg, $iWParam, $iLParam)
    #forceref $hWnd, $iMsg, $iWParam
    Local $iGUIWidth1 = BitAND($iLParam, 0xFFFF)
    Local $iGUIHeight1 = BitShift($iLParam, 16)
    local $iGUIWidth2 = _WinGetPosAccuracy($hGUI)[2]
    local $iGUIHeight2 = _WinGetPosAccuracy($hGUI)[3]
    ConsoleWrite("GUIWidth : " & $iGUIWidth1 & "  " & $iGUIWidth2 & "  " & $iGUIWidth1 - $iGUIWidth2  & @CRLF )
    ConsoleWrite("GUIHeight: " & $iGUIHeight1 & "  " & $iGUIHeight2 & "  " & $iGUIHeight1 - $iGUIHeight2  & @CRLF  & @CRLF )
    Return ($GUI_RUNDEFMSG)
EndFunc   ;==>WM_SIZE

;~ WinGetPos
;~  GUIWidth : 200  216  -16
;~  GUIHeight: 180  239  -59

;~ _WinGetPosAccuracy
;~ GUIWidth : 200  202  -2
;~ GUIHeight: 180  232  -52


Func _WinGetPosAccuracy($hWnd)
    Local $HWPos[4] ; [0]=X  [1]=Y  [2]=Width  [3]=Height
    If WinExists($hWnd) Then
        $HWPos[0] = _WindowDWMX($hWnd)
        $HWPos[1] = _WindowDWMY($hWnd)
        $HWPos[2] = _WindowDWMWidth($hWnd)
        $HWPos[3] = _WindowDWMHeight($hWnd)
    Else
        Return SetError(1, 0, "")
    EndIf
    Return $HWPos

EndFunc   ;==>_WinGetPosAccuracy

#Region === a short extract from Pal, Peter's AutoIt Library, version 1.25 UDF ===

; https://www.autoitscript.com/forum/topic/201518-pal-peters-autoit-functions-library/

; #FUNCTION# ====================================================================================================================
; Name...........: _WindowDWMWidth
; Description....: Returns window width as registered by the Desktop Window Manager (DWM)
; Syntax.........: _WindowDWMWidth($hGUI)
; Parameters.....: $hGUI                    - Window GUI handle
; Return values..: Desktop Window Manager window width
; Author.........: Peter Verbeek
; Modified.......:
; ===============================================================================================================================
Func _WindowDWMWidth($hGUI)
    ; Try finding width by the Desktop Window Manager, Windows Vista and higher
    Local $tagRect = "struct;long Left;long Top;long Right;long Bottom;endstruct", $tRect = DllStructCreate($tagRect)
    DllCall("Dwmapi.dll", "long", "DwmGetWindowAttribute", "hwnd", WinGetHandle($hGUI), "dword", 9, "ptr", DllStructGetPtr($tRect), "dword", DllStructGetSize($tRect))
    If @error = 0 And DllStructGetData($tRect, 3) - DllStructGetData($tRect, 1) > 0 And DllStructGetData($tRect, 4) - DllStructGetData($tRect, 2) > 0 Then Return DllStructGetData($tRect, 3) - DllStructGetData($tRect, 1)
    ; Alternatively return window width by AutoIt
    Local $aWindow = WinGetPos($hGUI)
    If @error <> 0 Or $aWindow[0] = -32000 Then Return 0                ; correcting for a minimized window
    Return $aWindow[2]
EndFunc   ;==>_WindowDWMWidth
; #FUNCTION# ====================================================================================================================
; Name...........: _WindowDWMHeight
; Description....: Returns window height as registered by the Desktop Window Manager (DWM)
; Syntax.........: _WindowDWMHeight($hGUI)
; Parameters.....: $hGUI                    - Window GUI handle
; Return values..: Desktop Window Manager window height
; Author.........: Peter Verbeek
; Modified.......:
; ===============================================================================================================================
Func _WindowDWMHeight($hGUI)
    ; Try finding width by the Desktop Window Manager, Windows Vista and higher
    Local $tagRect = "struct;long Left;long Top;long Right;long Bottom;endstruct", $tRect = DllStructCreate($tagRect)
    DllCall("Dwmapi.dll", "long", "DwmGetWindowAttribute", "hwnd", WinGetHandle($hGUI), "dword", 9, "ptr", DllStructGetPtr($tRect), "dword", DllStructGetSize($tRect))
    If @error = 0 And DllStructGetData($tRect, 3) - DllStructGetData($tRect, 1) > 0 And DllStructGetData($tRect, 4) - DllStructGetData($tRect, 2) > 0 Then Return DllStructGetData($tRect, 4) - DllStructGetData($tRect, 2)
    ; Alternatively return window height by AutoIt
    Local $aWindow = WinGetPos($hGUI)
    If @error <> 0 Or $aWindow[0] = -32000 Then Return 0                ; correcting for a minimized window
    Return $aWindow[3]
EndFunc   ;==>_WindowDWMHeight
; #FUNCTION# ====================================================================================================================
; Name...........: _WindowDWMX
; Description....: Returns window x coordinate as registered by the Desktop Window Manager (DWM)
; Syntax.........: _WindowDWMWidth($hGUI)
; Parameters.....: $hGUI                    - Window GUI handle
; Return values..: Desktop Window Manager window x coordinate
; Author.........: Peter Verbeek
; Modified.......:
; ===============================================================================================================================
Func _WindowDWMX($hGUI)
    ; Try finding x coordinate by the Desktop Window Manager, Windows Vista and higher
    Local $tagRect = "struct;long Left;long Top;long Right;long Bottom;endstruct", $tRect = DllStructCreate($tagRect)
    DllCall("Dwmapi.dll", "long", "DwmGetWindowAttribute", "hwnd", WinGetHandle($hGUI), "dword", 9, "ptr", DllStructGetPtr($tRect), "dword", DllStructGetSize($tRect))
    If @error = 0 And DllStructGetData($tRect, 3) - DllStructGetData($tRect, 1) > 0 And DllStructGetData($tRect, 4) - DllStructGetData($tRect, 2) > 0 Then Return DllStructGetData($tRect, 1)
    ; Alternatively return window width by AutoIt
    Local $aWindow = WinGetPos($hGUI)
    If @error <> 0 Or $aWindow[0] = -32000 Then Return 0                ; correcting for a minimized window
    Return $aWindow[0]
EndFunc   ;==>_WindowDWMX
; #FUNCTION# ====================================================================================================================
; Name...........: _WindowDWMY
; Description....: Returns window y coordinate as registered by the Desktop Window Manager (DWM)
; Syntax.........: _WindowDWMWidth($hGUI)
; Parameters.....: $hGUI                    - Window GUI handle
; Return values..: Desktop Window Manager window y coordinate
; Author.........: Peter Verbeek
; Modified.......:
; ===============================================================================================================================
Func _WindowDWMY($hGUI)
    ; Try finding y coordinate by the Desktop Window Manager, Windows Vista and higher
    Local $tagRect = "struct;long Left;long Top;long Right;long Bottom;endstruct", $tRect = DllStructCreate($tagRect)
    DllCall("Dwmapi.dll", "long", "DwmGetWindowAttribute", "hwnd", WinGetHandle($hGUI), "dword", 9, "ptr", DllStructGetPtr($tRect), "dword", DllStructGetSize($tRect))
    If @error = 0 And DllStructGetData($tRect, 3) - DllStructGetData($tRect, 1) > 0 And DllStructGetData($tRect, 4) - DllStructGetData($tRect, 2) > 0 Then Return DllStructGetData($tRect, 2)
    ; Alternatively return window width by AutoIt
    Local $aWindow = WinGetPos($hGUI)
    If @error <> 0 Or $aWindow[0] = -32000 Then Return 0                ; correcting for a minimized window
    Return $aWindow[1]
EndFunc   ;==>_WindowDWMY
#EndRegion === a short extract from Pal, Peter's AutoIt Library, version 1.25 UDF ===

 

I know that I know nothing

Link to comment
Share on other sites

@ioa747I had a look at the program you provided. I got strange results.

I had a look at some functions, e.g. "_WindowDWMWidth($hGUI)". The author proposes two soluions, solution 2
    ; Alternatively return window width by AutoIt
    Local $aWindow = WinGetPos($hGUI)
    If @error <> 0 Or $aWindow[0] = -32000 Then Return 0                ; correcting for a minimized window
    Return $aWindow[2]

( this is what I have used )

The other way proposed by the author
    ; Try finding width by the Desktop Window Manager, Windows Vista and higher
    Local $tagRect = "struct;long Left;long Top;long Right;long Bottom;endstruct", $tRect = DllStructCreate($tagRect)
   ...

( this gives strange results on my system )

Nine gave the answer I needed - WinGetPos returns window size including title, menu, ....
WinGetClientSize returns the client area.
To compute the size/position of controls after window resizing you need to use WinGetClientSize.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...