ListIconGadget - resize content to fit perfectly
Posted: Mon Apr 15, 2024 8:10 am
I'd like to adjust the height of a listicon gadget to show exactly five rows without showing a scrollbar. The column widths have to be adjusted as well to fill the whole space of the gadget.
Actually I am using something like the following code, which is not perfect (the left gap seems to be larger than right one).
I am also unsure if it fails completely on different PC's, especially for screen scaling.
Actually I am using something like the following code, which is not perfect (the left gap seems to be larger than right one).
I am also unsure if it fails completely on different PC's, especially for screen scaling.
Code: Select all
; Define
Enumeration
#Win
#ListSource
#Font
EndEnumeration
#PB_DpiBits= 16
#PB_DpiScale= 1<<#PB_DpiBits
#ViewSourceLines=5
Global DpiScale.i
Global ListWidth.i
Global ListHeight.i
Global ListBorder.i
Global ListScroll.i
Macro ScaleUp(value)
(((value)*DpiScale)>>#PB_DpiBits)
EndMacro
Macro ScaleDown(value)
(((value)<<#PB_DpiBits)/DpiScale)
EndMacro
; EndDefine
Procedure Main()
Protected Metrics.NONCLIENTMETRICS
Metrics\cbSize=SizeOf(Metrics)
SystemParametersInfo_(#SPI_GETNONCLIENTMETRICS,SizeOf(Metrics),@Metrics,0)
LoadFont(#Font,PeekS(@Metrics\lfMenuFont\lfFaceName[0]),Metrics\lfMenuFont\lfHeight+2)
DpiScale=GetDeviceCaps_(GetDC_(0),#LOGPIXELSX)<<#PB_DpiBits/96
Width=600
Height=200
If OpenWindow(#Win,X,Y,Width,Height,"List",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget|#PB_Window_ScreenCentered)
ListIconGadget(#ListSource,20,262,500,130,"Line",60,#PB_ListIcon_FullRowSelect|#LVS_NOCOLUMNHEADER)
AddGadgetColumn(#ListSource,1,"Code",400)
WindowBounds(#Win,400,420,2000,1200)
SetGadgetFont(#ListSource,FontID(#Font))
; ***********************************************************************************************
ListBorder=GetSystemMetrics_(#SM_CXEDGE)<<1;+20
ListScroll=GetSystemMetrics_(#SM_CXVSCROLL)
ListWidth=WindowWidth(#Win)-40
ListHeight=90;
; ***********************************************************************************************
Rect.rect
Id=GadgetID(#ListSource)
AddGadgetItem(#ListSource,0,#LF$)
; same as #SM_EDGE (see above)?
GetWindowRect_(Id,Rect)
ListBorder=rect\bottom-rect\top
GetClientRect_(Id,Rect)
ListBorder+rect\top-rect\bottom
; could this be done easier?
Rect\left=#LVIR_LABEL
SendMessage_(Id,#LVM_GETITEMRECT,0,Rect)
ListHeight=(Rect\Bottom-Rect\Top)*#ViewSourceLines+ListBorder
ClearGadgetItems(#ListSource)
For x=0 To #ViewSourceLines-1
AddGadgetItem(#ListSource,x,#LF$+" - "+Str(x+1)+" -")
Next x
SetGadgetItemColor(#ListSource,#ViewSourceLines/2,#PB_Gadget_BackColor,$ffe0e0)
ResizeGadget(#ListSource,#PB_Ignore,#PB_Ignore,ListWidth,ListHeight)
n=GetGadgetItemAttribute(#ListSource,#Null,#PB_ListIcon_ColumnWidth,0)
SetGadgetItemAttribute(#ListSource,#Null,#PB_ListIcon_ColumnWidth,ListWidth-n-ListBorder*2,1)
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
EndProcedure
Main()