Syntax
Width.d = WidgetGetWidth(Widget, ReturnLayoutDefined.b=#False)Description
Retrieves the current actual rendered width of the specified widget in device-independent pixels (DIPs). Optionally, it can return the ideal width defined in the layout.
Parameters
Widget
The handle of the ProGUI widget whose width is to be retrieved.
ReturnLayoutDefined.b (optional)
If set to #True, the function returns the ideal width set
for the widget in the layout (using WidgetSetWidth() or CreateWidget()), potentially returning
#PG_Widget_FitContent (-1) if that was set. If
#False (default), returns the actual calculated width after
layout adjustments.
Return Value
Returns the requested widget width as a floating-point number
(Double) in DIPs. Returns 0.0 or #False if the
widget handle is invalid. If ReturnLayoutDefined is
#True, it might return #PG_Widget_FitContent
(-1).
Remarks
The actual rendered width (`ReturnLayoutDefined = #False`) can differ from the ideal width due to layout constraints (e.g., Flexbox stretching/shrinking, Grid column sizing). Getting the layout-defined width is useful for checking the intended size versus the rendered size.
Example
IncludeFile "ProGUI_PB.pbi"
StartProGUI()
MyWindow = CreateWindow(0, 0, 400, 200, "Widget Width Example", #PG_Window_Default | #PG_Window_LayoutFlex) ; Use a flex layout
LayoutSetPadding(#Null, 10) ; Set the current layout padding
If MyWindow
; Widget with a fixed ideal width
Widget1 = CreateWidget(0, 0, 150, 50)
WidgetSetClass(Widget1, "widget1")
WidgetSetMargin(Widget1, 5)
; Widget ideal width set to 200
Widget2 = CreateWidget(0, 0, 200, 50)
WidgetSetMinWidth(Widget2, 50) ; Give it a minimum width
WidgetSetClass(Widget2, "widget2")
WidgetSetMargin(Widget2, 5)
WindowShow(MyWindow, #True, #PG_WindowShow_ScreenCentered)
; Get widths after layout calculation
Width1_Actual.d = WidgetGetWidth(Widget1)
Width1_Ideal.d = WidgetGetWidth(Widget1, #True)
Width2_Actual.d = WidgetGetWidth(Widget2)
Width2_Ideal.d = WidgetGetWidth(Widget2, #True) ; Will be -1 (#PG_Widget_FitContent)
Debug "Widget 1 - Ideal Width: " + StrD(Width1_Ideal) + ", Actual Width: " + StrD(Width1_Actual)
Debug "Widget 2 - Ideal Width: " + StrD(Width2_Ideal) + ", Actual Width: " + StrD(Width2_Actual)
Repeat
Event = WaitWindowEvent()
Width2_Actual.d = WidgetGetWidth(Widget2)
Width2_Ideal.d = WidgetGetWidth(Widget2, #True) ; Will be -1 (#PG_Widget_FitContent)
Debug "Widget 2 - Ideal Width: " + StrD(Width2_Ideal) + ", Actual Width: " + StrD(Width2_Actual)
Until Event = #PB_Event_CloseWindow
EndIf
StopProGUI()See Also
WidgetSetWidth, WidgetGetHeight, WidgetGetX, WidgetGetY, WidgetSetMinWidth
Supported OS
Windows, Linux