Sign in to follow this  
Followers 0
BennyBB

Border width and height of GUI

8 posts in this topic

#1 ·  Posted (edited)

Hi all,

I'm working on a new project and I have to find a control in a gui but I need the coordinates relative to the whole desktop ... the problem is that

ControlGetPos ( "title", "text", controlID )

returns window relative coordinates. So I use

WinGetPos ( "title" [, "text"] )

to get the Gui coordinates Then I sum up the two returned values for the x variable :

$guipos = WingetPos($gui)
$controlpos = ControlGetPos($gui,"",$controlid)
$controlpos_x = $guipos[0]+$controlpos[0]

this part works fine...

but there comes my problem there is 1 part of the window which is not mentioned in both functions:

The GUI border :)

I need to know how high/width this is:

Posted Image

Are there any funcs to get this values or can I may guess it from the Styles and exStyles the GUI uses?

Sincerely BennyBB

Edited by BennyBB

Share this post


Link to post
Share on other sites



It doesn't matter what are the window frame dimensions, the client origin is still at (0, 0):

#include <WinAPI.au3>

Run("calc.exe")
WinWaitActive("[CLASS:SciCalc]")
$hWnd = WinGetHandle("[CLASS:SciCalc]")
$hCtrl = ControlGetHandle($hWnd, 0, "Button58")

$aPos = ControlGetPos($hWnd, 0, $hCtrl)
$tPt = _MakePoint($aPos[0], $aPos[1])
$tPt = _WinAPI_ClientToScreen($hWnd, $tPt)
$iX = DllStructGetData($tPt, "X")
$iY = DllStructGetData($tPt, "Y")

ConsoleWrite($iX & @TAB & $iY & @CRLF)
MouseMove($iX, $iY, 20)
Sleep(1000)
MouseMove(0, 0, 20)

; Same as
$aPos = WinGetPos($hCtrl)
ConsoleWrite($aPos[0] & @TAB & $aPos[1] & @CRLF)
MouseMove($aPos[0], $aPos[1], 20)



Func _MakePoint($iX, $iY)
    Local $tPt = DllStructCreate($tagPOINT)
    
    DllStructSetData($tPt, "X", $iX)
    DllStructSetData($tPt, "Y", $iY)
    Return $tPt
EndFunc

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

WinGetClientSize can be used to get the width of the border

$cSize = WinGetClientSize($window)
$wSize = WinGetPos($window)
$borderSize = ($wSize[2] - $cSize[0]) / 2
$captionSize = ($wSize[3] - $cSize[1]) - $borderSize

though you can use _WinAPI_ClientToScreen too

#Include <WinAPI.au3>
_WinAPI_ClientToScreen($hWnd, ByRef $tPoint)
Edited by skyboy

Share this post


Link to post
Share on other sites

Hi all,

I'm working on a new project and I have to find a control in a gui but I need the coordinates relative to the whole desktop ...

Why do it the hard way when you already have a function that returns screen coordinates? :)

WinGetPos(GUICtrlGetHandle($controlid))

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Use _WinAPI_GetSystemMetrics() to find the border height and width.

For example this code would get the border width:

#include <WinAPI.au3> 
$BorderWidth = _WinAPI_GetSystemMetrics(8)

The number 8 can be replaced with various values, all of which you can find at the MSDN page...

Edited by Vadersapien

Try Pacfox, my Firefox theme.Try Power Eject, my windows gadget that allows you to eject most drives.Using AutoIt 3.3.4.0, Windows 7 Premium, Intel Core 2 Quad CPU @ 2.66ghz, 4gb RAM, Nvidia GeForce 9500GT Graphics Card & Samsung 22" Monitor.

Share this post


Link to post
Share on other sites

It doesn't matter what are the window frame dimensions, the client origin is still at (0, 0):

#include <WinAPI.au3>

Run("calc.exe")
WinWaitActive("[CLASS:SciCalc]")
$hWnd = WinGetHandle("[CLASS:SciCalc]")
$hCtrl = ControlGetHandle($hWnd, 0, "Button58")

$aPos = ControlGetPos($hWnd, 0, $hCtrl)
$tPt = _MakePoint($aPos[0], $aPos[1])
$tPt = _WinAPI_ClientToScreen($hWnd, $tPt)
$iX = DllStructGetData($tPt, "X")
$iY = DllStructGetData($tPt, "Y")

ConsoleWrite($iX & @TAB & $iY & @CRLF)
MouseMove($iX, $iY, 20)
Sleep(1000)
MouseMove(0, 0, 20)

; Same as
$aPos = WinGetPos($hCtrl)
ConsoleWrite($aPos[0] & @TAB & $aPos[1] & @CRLF)
MouseMove($aPos[0], $aPos[1], 20)



