[Solved] Get X pos and width of a StatusBar field?

Windows specific forum
BarryG
Addict
Addict
Posts: 4225
Joined: Thu Apr 18, 2019 8:17 am

[Solved] Get X pos and width of a StatusBar field?

Post by BarryG »

Hi, trying to work out how to get the X position and width of a specific StatusBar field. Not sure, but thinking it's to do with #SB_GETPARTS, but I don't know. Can someone please help me out? Code template below to work with. Thanks!

Code: Select all

OpenWindow(0, 400, 200, 600, 100, "StatusBar", #PB_Window_SystemMenu)

sb=CreateStatusBar(0, WindowID(0))
AddStatusBarField(Random(200,70))
AddStatusBarField(Random(200,70)) : StatusBarText(0,1,"X/W of this")
AddStatusBarField(Random(200,70))

; Do the math here. :)

Debug "X of field 1 = "+Str(x)
Debug "W of field 1 = "+Str(w)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Last edited by BarryG on Thu Feb 20, 2025 1:36 am, edited 3 times in total.
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: Get X pos and width of a StatusBar field?

Post by chi »

Code: Select all

OpenWindow(0, 400, 200, 600, 100, "StatusBar", #PB_Window_SystemMenu)

sb=CreateStatusBar(0, WindowID(0))
AddStatusBarField(Random(200,70))
AddStatusBarField(Random(200,70)) : StatusBarText(0,1,"X/W of this")
AddStatusBarField(Random(200,70))

Debug SendMessage_(sb, #SB_GETPARTS, 0, 0) ;field count

; Do the math here. :)
SendMessage_(sb, #SB_GETRECT, 1, sbRect.RECT)

Debug "X of field 1 = "+Str(sbRect\left)
Debug "W of field 1 = "+Str(sbRect\right-sbRect\left)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Et cetera is my worst enemy
BarryG
Addict
Addict
Posts: 4225
Joined: Thu Apr 18, 2019 8:17 am

Re: Get X pos and width of a StatusBar field?

Post by BarryG »

Thank you, Chi!

Just to show I was trying, here's part of my code that I was working on, which worked for each X position, and I was trying to get the widths next (which I assume I do by subtracting each new width from the previous in the array). I suck at basic math sometimes!

But your code is so much simpler, and doesn't need an array. Thank you again!

Code: Select all

Dim StatusBarFieldSize(5) ; Because my app has 5 StatusBar fields.
SendMessage_(statusbar,#SB_GETPARTS,5,@StatusBarFieldSize()) ; Get each field X pos into the array.
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: [Solved] Get X pos and width of a StatusBar field?

Post by chi »

If you only need the right edge of each field, you could use SB_GETPARTS like this...

Code: Select all

OpenWindow(0, 400, 200, 600, 100, "StatusBar", #PB_Window_SystemMenu)

sb=CreateStatusBar(0, WindowID(0))
AddStatusBarField(Random(200,70))
AddStatusBarField(Random(200,70)) : StatusBarText(0,1,"X/W of this")
AddStatusBarField(Random(200,70))
AddStatusBarField(#PB_Ignore)

count = SendMessage_(sb, #SB_GETPARTS, 0, 0)
Dim statbar.l(count-1)
SendMessage_(sb, #SB_GETPARTS, count, statbar())
For i=0 To count-1
  Debug "field " + Str(i) + " re: " + statbar(i)
Next
Debug ""

; Do the math here. :)
SendMessage_(sb, #SB_GETRECT, 1, sbRect.RECT)

Debug "X of field 1 = "+Str(sbRect\left)
Debug "W of field 1 = "+Str(sbRect\right-sbRect\left)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Et cetera is my worst enemy
Post Reply