# Calculate New position and size of a Rect

## Recommended Posts

I trying calculate the new position and size of a rect in diferent gui sizes. Here`s my function

```\$aRect = _Convert(10,10,20,20,800,600,1024,768)

MsgBox(0,"","Left: " & \$aRect[0] & @CRLF & "Top: " & \$aRect[1] & @CRLF & "Widht: " & \$aRect[2] & @CRLF & "Height: " & \$aRect[3])

Func _Convert(\$iLeft,\$iTop,\$iWidht,\$iHeight,\$iSResWidht,\$iSResHeight,\$iDResWidht,\$iDResHeight)

Local \$aRect[4]

\$iPercentX = \$iDResWidht / \$iSResWidht
\$iPercentY = \$iDResHeight / \$iSResHeight

\$aRect[0] = \$iLeft * \$iPercentX
\$aRect[1] = \$iTop * \$iPercentY
\$aRect[2] = \$iWidht * \$iPercentX
\$aRect[3] = \$iHeight * \$iPercentY

Return \$aRect

EndFunc;_Convert```

Someone can tell me if it returning exactly. Thanks in advance

##### Share on other sites

How should the result look like when the ratio of the destination dimension is different, e.g. 1920x1080?

Br,

UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

Looks working. See

```\$aRect = _Convert(10,10,20,20,102.4,80,192,108)

;MsgBox(0,"","Left: " & \$aRect[0] & @CRLF & "Top: " & \$aRect[1] & @CRLF & "Widht: " & \$aRect[2] & @CRLF & "Height: " & \$aRect[3])

Func _Convert(\$iLeft,\$iTop,\$iWidht,\$iHeight,\$iSResWidht,\$iSResHeight,\$iDResWidht,\$iDResHeight)

Local \$aRect[4]

\$iPercentX = \$iDResWidht / \$iSResWidht
\$iPercentY = \$iDResHeight / \$iSResHeight

\$aRect[0] = \$iLeft * \$iPercentX
\$aRect[1] = \$iTop * \$iPercentY
\$aRect[2] = \$iWidht * \$iPercentX
\$aRect[3] = \$iHeight * \$iPercentY

Return \$aRect

EndFunc;_Convert

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
\$Form1 = GUICreate("Form1", 900,450,-1,-1)
GUISetState(@SW_SHOW)

GUICtrlCreateGraphic(1,1,102.4,80)
GUICtrlSetBkColor(-1,0x0000FF)
GUICtrlSetState(-1,\$GUI_DISABLE)

GUICtrlCreateGraphic(10,10,20,20)
GUICtrlSetBkColor(-1,0xFF0000)

GUICtrlCreateGraphic(450,1,192,108)
GUICtrlSetBkColor(-1,0x0000FF)
GUICtrlSetState(-1,\$GUI_DISABLE)

GUICtrlCreateGraphic(450+\$aRect[0],\$aRect[1],\$aRect[2],\$aRect[3])
GUICtrlSetBkColor(-1,0xFF0000)

While 1
\$nMsg = GUIGetMsg()
Switch \$nMsg
Case \$GUI_EVENT_CLOSE
Exit

EndSwitch
WEnd```

##### Share on other sites

If this is the result you are expecting then it is ok.

I thought the ration of the red square should be the same after resize.

Br,

UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

## Create an account

Register a new account

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...