Func _MakePoint($iX, $iY)
    Local $tPt = DllStructCreate($tagPOINT)
    
    DllStructSetData($tPt, "X", $iX)
    DllStructSetData($tPt, "Y", $iY)
    Return $tPt
EndFunc

Why do it the hard way when you already have a function that returns screen coordinates? ;)

WinGetPos(GUICtrlGetHandle($controlid))

That was what I was looking for :evil:

Thank you very much

But also Thanks for the other replies, but I'm not well versed in this WinApi stuff :evil:

BennyBB

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

On 7.12.2009 at 11:57 PM, Vadersapien said:

...this code would get the border width:

 

#include <WinAPI.au3> 
$BorderWidth = _WinAPI_GetSystemMetrics(8)

The number 8 can be replaced with various values, all of which you can find at the MSDN page...

Cool  that what I was looking for. :D

But well avoid using 'naked literals' like this '8'. Or when ya do be aware that this is a bad coding style.
The thing is it's hard to read. Put at least a comment behide what this '8' means.
Using const's (or enum ) you define once - or import from somewhere is even better:

#include <WinAPI.au3>
#include <WindowsConstants.au3>

$BorderWidth = _WinAPI_GetSystemMetrics( $SM_CYDLGFRAME ) ; 0x8

Well while most common ide's will show you the value that's behind some const when you just move the mouse over it.
SciTe is not that advanced yet so maybe put the real value just behind as comment ; 0x8

 

So and in the end here is some Script that will call 

Call GetSystemMetrics with all SM_* value that are defined in WindowsConstants.au3 and show the return value.

;
;  - = GetSystemMetrics demo = -
;
;    Runs GetSystemMetrics with all possible values that are defined in Autoit
;
    #include <WinAPI.au3>

    ; Get all SM_*  const's listed in WindowsConstants.au3
    $SM_AllKnowValues = StringRegExp( _
        _AutoIT_IncludeFile_Load("WindowsConstants.au3"), _
        "\$(SM_\w+)\s*=\s*(0x)?(\d+)", _
        $STR_REGEXPARRAYGLOBALFULLMATCH  _
    )

    $HelpUrl =  "https://msdn.microsoft.com/en-us/library/ms724385(VS.85).aspx"


#include<array.au3>

;~ GetNShowData ()
AdlibRegister( GetNShowData )
Sleep(500)

Func GetNShowData()


    local $OutputLines[0][4];=[["-", "-", "-","-"]]

    For $i = 0 To UBound($SM_AllKnowValues) - 1
        Local $aMatch = $SM_AllKnowValues[$i]

        $SM_Name = $aMatch[1]
        $IsHex   = $aMatch[2]
        $SM_Value= $aMatch[3]

        $SM_Value=  $IsHex? dec($SM_Value): $SM_Value

        $ret= _WinAPI_GetSystemMetrics($SM_Value)
        $OutputLine =   StringFormat( _
                "%2i -> %-23s => %5i [ 0x%.3x ]" & _
                "   -  %s#%s\n" , _
                $SM_Value, $SM_Name, $ret, $ret, _
                $HelpUrl, $SM_Name )
        ConsoleWrite ($OutputLine)

        local $ArrLine[][]= [[ _
                   $SM_Value, $SM_Name, $ret, StringFormat( "0x%.3x", $ret) ]]
        $Header = "Value|SM_Name|RetVal|RetVal"

        _ArrayAdd( $OutputLines, $ArrLine )

    Next

    Const $ArrayDisplay_ColumnNoRow = 0x40
    Const $ArrayDisplay_ColumnTextAlignRight = 0x2

    _ArrayDisplay($OutputLines,"GetSystemMetrics Return Values","", _
        $ArrayDisplay_ColumnNoRow + $ArrayDisplay_ColumnTextAlignRight,"", _
        $Header, -1 , 0 , _UserFunc )


EndFunc



Func _UserFunc($aArray_2D, $aSelected)

    $itemIndex = $aSelected[1]
    $SM_Name =  $aArray_2D [$itemIndex] [1]

    $url =  StringFormat("%s#%s", $HelpUrl, $SM_Name)
    ShellExecute($url)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $url = ' & $url & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console


EndFunc   ;==>_UserFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _AutoIT_IncludeFile_Load
; Description ...: Loads a file frim the AutoIT Include
; Syntax ........: _AutoIT_IncludeFile_Load($IncludeName)
; Parameters ....: $IncludeName         - *.au3 Include Filename
; Return values .: Success: FileContent (@extended Bytes Read)
;                  Failure: sets @error = 1
; Author ........: YourRobinson1Name
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
    Func _AutoIT_IncludeFile_Load($IncludeName)

        $Path_Au3File = @AutoItExe & "\..\Include\" & $IncludeName
        $Ret = _myFileRead( $Path_Au3File )
        Return SetError(@error, @extended, $Ret )
    EndFunc


