Jump to content
Sign in to follow this  
jfcby

Position Window Inside Desktop

Recommended Posts

jfcby

Hi,

In my full script I've set it up to remember the program window position and saves it to an ini file so that when the program restarts it will be located in the user chosen position. But sometimes the window will get positioned outside of the desktop where it cannot be seen.

Below is a test script where I compare the WinGetPos to the GetDesktopArea but it does not work properly. If the window is outside of the desktop at the bottom left or the top right then the script does recognize that.

How can the script below be modified to keep the specified program window inside the desktop work area?

I've search through this forum and did not find a solution.

#include <WinAPI.au3>
 
Global $DesktopArea = _GetDesktopArea()
$winPos = WinGetPos("AutoIt Help") 

    If $winPos[0] < $DesktopArea[0] or $winPos[1] < $DesktopArea[1] or _
        $DesktopArea[2] > $winPos[1] or $DesktopArea[3] > $winPos[1] Then        
        
        MsgBox(0, "Debug Test #1", "DesktopArea: " & $DesktopArea[0] & " " & $DesktopArea[1] & " " & $DesktopArea[2] & " " & $DesktopArea[3] & @CRLF & _
    "winPos: " & $winPos[0] & " " & $winPos[1] & " " & $winPos[2] & " " & $winPos[3])
    
    Else
        MsgBox(0, "Debug Test #2", "DesktopArea: " & $DesktopArea[0] & " " & $DesktopArea[1] & " " & $DesktopArea[2] & " " & $DesktopArea[3] & @CRLF & _
    "winPos: " & $winPos[0] & " " & $winPos[1] & " " & $winPos[2] & " " & $winPos[3])
    EndIf

; Get the working visible area of the desktop, this doesn't include the area covered by the taskbar.
Func _GetDesktopArea()
    Local Const $SPI_GETWORKAREA = 48
    Local $tWorkArea = DllStructCreate($tagRECT)
    _WinAPI_SystemParametersInfo($SPI_GETWORKAREA, 0, DllStructGetPtr($tWorkArea))    
    Local $aReturn[4] = [DllStructGetData($tWorkArea, "Left"), DllStructGetData($tWorkArea, "Top"), _
            DllStructGetData($tWorkArea, "Right") - DllStructGetData($tWorkArea, "Left"), DllStructGetData($tWorkArea, "Bottom") - DllStructGetData($tWorkArea, "Top")]    
    Return $aReturn
EndFunc   ;==>_GetDesktopArea

Thanks for your help.

Edited by jfcby

Determined -- Devoted -- Delivered Make your mind up -- to seriously apply yourself -- accomplishing the desired results. **** A soft answer turneth away wrath: but grievous words stir up anger. Proverbs 15:1 KJB ****

Share this post


Link to post
Share on other sites
PhoenixXL

You might even had to add to check for Minimized, Hidden and Maximized windows explicitly otherwise

For your case the following would satisfy the condition

#include <WinAPI.au3>
#include <Array.au3>

If BitAND(WinGetState("AutoIt Help"), 2) Then

    $DesktopArea = _GetDesktopArea()
    $winPos = WinGetPos("AutoIt Help")

    If $winPos[0] > $DesktopArea[0] And $winPos[1] > $DesktopArea[1] And _
            $DesktopArea[2] > $winPos[0] + $winPos[2] And $DesktopArea[3] > $winPos[1] + $winPos[3] Then

        MsgBox(0, "Debug Test", "Window Inside Desktop Area")

    Else
        MsgBox(0, "Debug Test", "Window NOT Inside Desktop Area")
    EndIf

    _ArrayDisplay($winPos, "WinPos") ;these are X, Y position and Width and Height
    _ArrayDisplay($DesktopArea, "DesktopArea") ;whereas these are all positions(coordinates)

Else
    MsgBox(16, "Err", "The Win is not visible - Minimized or Hidden")

EndIf
; Get the working visible area of the desktop, this doesn't include the area covered by the taskbar.
Func _GetDesktopArea()
    Local Const $SPI_GETWORKAREA = 48
    Local $tWorkArea = DllStructCreate($tagRECT)
    _WinAPI_SystemParametersInfo($SPI_GETWORKAREA, 0, DllStructGetPtr($tWorkArea))
    Local $aReturn[4] = [DllStructGetData($tWorkArea, "Left"), DllStructGetData($tWorkArea, "Top"), _
            DllStructGetData($tWorkArea, "Right") - DllStructGetData($tWorkArea, "Left"), DllStructGetData($tWorkArea, "Bottom") - DllStructGetData($tWorkArea, "Top")]
    Return $aReturn
EndFunc   ;==>_GetDesktopArea

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
jfcby

PhoenixXL, Your solution worked great. Thanks for your help.


Determined -- Devoted -- Delivered Make your mind up -- to seriously apply yourself -- accomplishing the desired results. **** A soft answer turneth away wrath: but grievous words stir up anger. Proverbs 15:1 KJB ****

Share this post


Link to post
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
Sign in to follow this  

×