ListIcon Scroll Position Saving

Share your advanced PureBasic knowledge/code with the community.
Psych
Enthusiast
Enthusiast
Posts: 239
Joined: Thu Dec 18, 2008 3:35 pm
Location: Wales, UK

ListIcon Scroll Position Saving

Post by Psych »

I wrote this because I had problems with a listicon jumping around when items were added, and I didnt like it.

Code: Select all

Procedure ListViewSaveScroll(gadget,disableredraw=1)
	If IsGadget(gadget)
		If CountGadgetItems(gadget)
			If disableredraw
				SendMessage_(GadgetID(gadget),#WM_SETREDRAW,0,0)
			EndIf
			SetProp_(GadgetID(gadget),@"ListScrollPos",GetScrollPos_(GadgetID(gadget),#SB_VERT))
		EndIf
	EndIf
EndProcedure
Procedure ListViewRestoreScroll(gadget,enableredraw=1)
	If IsGadget(gadget)
		If CountGadgetItems(gadget)
			Protected r.RECT,p=GetProp_(GadgetID(gadget),@"ListScrollPos")
			r\left=#LVIR_BOUNDS
			SendMessage_(GadgetID(gadget),#LVM_GETITEMRECT,0,@r)
			SendMessage_(GadgetID(gadget),#LVM_SCROLL,0,(r\bottom-r\top)*p)
			If enableredraw
				SendMessage_(GadgetID(gadget),#WM_SETREDRAW,-1,0)
				RedrawWindow_(GadgetID(gadget),0,0,#RDW_ERASE|#RDW_FRAME|#RDW_INVALIDATE|#RDW_ALLCHILDREN)
			EndIf
		EndIf
	EndIf
EndProcedure
So basically you write:

Code: Select all

ListViewSaveScroll(listview)

;modify list

ListViewRestoreScroll(listview)
My code actually worked without the final RedrawWindow, but It doesn't flicker when included and I wouldnt want my control displaying old information, so better to be safe than sorry.
Tested on Win7 x64
**edit... you didnt need the r.RECT declare (now removed), that was for an earlier method, lol**
Last edited by Psych on Sun Aug 26, 2012 5:24 am, edited 1 time in total.
----------------------------------------------------------------------------
Commenting your own code is admitting you don't understand it.
----------------------------------------------------------------------------
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: ListIcon Scroll Position Saving

Post by IdeasVacuum »

Nice snippet, thanks for sharing it 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply