[Solved] Scrollbar - detect size of dynamic bars (Windows)

Just starting out? Need help? Post your questions and find answers here.
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

[Solved] Scrollbar - detect size of dynamic bars (Windows)

Post by Michael Vogel »

I'd like to retrieve the length of the scrollbar indicators - which would allow to scroll page wise, etc.

The following example allows to scroll up/down/left/right page by page by using the buttons - but the how to calculate the magic number to do stepping correctly? As soon some lines are inserted (by pressing the button '+') or the width of the gadget will be changed, the values have to be recalculated.

Code: Select all

LoadFont(0,"Tahoma",14)

si.SCROLLINFO

OpenWindow(0,0,0,420,250,"Window",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)
ListIconGadget(1,0,0,200,250,"",1000)
TextGadget(2,310,10,100,25,"")
TextGadget(3,310,40,100,25,"")
TextGadget(4,310,70,100,25,"")
TextGadget(5,310,100,100,25,"")

ButtonGadget(10,345,180,30,25,"+")
ButtonGadget(11,310,180,30,25,"<")
ButtonGadget(13,345,150,30,25,"^")
ButtonGadget(12,380,180,30,25,">")
ButtonGadget(14,345,210,30,25,">")
AddWindowTimer(0,0,250)

SetGadgetFont(1,FontID(0))
text$ = "yiuy ioy  oiuoi uoiu oiu oiu uoi io oi uo yioioiyiuyui iouioioiyoiyoiyoiy oiy oiy oiy o yoiy oiyoiy "
For i = 0 To 15
	AddGadgetItem(1, -1, Str(i)+" "+text$)
Next

