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: