How to obtain number of element at top of scrolling window

Just starting out? Need help? Post your questions and find answers here.
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

How to obtain number of element at top of scrolling window

Post by Blue »

Sorry for the clumsy wording of the question...

I'm pretty sure I saw this question asked and answered before, but I don't even know how to formulate my search intelligently enough to get an answer. I've tried — in vain — all I could think of.

Since words won't come to mind, allow me to use a picture :
Image
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: How to obtain number of element at top of scrolling wind

Post by RASHAD »

Hello Blue

Code: Select all

OpenWindow(0,200,200,400,150,"Title", #PB_Window_SystemMenu|#PB_Window_ScreenCentered) 

TreeGadget(1,0,0,400,150) 
For t=1 To 50
  AddGadgetItem(1,-1,"item : "+Str(t)) 
Next
 
Repeat 
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
          Quit = 1
          
    Case #WM_NCMOUSEMOVE
        Position = SendMessage_(GadgetID(1), #WM_NCHITTEST, 0, DesktopMouseY()<<16 + DesktopMouseX())
        Select Position
          Case #HTVSCROLL
            item = GetScrollPos_(GadgetID(1), #SB_VERT)+1
            If item <> olditem              
              Debug "First Visible Item  = " + Str(item)
            EndIf
            olditem = item
        EndSelect
        
      
    Case #WM_KEYUP
        If GetActiveGadget() = 1 And (EventwParam() = 38 Or EventwParam() = 40)
          item = GetScrollPos_(GadgetID(1), #SB_VERT)+1
          If item <> olditem              
            Debug "First Visible Item  = " + Str(item)
          EndIf
          olditem = item
        EndIf          
  EndSelect 
Until Quit = 1
Egypt my love
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How to obtain number of element at top of scrolling wind

Post by Shardik »

Blue wrote:I'm pretty sure I saw this question asked and answered before, but I don't even know how to formulate my search intelligently enough to get an answer. I've tried — in vain — all I could think of.
RASHAD already posted two examples in this thread from 2013... :wink:

And you posted already a similar question on how to detect the top visible row in a ListViewGadget in this thread. There I posted a cross-platform solution. I reduced the example to Windows-only code and modified it for the TreeGadget:

Code: Select all

Procedure.I GetVisibleTopRow(ListIconID.I)
  Protected Item.TVITEM

  Item\mask = #TVIF_PARAM
  Item\hItem = SendMessage_(GadgetID(ListIconID), #TVM_GETNEXTITEM,
    #TVGN_FIRSTVISIBLE, GadgetItemID(ListIconID, 0))
  SendMessage_(GadgetID(TreeGadgetID), #TVM_GETITEM, 0, @Item)

  ProcedureReturn Item\lParam
EndProcedure

OpenWindow(0, 100, 100, 200, 155, "")
TreeGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 55)
ButtonGadget(1, 10, WindowHeight(0) - 35, WindowWidth(0) - 20, 25,
  "Get current top row")

For i = 1 To 50
  AddGadgetItem(0, -1, "Item " + Str(i))
Next i

SetWindowTitle(0, "Visible top row: " + Str(GetVisibleTopRow(0) + 1))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 1
        SetWindowTitle(0, "Visible top row: " + Str(GetVisibleTopRow(0) + 1))
      EndIf
  EndSelect
ForEver
User avatar
C87
Enthusiast
Enthusiast
Posts: 176
Joined: Mon Jul 17, 2017 7:22 am
Location: Cotswolds England

Re: How to obtain number of element at top of scrolling wind

Post by C87 »

Hello Blue, Having seen your post, I'm interested to know which software you used to show your example with the graphics and text added?

Also, have to say the solutions to your problem are absolutely first class. As it is so often the case in the PureB forum.

Cheers, C87
If it's falling over......just remember the computer is never wrong!
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: How to obtain number of element at top of scrolling wind

Post by RASHAD »

Thanks Shardik :P
I did not remember any
I should save my posts and the other forum members posts with the URL.
Bad

Thanks also for your snippet
Egypt my love
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to obtain number of element at top of scrolling wind

Post by infratec »

Extended version:

Code: Select all

EnableExplicit


Procedure.i GetVisibleFirstTreeItem(TreeGadget.i)
  
  Protected Item.TVITEM

  Item\mask = #TVIF_PARAM
  Item\hItem = SendMessage_(GadgetID(TreeGadget), #TVM_GETNEXTITEM, #TVGN_FIRSTVISIBLE, GadgetItemID(TreeGadget, 0))
  SendMessage_(GadgetID(TreeGadget), #TVM_GETITEM, 0, @Item)
  
  ProcedureReturn Item\lParam
  
EndProcedure



Procedure.i GetVisibleTreeCount(TreeGadget.i)
  
  ProcedureReturn SendMessage_(GadgetID(TreeGadget), #TVM_GETVISIBLECOUNT, 0, 0)
  
EndProcedure


Macro SetTitle()
  Count = GetVisibleTreeCount(0)
  SetWindowTitle(0, "Visible top row: " + Str(GetVisibleFirstTreeItem(0) + 1) + " " + Str(GetVisibleFirstTreeItem(0) + Count))  
EndMacro


Define i.i, Count.i


OpenWindow(0, 100, 100, 200, 155, "", #PB_Window_SystemMenu|#PB_Window_SizeGadget)
WindowBounds(0, 200, 155, 200, #PB_Ignore)
TreeGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 55)
ButtonGadget(1, 10, WindowHeight(0) - 35, WindowWidth(0) - 20, 25, "Get current top row")

For i = 1 To 50
  AddGadgetItem(0, -1, "Item " + Str(i))
Next i

SetTitle()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 1
        SetTitle()
      EndIf
    Case #PB_Event_SizeWindow
      ResizeGadget(0, 10, 10, #PB_Ignore, WindowHeight(0) - 55)
      ResizeGadget(1, 10, WindowHeight(0) - 35, #PB_Ignore, #PB_Ignore)
      SetTitle()
  EndSelect
ForEver
PeDe
Enthusiast
Enthusiast
Posts: 119
Joined: Sun Nov 26, 2017 3:13 pm
Location: Vienna
Contact:

Re: How to obtain number of element at top of scrolling wind

Post by PeDe »

C87 wrote:Hello Blue, Having seen your post, I'm interested to know which software you used to show your example with the graphics and text added?
I think Blue already mentioned it in another thread, it's "FastStone Capture v9". I like to use it, it also works well on very old PCs.
Edit: viewtopic.php?p=537823#p537823

Peter
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: How to obtain number of element at top of scrolling wind

Post by Blue »

Thank you to each of you.
With your enlightening contributions, I now have everything I need.
But they illustrate PB's major issue : i don't even have to think anymore! :shock:
Others do the thinking for me, and they do it so much better. :D
Seriously, though, had I searched for the word item instead of element, I would have found what I was looking for.

A special invitation to Shardik : there's a position open as Memory Manager in my brain. It's been vacant for a while now. It's yours for the asking... Meanwhile, your shocking exposé of my old post forced me to rediscover the "View your posts" function at the top right of each page. Very useful. Now, if I can only remember to use it....
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: How to obtain number of element at top of scrolling wind

Post by Blue »

Note to Shardik
Very interesting and imaginative solution, the way you use the window title as a display for the result that you're tracking. That never occurred to me before now, but I can see that, as a fast and dirty solution, it's hard to beat.

Thanks for a very interesting idea.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
Post Reply