; #FUNCTION# ====================================================================================================================
; Name ..........: _myFileRead
; Description ...: a wapper for FileRead(...)
;                  incase of an error an inputbox apperars, allowing the user to correct the path
; Syntax ........: _myFileRead($TheFile)
; Parameters ....: $TheFile             - The file to read.
; Return values .: Success: FileContent
;                  Failure: sets @error = 1
; Author ........: Robinson1
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
    Func _myFileRead( $TheFile, $Count = -1)

        while True
            $Ret = FileRead( $TheFile, $Count)

            if @extended > 0 Then
                Return SetError( @error, @extended, $Ret)
            Else
                $TheFile = InputBox( "Error: FileRead returned " & @error,  _
                    "Please edit the path:", $TheFile)
                if $TheFile = "" Then _
                    Return SetError (1)
            EndIf
        WEnd
    EndFunc

The Output will look like this:

0 -> SM_CXSCREEN             =>  1680 [ 0x690 ]  
 1 -> SM_CYSCREEN             =>  1050 [ 0x41a ]  
 2 -> SM_CXVSCROLL            =>    20 [ 0x014 ]  
 3 -> SM_CYHSCROLL            =>    20 [ 0x014 ]  
 4 -> SM_CYCAPTION            =>    25 [ 0x019 ]  
 5 -> SM_CXBORDER             =>     1 [ 0x001 ]  
 6 -> SM_CYBORDER             =>     1 [ 0x001 ]  
 7 -> SM_CXDLGFRAME           =>     3 [ 0x003 ]  
 8 -> SM_CYDLGFRAME           =>     3 [ 0x003 ]  
 9 -> SM_CYVTHUMB             =>    20 [ 0x014 ]  
