Page 1 of 1
ListIcon: Adjusting wide of a column to the width of a text
Posted: Wed Jul 19, 2006 9:02 pm
by QuimV

Hello,
I find problems trying to adjust the wide of a column of a ListIcon to the width of a text.
Is able someone to help me?
The code I use is the following one:
Thanks in advanced!
QuimV
If StartDrawing(WindowOutput(#Window_Main))
wide.l=TextWidth("Test Field Name")
Else
wide=100
EndIf
;Add Column
AddGadgetColumn(#Gadget_Main_ListIcon, 1, "Test Field Name", wide)
Posted: Wed Jul 19, 2006 9:12 pm
by srod
The thing that comes to mind is that you need to set the drawing font to match the font of the listicon gadget.
Get the font of the listicon using either GetGadgetFont() or the api message #WM_GETFONT.
Once you have the font, use the command DrawingFont() after the StartDrawing() one.
Re: ListIcon: Adjusting wide of a column to the width of a t
Posted: Wed Jul 19, 2006 10:34 pm
by Kiffi
Hello QuimV,
QuimV wrote:I find problems trying to adjust the wide of a column of a ListIcon to the width of a text.
you can use
Code: Select all
SendMessage_(GadgetID(YourListIconGadget), #LVM_SETCOLUMNWIDTH, YourColumn, #LVSCW_AUTOSIZE_USEHEADER)
Greetings ... Kiffi
ListIcon: Adjusting wide of a column to the width of a text
Posted: Thu Jul 20, 2006 11:56 am
by QuimV
Hi,
Kiffi. You are right. Your code runs properly.
srod: Unfortunely TextWidth seems does not run properly.
Attach test code.
Thanks friends.
QuimV
;-------------------- TEST CODE ------------------------------
If OpenWindow(0, 100, 100, 300, 100, "ListIcon Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If LoadFont(0, "Verdana", 8,0)
SetGadgetFont(#PB_Default, FontID(0)) ; Set the loaded Verdana 8 font as new standard
EndIf
If CreateGadgetList(WindowID(0))
ListIconGadget(0, 5, 5, 290, 90, "Name", 50, #PB_ListIcon_FullRowSelect|#PB_ListIcon_AlwaysShowSelection)
If StartDrawing(WindowOutput(0))
FontID.l = GetGadgetFont(0)
DrawingFont(FontID)
text_wide.l=TextWidth("Test + test + test")
StopDrawing()
Else
ancho=100
EndIf
AddGadgetColumn(0, 1,"Test + test + test", text_wide)
AddGadgetItem(0, -1, "Harry Rannit"+Chr(10)+"12 Parliament Way, Battle Street, By the Bay")
AddGadgetItem(0, -1, "Ginger Brokeit"+Chr(10)+"130 PureBasic Road, BigTown, CodeCity")
SendMessage_(GadgetID(0), #LVM_SETCOLUMNWIDTH, 1, #LVSCW_AUTOSIZE_USEHEADER)
Debug "text_wide: " + Str(text_wide) ;pixels = 100
Debug "WindowWidth: " + Str(WindowWidth(0)) ;pixels = 300
Debug "GetGadgetItemAttribute: " + Str(GetGadgetItemAttribute(0, 1, #PB_ListIcon_ColumnWidth,1)) ;pixels = 100
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf
EndIf
Posted: Thu Jul 20, 2006 6:01 pm
by srod
QuimV, the code works okay here. You just have to comment out the SendMessage_() as you can't use both methods!
Code: Select all
If OpenWindow(0, 100, 100, 300, 100, "ListIcon Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If LoadFont(0, "Verdana", 8,0)
SetGadgetFont(#PB_Default, FontID(0)) ; Set the loaded Verdana 8 font as new standard
EndIf
If CreateGadgetList(WindowID(0))
ListIconGadget(0, 5, 5, 290, 90, "Name", 50, #PB_ListIcon_AlwaysShowSelection)
If StartDrawing(WindowOutput(0))
FontID.l = GetGadgetFont(0)
DrawingFont(FontID)
text_wide.l=TextWidth("Test + test + test")
StopDrawing()
Else
ancho=100
EndIf
AddGadgetColumn(0, 1,"Test + test + test", text_wide)
AddGadgetItem(0, -1, "Harry Rannit"+Chr(10)+"12 Parliament")
AddGadgetItem(0, -1, "Ginger Brokeit"+Chr(10)+"130 PureBasic")
;SendMessage_(GadgetID(0), #LVM_SETCOLUMNWIDTH, 1, #LVSCW_AUTOSIZE_USEHEADER)
Debug "text_wide: " + Str(text_wide) ;pixels = 100
Debug "WindowWidth: " + Str(WindowWidth(0)) ;pixels = 300
Debug "GetGadgetItemAttribute: " + Str(GetGadgetItemAttribute(0, -1, #PB_ListIcon_ColumnWidth,1)) ;pixels = 100
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf
EndIf
Posted: Thu Jul 20, 2006 6:12 pm
by QuimV
Hello srod,
Unfortunly if I comment out the SendMessage_(), the code does not work on my PC.
My O.S. is W2K. May be in WXP runs OK!
Thanks again
QuimV
Posted: Thu Jul 20, 2006 6:54 pm
by srod
Sorry, I'm half asleep at the moment.
Seems that Window's uses 16 pixels to accommodate the dividers etc. (at least on my setup). Accordingly you have to add 16 pixels to the column width etc.
I can't find anything in system metrics about this, but I'm still looking...
Posted: Thu Jul 20, 2006 7:04 pm
by QuimV

Hi srod,
You are right, the dividers .... +16 pixels...!
OK!
Thanks a lot for your help.
QuimV
Posted: Tue Feb 06, 2007 11:12 am
by Shardik
Helpful to know:
Microsoft Knowledge Base Article ID 246364 wrote:
SYMPTOMS
Sending a list view control an LVM_SETCOLUMNWIDTH message where the new width is set to LVSCW_AUTOSIZE or LVSCW_AUTOSIZE_USEHEADER will not work as expected for column 0 in report mode if an indent value is specified for any item.
CAUSE
The LVSCW_AUTOSIZE and LVSCW_AUTOSIZE_USEHEADER both are used to calculate the width of a column header based on either the header test and item text length. On COMMCTL32.DLL versions 4.70 and 4.72, the indent is not included in this calculation.
RESOLUTION
This has been fixed as of 5.80 of COMCTL32.dll. For versions prior to 5.80 the width of the column must be set by specifying a width in the lParam of the LVM_SETCOLUMNWIDTH message.
Code example for adjusting the width of column 0 or 1 of a ListIconGadget either to the column's header width or to the width of the column's widest column entry:
Code: Select all
If OpenWindow(0, 100, 100, 300, 160, "ListIcon adjust column width", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If CreateGadgetList(WindowID(0))
If LoadFont(0, "Verdana", 8, 0)
SetGadgetFont(#PB_Default, FontID(0)) ; Set the loaded Verdana 8 font as new standard
EndIf
ListIconGadget(0, 5, 5, 290, 50, "Name", 50, #PB_ListIcon_AlwaysShowSelection)
AddGadgetColumn(0, 1, "Test + test + test", 94)
AddGadgetItem(0, -1, "Harry Rannit" + #LF$ + "12 Parliament")
AddGadgetItem(0, -1, "Ginger Brokeit" + #LF$ + "130 PureBasic")
ButtonGadget(1, 5, 60, 290, 20, "Adjust column 0 to header width")
ButtonGadget(2, 5, 85, 290, 20, "Adjust column 0 to column width")
ButtonGadget(3, 5, 110, 290, 20, "Adjust column 1 to header width")
ButtonGadget(4, 5, 135, 290, 20, "Adjust column 1 to column width")
If StartDrawing(WindowOutput(0))
FontID = GetGadgetFont(0)
DrawingFont(FontID)
Column0HeaderWidth = TextWidth("Name") + 16
StopDrawing()
EndIf
Repeat
WindowEvent = WaitWindowEvent()
If WindowEvent = #PB_Event_Gadget
Select EventGadget()
Case 1
SendMessage_(GadgetID(0), #LVM_SETCOLUMNWIDTH, 0, Column0HeaderWidth)
Case 2
SendMessage_(GadgetID(0), #LVM_SETCOLUMNWIDTH, 0, #LVSCW_AUTOSIZE)
Case 3
SendMessage_(GadgetID(0), #LVM_SETCOLUMNWIDTH, 1, #LVSCW_AUTOSIZE_USEHEADER)
Case 4
SendMessage_(GadgetID(0), #LVM_SETCOLUMNWIDTH, 1, #LVSCW_AUTOSIZE)
EndSelect
EndIf
Until WindowEvent = #PB_Event_CloseWindow
EndIf
EndIf
If you adjust the last column of a ListIconGadget to its header width, be aware of this MSDN statement:
http://msdn.microsoft.com/library/defau ... nwidth.asp
MSDN wrote:
LVSCW_AUTOSIZE_USEHEADER
Automatically sizes the column to fit the header text. If you use this value with the last column, its width is set to fill the remaining width of the list-view control.
Posted: Tue Feb 06, 2007 9:20 pm
by QuimV

Interesting,
Thanks Shardik