GetWindowWidth() : Returns combined width of 2 controls.

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

GetWindowWidth() : Returns combined width of 2 controls.

17 Jun 2021, 15:26

GetWindowWidth() accepts 2 Hwnds as parameters.
Second parameter can be a true in which case, Client width of Hwnd (parameter 1) will be returned

Call examples:

Code: Select all

GetWindowWidth(Hwnd) ; returns width of a Window/Control.
GetWindowWidth(Hwnd1, Hwnd2) ; returns combined width, i.e, space occupied by 2 controls including all the distance between them.
GetWindowWidth(Hwnd, True) ; returns client width of a Window/Control.
 
Note 1: For combined width, the Hwnds should be passed in correct order.
Control on left should be parameter 1 and the one right should be parameter 2.

Note 2:
just me wrote: The values returned by the function might need to be scaled if used for AHK Gui's without -DPIScale.
GuiControlGet, ..., Pos might be the better approach in this case
 
The function along with a GUI demo:

Code: Select all

GetWindowWidth(Hwnd1, Hwnd2:=0) { ; By SKAN on D46G/D46J @ tiny.cc/getwindowwidth
Local
  Client := (Hwnd2 <= 0xffff ? !! Hwnd2 : 0)
  VarSetCapacity(RECT, Hwnd2 > 0xffff ? 32 : 16, 0)
  Function := Format("User32.dll\Get{:}Rect", Client ? "Client" : "Window")
  L := DllCall(Function, "Ptr",Hwnd1, "Ptr",&RECT)
  R := Client ? 0 : L ? DllCall(Function, "Ptr",Hwnd2, "Ptr",&RECT+16) : 0
Return L ? NumGet(RECT, R ? 24 : 8, "Int") - NumGet(RECT, "Int") : ""
}


#NoEnv
#Warn
#SingleInstance, Force
SetWorkingDir, %A_ScriptDir%
SetBatchLines, -1

; The following semi-transparent bitmap should be visible on any window background color
hhr := DllCall("Gdi32.dll\CreateBitmap", "Int",1, "Int",2, "Int",0x1, "Int",32
              ,"Int64P",0x7f0f0f0f7ff0f0f0, "Ptr")

Gui, New, hwndhgui, GetWindowWidth() - Demo
Gui +DPIScale
Gui, Margin, 10, 10
Gui, Font, s9, Segoe UI
Gui, Font, s12, Consolas
Gui -DPIScale

Gui, Font, s9, Segoe UI
Gui, Add, Text, xm ym, Edit &1

Gui, Font, s12, Consolas
Gui, Add, Edit, xm y+4 hwndhEdit1, WWWWWWWWWWWW
GuiControl,, %hEdit1%
EW := GetWindowWidth(hEdit1)


Gui, Font, s9, Segoe UI
Gui, Add, Text, x+m ym, Edit &2

Gui, Font, s12, Consolas
Gui, Add, Edit, xp y+4 w%EW% hwndhEdit2

Gui, Font, s9, Segoe UI
Gui, Add, Text, x+m ym, Edit &3

Gui, Font, s12, Consolas
Gui, Add, Edit, xp y+4 w%EW% hwndhEdit3

Gui, Add, Picture, xm y+m w10 hwndhL1, HBITMAP:*%hhr%

Gui, Font, s9, Segoe UI
Gui, Add, Text, xm y+m, Edit &4

EW := GetWindowWidth(hEdit1, hEdit3)
Gui, Font, s12, Consolas
Gui, Add, Edit, xp y+4 w%EW% R12 hwndhEdit4, A_ScreenDPI = %A_ScreenDPI%

Gui, Add, Picture, xm y+m w10 hwndhL2, HBITMAP:*%hhr%

Gui, Font, s9, Segoe UI
Gui, Add, Button, gGuiClose hwndhButton, % "  E&xitApp  "

Gui, Show, AutoSize Hide
LW := GetWindowWidth(hGui, True)
GuiControl, Move, %hL1%, x0 w%LW%
GuiControl, Move, %hL2%, x0 w%LW%

GuiControlGet, B, Pos, %hButton%
GuiControl, Move, %hButton%, % "x" LW-BX-BW

Gui, Show
Return

GuiClose:
 ExitApp
 
Screenshots of demo taken in different Screen DPI:
 
image.png
image.png (9.4 KiB) Viewed 699 times
 
image.png
image.png (13.32 KiB) Viewed 699 times
 
image.png
image.png (16.84 KiB) Viewed 699 times
My Scripts and Functions: V1  V2
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: GetWindowWidth() : Returns combined width of 2 controls.

19 Jun 2021, 05:07

Hi SKAN,

Code: Select all

GetWindowWidth(Hwnd1, Hwnd2, True) ; can't imagine a use for this. Combined client width of 2 controls including all distance between them.
The returned 'combined width' is always the width of the second control.

The values returned by the function might need to be scaled if used for AHK Gui's without -DPIScale. GuiControlGet, ..., Pos might be the better approach in this case
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: GetWindowWidth() : Returns combined width of 2 controls.

19 Jun 2021, 07:57

Dear @just me

Thanks for testing 🙏
you wrote:

Code: Select all

GetWindowWidth(Hwnd1, Hwnd2, True) ; can't imagine a use for this. Combined client width of 2 controls including all distance between them.
The returned 'combined width' is always the width of the second control.
Yes. :oops:
It was redundant anyway... Have removed it.
you wrote: The values returned by the function might need to be scaled if used for AHK Gui's without -DPIScale.
GuiControlGet, ..., Pos might be the better approach in this case
I don't understand. :roll:
GetWindowWidth() would return the same value as GuiControlGet, ..., Pos irrespective of DPIScale.
Can you show me an example, please.
 
In some instances, GetWindowWidth() might be easier to use than GuiControlGet, ..., Pos.
For example, if we attach an UpDown to Edit control, their rectangles overlap.
With GuiControlGet, ..., Pos, you need to get/save the width of Edit control before attaching an UpDown control.
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: GetWindowWidth() : Returns combined width of 2 controls.

19 Jun 2021, 08:09

Dear @SKAN,

did you try to run your GUI demo without the Gui +/-DPIScale lines on 144 DPI?
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: GetWindowWidth() : Returns combined width of 2 controls.

19 Jun 2021, 08:23

just me wrote:
19 Jun 2021, 08:09
did you try to run your GUI demo without the Gui +/-DPIScale lines on 144 DPI?
I did try on 120 DPI. My calculation were getting messed up.
I inserted GuiControl,,Pos and I now see the difference between 2. :thumbup:
I will add your quote to the OP. Thanks. :)

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Epoch and 50 guests