10 -> SM_CXHTHUMB             =>    20 [ 0x014 ]  
11 -> SM_CXICON               =>    40 [ 0x028 ]  
12 -> SM_CYICON               =>    40 [ 0x028 ]  
13 -> SM_CXCURSOR             =>    32 [ 0x020 ]  
14 -> SM_CYCURSOR             =>    32 [ 0x020 ]  
15 -> SM_CYMENU               =>    25 [ 0x019 ]  
16 -> SM_CXFULLSCREEN         =>  1449 [ 0x5a9 ]  
17 -> SM_CYFULLSCREEN         =>  1025 [ 0x401 ]  
18 -> SM_CYKANJIWINDOW        =>     0 [ 0x000 ]  
19 -> SM_MOUSEPRESENT         =>     1 [ 0x001 ]  
20 -> SM_CYVSCROLL            =>    20 [ 0x014 ]  
21 -> SM_CXHSCROLL            =>    20 [ 0x014 ]  
22 -> SM_DEBUG                =>     0 [ 0x000 ]  
23 -> SM_SWAPBUTTON           =>     0 [ 0x000 ]  
24 -> SM_RESERVED1            =>     0 [ 0x000 ]  
25 -> SM_RESERVED2            =>     0 [ 0x000 ]  
26 -> SM_RESERVED3            =>     0 [ 0x000 ]  
27 -> SM_RESERVED4            =>     0 [ 0x000 ]  
28 -> SM_CXMIN                =>   145 [ 0x091 ]  
29 -> SM_CYMIN                =>    37 [ 0x025 ]  
30 -> SM_CXSIZE               =>    23 [ 0x017 ]  
31 -> SM_CYSIZE               =>    24 [ 0x018 ]  
32 -> SM_CXFRAME              =>     6 [ 0x006 ]  
33 -> SM_CYFRAME              =>     6 [ 0x006 ]  
34 -> SM_CXMINTRACK           =>   145 [ 0x091 ]  
35 -> SM_CYMINTRACK           =>    37 [ 0x025 ]  
36 -> SM_CXDOUBLECLK          =>     4 [ 0x004 ]  
37 -> SM_CYDOUBLECLK          =>     4 [ 0x004 ]  
38 -> SM_CXICONSPACING        =>   143 [ 0x08f ]  
39 -> SM_CYICONSPACING        =>    94 [ 0x05e ]  
40 -> SM_MENUDROPALIGNMENT    =>     0 [ 0x000 ]  
41 -> SM_PENWINDOWS           =>     0 [ 0x000 ]  
42 -> SM_DBCSENABLED          =>     0 [ 0x000 ]  
43 -> SM_CMOUSEBUTTONS        =>     5 [ 0x005 ]  
44 -> SM_SECURE               =>     0 [ 0x000 ]  
45 -> SM_CXEDGE               =>     2 [ 0x002 ]  
46 -> SM_CYEDGE               =>     2 [ 0x002 ]  
47 -> SM_CXMINSPACING         =>   160 [ 0x0a0 ]  
48 -> SM_CYMINSPACING         =>    30 [ 0x01e ]  
49 -> SM_CXSMICON             =>    20 [ 0x014 ]  
50 -> SM_CYSMICON             =>    20 [ 0x014 ]  
51 -> SM_CYSMCAPTION          =>    25 [ 0x019 ]  
52 -> SM_CXSMSIZE             =>    15 [ 0x00f ]  
53 -> SM_CYSMSIZE             =>    24 [ 0x018 ]  
54 -> SM_CXMENUSIZE           =>    23 [ 0x017 ]  
55 -> SM_CYMENUSIZE           =>    24 [ 0x018 ]  
56 -> SM_ARRANGE              =>     8 [ 0x008 ]  
57 -> SM_CXMINIMIZED          =>   160 [ 0x0a0 ]  
58 -> SM_CYMINIMIZED          =>    30 [ 0x01e ]  
59 -> SM_CXMAXTRACK           =>  1696 [ 0x6a0 ]  
60 -> SM_CYMAXTRACK           =>  1066 [ 0x42a ]  
61 -> SM_CXMAXIMIZED          =>  1461 [ 0x5b5 ]  
62 -> SM_CYMAXIMIZED          =>  1062 [ 0x426 ]  
63 -> SM_NETWORK              =>     3 [ 0x003 ]  
67 -> SM_CLEANBOOT            =>     0 [ 0x000 ]  
68 -> SM_CXDRAG               =>     4 [ 0x004 ]  
69 -> SM_CYDRAG               =>     4 [ 0x004 ]  
70 -> SM_SHOWSOUNDS           =>     0 [ 0x000 ]  
71 -> SM_CXMENUCHECK          =>    17 [ 0x011 ]  
72 -> SM_CYMENUCHECK          =>    17 [ 0x011 ]  
73 -> SM_SLOWMACHINE          =>     0 [ 0x000 ]  
74 -> SM_MIDEASTENABLED       =>     0 [ 0x000 ]  
75 -> SM_MOUSEWHEELPRESENT    =>     1 [ 0x001 ]  
76 -> SM_XVIRTUALSCREEN       =>     0 [ 0x000 ]  
77 -> SM_YVIRTUALSCREEN       =>     0 [ 0x000 ]  
78 -> SM_CXVIRTUALSCREEN      =>  1680 [ 0x690 ]  
79 -> SM_CYVIRTUALSCREEN      =>  1050 [ 0x41a ]  
80 -> SM_CMONITORS            =>     1 [ 0x001 ]  
81 -> SM_SAMEDISPLAYFORMAT    =>     1 [ 0x001 ]  
82 -> SM_IMMENABLED           =>     1 [ 0x001 ]  
83 -> SM_CXFOCUSBORDER        =>     1 [ 0x001 ]  
84 -> SM_CYFOCUSBORDER        =>     1 [ 0x001 ]  
86 -> SM_TABLETPC             =>     0 [ 0x000 ]  
87 -> SM_MEDIACENTER          =>     1 [ 0x001 ]  
88 -> SM_STARTER              =>     0 [ 0x000 ]  
89 -> SM_SERVERR2             =>     0 [ 0x000 ]  
90 -> SM_CMETRICS             =>     0 [ 0x000 ]  
4096 -> SM_REMOTESESSION        =>     0 [ 0x000 ]
8192 -> SM_SHUTTINGDOWN         =>     0 [ 0x000 ]
8193 -> SM_REMOTECONTROL        =>     0 [ 0x000 ]
8194 -> SM_CARETBLINKINGENABLED =>     1 [ 0x001 ]

The  'Run User Func' button in ArrayDisplay will also launch MSDN GetSystemMetrics  documentation with the selected entry in the browser. Esc (or a click on Close ) does a refresh. To exit click the small 'Exit Script' button in ArrayDisplay.

It's a nice example for: 

  • Getting the Autoit include Dir + the use of StringRegExp to parse an *.au3 file.
  • SetError() to completely wrap an Autoit function (here FileRead) and also return the @error and @extended value
  • How to use _ArrayDisplay and _ArrayAdd

 

GetSystemMetrics_Demo.au3

Edited by Robinson1

Share this post


Link to post
Share on other sites

#8 ·  Posted

The user who you were quoting and replying to hasn't logged on in 7 years, for the record.


UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

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  
Followers 0