Repeat
	Select WaitWindowEvent(1)
	Case #PB_Event_Gadget,#PB_Event_Menu
		e=EventGadget()
		Select e
		Case 10
			AddGadgetItem(1, -1, text$)
		Case 11,12
			px=GetScrollPos_(GadgetID(1),#SB_HORZ)
			magic=181
			SetScrollPos_(GadgetID(1),#SB_HORZ,px+ magic *(1-(e&1)<<1),#True)
		Case 13,14
			py=GetScrollPos_(GadgetID(1),#SB_VERT)
			magic=7
			SetScrollPos_(GadgetID(1),#SB_VERT,py+ magic *(1-(e&1)<<1),#True)
		EndSelect

	Case #PB_Event_CloseWindow
		Quit = 1

	Case #PB_Event_Timer
		px=GetScrollPos_(GadgetID(1),#SB_HORZ)
		py=GetScrollPos_(GadgetID(1),#SB_VERT)
		GetScrollRange_(GadgetID(1),#SB_HORZ,@dummy,@wx)
		GetScrollRange_(GadgetID(1),#SB_VERT,@dummy,@wy)

		SetGadgetText(2,"PX: "+Str(px))
		SetGadgetText(3,"PY: "+Str(py))
		SetGadgetText(4,"WX: "+Str(wx))
		SetGadgetText(5,"WY: "+Str(wy))

	EndSelect

Until Quit = 1

Last edited by Michael Vogel on Mon Aug 15, 2022 2:27 pm, edited 2 times in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Scrollbar - detect size of dynamic bars (Windows)

Post by RASHAD »

Code: Select all

LoadFont(0,"Tahoma",14)

si.SCROLLINFO

OpenWindow(0,0,0,420,250,"Window",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)
ListIconGadget(1,0,0,200,250,"",1000)
TextGadget(2,310,10,100,25,"")
TextGadget(3,310,40,100,25,"")
TextGadget(4,310,70,100,25,"")
TextGadget(5,310,100,100,25,"")

ButtonGadget(10,345,180,30,25,"+")
ButtonGadget(11,310,180,30,25,"<")
ButtonGadget(13,345,150,30,25,"^")
ButtonGadget(12,380,180,30,25,">")
ButtonGadget(14,345,210,30,25,"v")
AddWindowTimer(0,0,250)

SetGadgetFont(1,FontID(0))
text$ = "yiuy ioy  oiuoi uoiu oiu oiu uoi io oi uo yioioiyiuyui iouioioiyoiyoiyoiy oiy oiy oiy o yoiy oiyoiy "
For i = 0 To 15
	AddGadgetItem(1, -1, Str(i)+" "+text$)
Next

item = 15
item_h = SendMessage_(GadgetID(1), #LVM_GETITEMSPACING, #True, 0) >> 16
Repeat
	Select WaitWindowEvent(1)
	Case #PB_Event_Gadget,#PB_Event_Menu
		e=EventGadget()
		Select e
		Case 10
			item + 1
    	AddGadgetItem(1, -1, Str(item)+" "+text$)	
		Case 11,12
			px=GetScrollPos_(GadgetID(1),#SB_HORZ)
			magic=181
			SetScrollPos_(GadgetID(1),#SB_HORZ,px+ magic *(1-(e&1)<<1),#True)
   	Case 13   	  
    	SendMessage_(GadgetID(1), #LVM_SCROLL, 0, -1 * Item_H)
    Case 14      
      SendMessage_(GadgetID(1), #LVM_SCROLL, 0, 1 * Item_H)
		EndSelect

	Case #PB_Event_CloseWindow
		Quit = 1

	Case #PB_Event_Timer
		px=GetScrollPos_(GadgetID(1),#SB_HORZ)
		py=GetScrollPos_(GadgetID(1),#SB_VERT)*item_h
		GetScrollRange_(GadgetID(1),#SB_HORZ,@dummy,@wx)
		GetScrollRange_(GadgetID(1),#SB_VERT,@dummy,@wy)

		SetGadgetText(2,"PX: "+Str(px))
		SetGadgetText(3,"PY: "+Str(py))
		SetGadgetText(4,"WX: "+Str(wx))
		SetGadgetText(5,"WY: "+Str(wy))

	EndSelect

Until Quit = 1


Egypt my love
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Scrollbar - detect size of dynamic bars (Windows)

Post by Michael Vogel »

Okay, done now - thanks to your help!

Here's the code which works fine now to do pagewise scrolling...

Code: Select all


EnableExplicit

Procedure GetScrollSize(Gadget,Scrollbar)

	Protected si.SCROLLINFO
	Protected ret

	Gadget=GadgetID(Gadget)

	With si
		\cbSize=SizeOf(SCROLLINFO)
		\fMask=#SIF_ALL;#SIF_PAGE|#SIF_POS
		GetScrollInfo_(Gadget,Scrollbar,@si)

		If \nMax-\nMin>=\nPage
			Debug Str(\nMin)+" ... "+Str(\nPos)+" ... "+Str(\nMax)+" [ "+Str(\nPage)+" ] ";+Str(\nTrackPos) <- nur beim Draggen verwendet
			ret=\nPage
			If Scrollbar=#SB_VERT
				ret*(SendMessage_(Gadget,#LVM_GETITEMSPACING,#True,0)>>16)
			EndIf
		EndIf
	EndWith

	ProcedureReturn ret

EndProcedure

Procedure Init()

	Protected text$
	Protected i

	LoadFont(0,"Tahoma",14)

	OpenWindow(0,0,0,420,250,"Window",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)
	ListIconGadget(1,0,0,200,250,"",1000)
	TextGadget(2,310,10,100,25,"")
	TextGadget(3,310,40,100,25,"")
	TextGadget(4,310,70,100,25,"")
	TextGadget(5,310,100,100,25,"")

	ButtonGadget(10,345,180,30,25,"+")
	ButtonGadget(11,310,180,30,25,"<")
	ButtonGadget(13,345,150,30,25,"^")
	ButtonGadget(12,380,180,30,25,">")
	ButtonGadget(14,345,210,30,25,"v")
	AddWindowTimer(0,0,250)

	SetGadgetFont(1,FontID(0))
	text$ = "yiuy ioy  oiuoi uoiu oiu oiu uoi io oi uo yioioiyiuyui iouioioiyoiyoiyoiy oiy oiy oiy o yoiy oiyoiy "

	For i = 0 To 15
		AddGadgetItem(1, -1, Str(i)+" "+text$)
	Next

EndProcedure
Procedure Main()

	Init()

	Protected e,item
	Protected px,py,wx,wy,dummy
	Protected width,height

	item=CountGadgetItems(1)


	Repeat
		Select WaitWindowEvent(1)
		Case #PB_Event_Gadget,#PB_Event_Menu
			e=EventGadget()
			Select e
			Case 10
				item + 1
				AddGadgetItem(1, -1, Str(item)+" "+RSet("",item*8," "+Str(item)))
			Case 11,12
				width=GetScrollSize(1,#SB_HORZ)
				If width
					SendMessage_(GadgetID(1), #LVM_SCROLL, (1-(e&1)<<1) * width,0)
				EndIf
			Case 13,14
				height=GetScrollSize(1,#SB_VERT)
				If height
					SendMessage_(GadgetID(1), #LVM_SCROLL, 0,(1-(e&1)<<1) * height)
				EndIf
			EndSelect

		Case #PB_Event_CloseWindow
			End

		Case #PB_Event_Timer
			px=GetScrollPos_(GadgetID(1),#SB_HORZ)
			py=GetScrollPos_(GadgetID(1),#SB_VERT)
			GetScrollRange_(GadgetID(1),#SB_HORZ,@dummy,@wx)
			GetScrollRange_(GadgetID(1),#SB_VERT,@dummy,@wy)

			SetGadgetText(2,"PX: "+Str(px))
			SetGadgetText(3,"PY: "+Str(py))
			SetGadgetText(4,"WX: "+Str(wx))
			SetGadgetText(5,"WY: "+Str(wy))

		EndSelect

	ForEver

EndProcedure

Main()
Post Reply