Jump to content

WinGetPos Incorrectly Returning Sizes


PeteF
 Share

Recommended Posts

Full script w/details in comments below, just in case you need to run it to see the problem.  Basically WinGetPos is incorrectly returning the W & H sizes, while at the same time correctly returning the positions. To compound the problem, I ran the same script on 2 different PCs and it gets2 different results as follows:

Win7 Pro PC.. GUICreate used to set W:300, H:100.       WinGetPos returns W:307, H:127  (PC screen res: 1366x768)

WinXP PC..... GUICreate used to set W:300, H:100.       WinGetPos returns W:308, H:134   (PC screen res: 1152x864)

The bigger problem occurs when you consider I want to save the Window sizes and recreate the window at a later date. but due to this problem, each time I ran the program the Window would get larger and larger.    --pete

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

; SCRIPT DESC.: Simple script here. Create & display one Window of a specific
; size & position. This Window is always ON TOP of other windows.
; User may resize the window to cover up unwanted content (as in use with a Web-Browser).
; Upon user closing the Window, DEBUG msgbox in script returns window size & position
; to be used in further development of this script.
;
; PROBLEM: If user does not resize the Window and simply clicks "X" to close the window,
; the DEBUG msgbox is reporting the wrong SIZES. It's actually the AutoIt WinGetPos
; function that is returning correct positions, but incorrectly returning the sizes.
; It should be returning 300 (W), 100 (H), but it's incorrectly  returning 307 (W), 127 (H)
;
_MAIN()

Func _MAIN()

    ;Local $sFilePath = "..\GUI\logo4___.gif"
    Local $l_SizeWidth_1, $l_SizeHeight_1, $l_PosLeft_1, $l_PosTop_1

    ; Defaults for Window 1  Size and Position
    $l_SizeWidth_1 = 300
    $l_SizeHeight_1 = 100
    $l_PosLeft_1 = 50
    $l_PosTop_1 = 50


    ; Create re-sizable GUI window/form that is blank with a black background and stays on TOP over over windows.
    Local $hGUI = GUICreate("COVER UNWANTED CONTENT", $l_SizeWidth_1, $l_SizeHeight_1, $l_PosLeft_1, $l_PosTop_1, _
            $WS_OVERLAPPEDWINDOW, $WS_EX_TOPMOST)

    ; Black Background
    GUISetBkColor(0x000000)

    ;GUICtrlCreatePic("..\GUI\msoobe.jpg", 0, 0, 400, 100)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)


    ; Create a picture control with a transparent image.
    ;GUICtrlCreatePic($sFilePath, 0, 0, 169, 68)

    ; Display the Window
    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()

            ; CASE IF USER X's out, CLOSE THE WINDOWS & QUIT
            Case $GUI_EVENT_CLOSE
                ExitLoop

                ; CASE Open a second window


        EndSwitch
    WEnd

    ; Obtain the window(s) Size & Positions
    _WindowPositionAndSize($hGUI)


    ; Save the window(s) size & positions
    ; TBD


    ; Delete the previous GUIs and all controls.
    GUIDelete($hGUI)


EndFunc   ;==>_MAIN


Func _WindowPositionAndSize($WindowID)

    ; Retrieve the position as well as height and width of the window.
    Local $aPos = WinGetPos($WindowID)

    ; DEBUG msgbox
    ; Display the array values returned by WinGetPos.
    MsgBox(0, "", "X-Pos: " & $aPos[0] & @CRLF & _
            "Y-Pos: " & $aPos[1] & @CRLF & _
            "Width: " & $aPos[2]  & @CRLF & _
            "Height: " & $aPos[3] )

            ; DEBUG notes
            ; Width 300 off by a factor of.. 0.9740261
            ; Height 100 off by a factor of.. 0.7874017
            ; I was hoping to add in some correction factors, but thse factors
            ; change dramatically at different Width & Height settings. Need a better fix!

EndFunc   ;==>_WindowPositioAndSize

 

 

Edited by PeteF
Link to comment
Share on other sites

I confirm, i get 306x129.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

From AutoIt help file>GUICreate function>Remarks:- "The size specified is the size of the client area of the window. The border and title bar will make the window slightly larger than specified. Using menu controls will also change the windows height."

WinGetPos() returns the outside dimensions of a window.
WinGetClientSize() will return the window's client area, as specified in the GUICreate() function.

Link to comment
Share on other sites

Ok Malkey, that's it!  Thank you!  So, I need to get the POSITIONS using WinGetPos () and the SIZES using WinGetClientSize (). I read the part you quoted from the Help file, but I didn't quite understand what it meant. Your explanation cleared up the confusion.

In the spirit of helping others, below is the full script all fixed up, and I took it one step further to return the POS & SIZE values back to the _MAIN function where the msgbox displays the results.   Very cool, Thanks again! 

