Listview without horizontal scroll bar
Posted: Wed Oct 20, 2010 10:32 pm
I needed a listview without a horizontal scroll bar for one of my projects, it took me a while to figure it out but it finally works. It uses flat scroll bars, you can also customize them a little with the flat scroll bar api but i haven't tried it.
It's pretty easy to use, just 4 functions to create, resize and add/remove items.
It's pretty easy to use, just 4 functions to create, resize and add/remove items.
Code: Select all
;Listview without horizontal scrollbar.
;Justin 11/10
enableexplicit
define.i event, margins, winwidth, winheight
define.i lv, i, lvWidth, hwHdr, hdrHeight
structure HDLAYOUT
*prc.RECT
*pwpos.WINDOWPOS
endstructure
procedure lvCreate(id.i, x.i, y.i, width.i, height.i, title.s, titlewidth.i, flags.i=0)
;Creates a listview with the appropiate styles.
define.i hwLv, r
r = ListIconGadget(id, x, y, width, height, title, titlewidth, flags|#LVS_NOSCROLL)
if id=#pb_any
hwLv = GadgetID(r)
else
hwLv = GadgetID(id)
endif
InitializeFlatSB_(hwLv)
SendMessage_(hwLv, #LVM_SETEXTENDEDLISTVIEWSTYLE, #LVS_EX_FLATSB, #LVS_EX_FLATSB)
procedurereturn r
endprocedure
procedure lvUpdateVScroll(lv.i)
;Updates the vertical scrollbar.
;Needs to be called when an item is inserted or removed or the listview size changes.
define.SCROLLINFO si
define.i hwLv
hwLv = GadgetID(lv)
si\cbSize = sizeof(SCROLLINFO)
si\fMask = #SIF_RANGE | #SIF_PAGE
si\nMin = 1
si\nMax = SendMessage_(hwLv, #LVM_GETITEMCOUNT, 0, 0)
si\nPage = SendMessage_(hwLv, #LVM_GETCOUNTPERPAGE, 0, 0)
FlatSB_SetScrollInfo_(hwLv, #SB_VERT, @si, #true)
endprocedure
procedure lvResizeHeader(lv.i)
;Resizes the header to fit the listview.
;Needs to be called when the listview size changes.
define.i hwHdr, hwLv
define.RECT rcLv
define.HDLAYOUT hdl
define.WINDOWPOS wpos
hwLv = GadgetID(lv)
hwHdr = SendMessage_(hwLv, #LVM_GETHEADER, 0, 0)
if hwHdr
GetClientRect_(hwLv, @rcLv)
hdl\prc = @rcLv
hdl\pwpos = @wpos
SendMessage_(hwHdr, #HDM_LAYOUT, 0, @hdl)
SetWindowPos_(hwHdr, hdl\pwpos\hwndInsertAfter, hdl\pwpos\x, hdl\pwpos\y, hdl\pwpos\cx, hdl\pwpos\cy, hdl\pwpos\flags | #SWP_SHOWWINDOW)
endif
endprocedure
procedure lvResize(lv.i, x.i, y.i, width.i, height.i)
;Resizes the listview and updates the header and vertical scrollbar.
ResizeGadget(lv, x, y, width, height)
lvUpdateVScroll(lv)
lvResizeHeader(lv)
endprocedure
procedure lvAddItem(lv.i, pos.i, text.s, image.i=0)
AddGadgetItem(lv, pos, text, image)
lvUpdateVScroll(lv)
endprocedure
procedure lvRemoveItem(lv.i, pos.i, text.s, image.i=0)
RemoveGadgetItem(lv, pos)
lvUpdateVScroll(lv)
endprocedure
;- TEST
margins = 5
winwidth = 300
winheight = 200
lvWidth = winwidth - margins*2
If OpenWindow(0, 100, 100, winwidth, winheight, "Listview Flat Scrollbar", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
lv = lvCreate(#pb_any, margins, margins, lvWidth, winheight - margins*2, "Column 1", lvWidth/2, #PB_ListIcon_FullRowSelect|#PB_ListIcon_AlwaysShowSelection)
AddGadgetColumn(lv, 1, "Column 2", lvWidth/2)
for i=1 to 20
lvAddItem(lv, -1, "Item " + str(i) + Chr(10) + "Subitem " + str(i))
next
Repeat
event = WaitWindowEvent()
select event
case #PB_Event_SizeWindow
lvResize(lv, #pb_ignore, #pb_ignore, WindowWidth(0)- margins*2, WindowHeight(0)- margins*2)
endselect
Until event = #PB_Event_CloseWindow
EndIf