One last thing. I'm just a few weeks into using AutoIt and something puzzles me about Functions related to Return Values of the array type.

HELP for WinGetClientSize states..

Return Value

  a 2-element array containing the following information:
    $aArray[0] = Width of window's client area
    $aArray[1] = Height of window's client area

 

My function  _WindowPositionAndSize  utilizes 2 AutoIt functions (WinGetPos  and WinGetClientSize).  So how does AutoIT know to assign the $aArray[0]  to the array variables I specify in my function?  This is especially puzzling when you consider that WinGetPos has a 4 element array and WinGetClientSize has a 2 element array and I used 2 separate arrays in my function and AutoIt assigned the values perfectly.  Amazing, but I don't quite understand how it works or what the rules are.

Can you explain how it works, or is this array thing explained in more detail anywhere in the HELP system?

--pete--

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

; SCRIPT DESC.: Simple script here. Create & display & 1 Window of a specific
; size & position. This Window is always ON TOP of other windows.
; User may resize the window to cover up unwanted content (as in use with a Web-Browser).
; Upon user closing the Window, DEBUG msgbox in script returns window size & position
; to be used in further development of this script.
;
;
_MAIN()

Func _MAIN()

    ;Local $sFilePath = "..\GUI\logo4___.gif"
    Local $l_SizeWidth_1, $l_SizeHeight_1, $l_PosLeft_1, $l_PosTop_1 ;Set values
    Local $l_SizeWidth, $l_SizeHeight, $l_PosLeft, $l_PosTop ;Returned values

    ; Defaults for Window 1  Size and Position
    $l_SizeWidth_1 = 300
    $l_SizeHeight_1 = 100
    $l_PosLeft_1 = 50
    $l_PosTop_1 = 50


    ; Create re-sizable GUI window/form that is blank with a black background and stays on TOP over over windows.
    Local $hGUI = GUICreate("COVER UNWANTED CONTENT", $l_SizeWidth_1, $l_SizeHeight_1, $l_PosLeft_1, $l_PosTop_1, _
            $WS_OVERLAPPEDWINDOW, $WS_EX_TOPMOST)

    ; Black Background
    GUISetBkColor(0x000000)

    ;GUICtrlCreatePic("..\GUI\msoobe.jpg", 0, 0, 400, 100)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)


    ; Create a picture control with a transparent image.
    ;GUICtrlCreatePic($sFilePath, 0, 0, 169, 68)

    ; Display the Window
    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()

            ; CASE IF USER X's out, CLOSE THE WINDOWS & QUIT
            Case $GUI_EVENT_CLOSE
                ExitLoop

                ; CASE Open a second window


        EndSwitch
    WEnd

    ; Obtain the window(s) Size & Positions
    _WindowPositionAndSize($hGUI, $l_SizeWidth, $l_SizeHeight, $l_PosLeft, $l_PosTop)

    ;Debug
    MsgBox(0, "", "Pos-Left: " & $l_PosLeft & @CRLF & _
            "Pos-Top: " & $l_PosTop & @CRLF & _
            "Width: " & $l_SizeWidth & @CRLF & _
            "Height: " & $l_SizeHeight)


    ; Save the window(s) size & positions
    ; TBD


    ; Delete the previous GUIs and all controls.
    GUIDelete($hGUI)


EndFunc   ;==>_MAIN



Func _WindowPositionAndSize($WindowID, ByRef $SizeWidth, ByRef $SizeHeight, ByRef $PosLeft, ByRef $PosTop)

    ; Retrieve the positions
    Local $aPos = WinGetPos($WindowID)

    ; Retrieve the sizes
    Local $aSize = WinGetClientSize($WindowID)

    $PosLeft = $aPos[0]
    $PosTop = $aPos[1]
    $SizeWidth = $aSize[0]
    $SizeHeight = $aSize[1]

EndFunc   ;==>_WindowPositionAndSize
Quote

 

 

Link to comment
Share on other sites

Just as the author/s of the functions WinGetPos and WinGetClientSize  had these functions return arrays, so to your _WindowPositionAndSize function can be rewritten to return an array.

Func _WindowPositionAndSize($WindowID)

    ; Retrieve the positions
    Local $aPos = WinGetPos($WindowID)

    ; Retrieve the sizes
    Local $aSize = WinGetClientSize($WindowID)

    Local $aRetArray[4] ; One array returned can have 4 values.  Global variables not required.

    $aRetArray[0] = $aPos[0]
    $aRetArray[1] = $aPos[1]
    $aRetArray[2] = $aSize[0]
    $aRetArray[3] = $aSize[1]

    Return $aRetArray
EndFunc   ;==>_WindowPositionAndSize

 

Edited by Malkey
Grammar correction
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

×
×
  • Create New...