coloring status bar text

Just starting out? Need help? Post your questions and find answers here.
User avatar
purebuilt
User
User
Posts: 46
Joined: Sun Dec 17, 2006 5:30 pm
Location: Milwaukee, WI, USA

coloring status bar text

Post by purebuilt »

Hi all,

How do you change the text color of one tab or section of your status bar text?

Thanks.
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

A code snipped can be found in the purearea code-archive. but it seems not to be possible just to colorize one tab. only the complete statusbar. :-( and even this does not work under all OS Versions.
Tranquil
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: coloring status bar text

Post by Fluid Byte »

purebuilt wrote:How do you change the text color of one tab or section of your status bar text?
Simple, you can't natively. Only option is to specify one or more owner-draw field parts:

Code: Select all

; Title:  Custom Statusbar Text Colors
; Author: Fluid Byte
; Date:   December 28, 2006

Structure ODSBTEXT
	szText.l
	clrFront.l
	clrBack.l
EndStructure

Procedure WindowCallback(hWnd.l,uMsg.l,wParam.l,lParam.l)
    Select uMsg
        Case #WM_DRAWITEM
 		*lpdis.DRAWITEMSTRUCT = lParam

		ClassName$ = Space(19) : GetClassName_(*lpdis\hwndItem,ClassName$,19)

		If ClassName$ = "msctls_statusbar32"
			*osbt.ODSBTEXT = *lpdis\itemData

			SetTextColor_(*lpdis\hDC,*osbt\clrFront)
			SetBkColor_(*lpdis\hDC,*osbt\clrBack)

			SendMessage_(*lpdis\hwndItem,#SB_GETBORDERS,0,aBorders.RECT)
			InflateRect_(*lpdis\rcItem,-aBorders\right / 2,0)		
			DrawText_(*lpdis\hDC,*osbt\szText,-1,*lpdis\rcItem,#DT_SINGLELINE | #DT_VCENTER)
		EndIf
    EndSelect
     
    ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

OpenWindow(0,0,0,350,100,"Custom Statusbar Text Colors",#WS_OVERLAPPEDWINDOW | 1)

SetWindowCallback(@WindowCallback())

Result = CreateStatusBar(0,WindowID(0))

AddStatusBarField(100) : StatusBarText(0,0,"Statusbar Field #1") 
AddStatusBarField(100) : StatusBarText(0,1,"Statusbar Field #2")
AddStatusBarField(100)

osbt.ODSBTEXT
osbt\szText = @"Statusbar Field #3"
osbt\clrFront = #Green
osbt\clrBack = #Black

SendMessage_(Result,#SB_SETTEXT,2 | #SBT_OWNERDRAW,osbt)

While WaitWindowEvent() ! 16 : Wend
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
zoot33
User
User
Posts: 69
Joined: Sat Jul 11, 2009 7:43 pm
Location: Oxford, UK

Re: coloring status bar text

Post by zoot33 »

In the above code, how could you centre the text within an owner drawn status area ?

Thanks
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: coloring status bar text

Post by Fluid Byte »

Just add the #DT_CENTER flag to the DrawText_() command.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
zoot33
User
User
Posts: 69
Joined: Sat Jul 11, 2009 7:43 pm
Location: Oxford, UK

Re: coloring status bar text

Post by zoot33 »

Thanks a lot Fluid
User avatar
Lord
Addict
Addict
Posts: 907
Joined: Tue May 26, 2009 2:11 pm

Re: coloring status bar text

Post by Lord »

Hi!

I know, this is an old posting, but I need some help here:

I tried to make a procedure from the above code.
The original code works, after changing szText.l to szText.i in structure ODBSTEXT.
So this is not my problem I got.

When moving the needed code part into a procedure, there is no text "3" in field #2.

I modified the code a bit to show the problem.
Just run my code first with Test=#True. There is a red "3" in field #2.
Now change Test to #False in order to run the code in the procedure.
There is no text in field 2.

What did I miss? Here is the code:

Code: Select all

; Title:  Custom Statusbar Text Colors
; Author: Fluid Byte
; Date:   December 28, 2006

Structure ODSBTEXT
   szText.i;   changed from .l to .i otherwise there is an IMA on DrawText_()
   clrFront.l
   clrBack.l
EndStructure

Procedure WindowCallback(hWnd.l,uMsg.l,wParam.l,lParam.l)
  Select uMsg
    Case #WM_DRAWITEM
      *lpdis.DRAWITEMSTRUCT = lParam
      
      ClassName$ = Space(19) : GetClassName_(*lpdis\hwndItem, ClassName$,19)
      
      If ClassName$ = "msctls_statusbar32"
        *osbt.ODSBTEXT = *lpdis\itemData
        
        SetTextColor_(*lpdis\hDC, *osbt\clrFront)
        SetBkColor_(*lpdis\hDC, *osbt\clrBack)
        
        SendMessage_(*lpdis\hwndItem, #SB_GETBORDERS, 0, aBorders.RECT)
        InflateRect_(*lpdis\rcItem, -aBorders\right / 2, 0)      
        DrawText_(*lpdis\hDC, *osbt\szText, -1, *lpdis\rcItem, #DT_SINGLELINE | #DT_VCENTER | #DT_RIGHT)
      EndIf
  EndSelect
  
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

Procedure myStatusBarText(Field, Text.s, fCol, bCol)
  Text+"   "
  osbt.ODSBTEXT
  osbt\szText = @Text
  osbt\clrFront = fCol
  osbt\clrBack  = bCol
  SendMessage_(Result,#SB_SETTEXT, Field | #SBT_OWNERDRAW, osbt)
EndProcedure  

OpenWindow(0,0,0,350,100,"Custom Statusbar Text Colors",#WS_OVERLAPPEDWINDOW | 1)

SetWindowCallback(@WindowCallback())

Result = CreateStatusBar(0,WindowID(0))

AddStatusBarField(100) : StatusBarText(0, 0, "1", #PB_StatusBar_Right)
AddStatusBarField(100) : StatusBarText(0, 1, "2", #PB_StatusBar_Right)
AddStatusBarField(100) :

Test=#True;  <--- Change here to #False to see the the difference

Text.s="3"
Field=2
fCol.l=#Red
bCol.l=$F1EDED

If Test=#False
  myStatusBarText(Field, Text, fCol, bCol)
Else
  Text+"   "
  osbt.ODSBTEXT
  osbt\szText = @Text
  osbt\clrFront = fCol
  osbt\clrBack  = bCol
  SendMessage_(Result,#SB_SETTEXT, Field | #SBT_OWNERDRAW, osbt)
EndIf

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Image
ccode
User
User
Posts: 99
Joined: Sat Jun 23, 2018 5:21 pm

Re: coloring status bar text

Post by ccode »

Hello Lord,

The variable "Result" is unknown in your procedure.

Try this:

Code: Select all

; Title:  Custom Statusbar Text Colors
; Author: Fluid Byte
; Date:   December 28, 2006

Global Result ; Look here

Structure ODSBTEXT
   szText.i;   changed from .l to .i otherwise there is an IMA on DrawText_()
   clrFront.l
   clrBack.l
EndStructure

Procedure WindowCallback(hWnd.l,uMsg.l,wParam.l,lParam.l)
  Select uMsg
    Case #WM_DRAWITEM
      *lpdis.DRAWITEMSTRUCT = lParam
     
      ClassName$ = Space(19) : GetClassName_(*lpdis\hwndItem, ClassName$,19)
     
      If ClassName$ = "msctls_statusbar32"
        *osbt.ODSBTEXT = *lpdis\itemData
       
        SetTextColor_(*lpdis\hDC, *osbt\clrFront)
        SetBkColor_(*lpdis\hDC, *osbt\clrBack)
       
        SendMessage_(*lpdis\hwndItem, #SB_GETBORDERS, 0, aBorders.RECT)
        InflateRect_(*lpdis\rcItem, -aBorders\right / 2, 0)     
        DrawText_(*lpdis\hDC, *osbt\szText, -1, *lpdis\rcItem, #DT_SINGLELINE | #DT_VCENTER | #DT_RIGHT)
      EndIf
  EndSelect
 
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

Procedure myStatusBarText(Field, Text.s, fCol, bCol)
  Text+"   "
  osbt.ODSBTEXT
  osbt\szText = @Text
  osbt\clrFront = fCol
  osbt\clrBack  = bCol
  SendMessage_(Result,#SB_SETTEXT, Field | #SBT_OWNERDRAW, osbt)
EndProcedure 

OpenWindow(0,0,0,350,100,"Custom Statusbar Text Colors",#WS_OVERLAPPEDWINDOW | 1)

SetWindowCallback(@WindowCallback())

Result = CreateStatusBar(0,WindowID(0))

AddStatusBarField(100) : StatusBarText(0, 0, "1", #PB_StatusBar_Right)
AddStatusBarField(100) : StatusBarText(0, 1, "2", #PB_StatusBar_Right)
AddStatusBarField(100) :

Test=#False;  <--- Change here to #False to see the the difference

Text.s="3"
Field=2
fCol.l=#Red
bCol.l=$F1EDED

If Test=#False
  myStatusBarText(Field, Text, fCol, bCol)
Else
  Text+"   "
  osbt.ODSBTEXT
  osbt\szText = @Text
  osbt\clrFront = fCol
  osbt\clrBack  = bCol
  SendMessage_(Result,#SB_SETTEXT, Field | #SBT_OWNERDRAW, osbt)
EndIf

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
User avatar
Lord
Addict
Addict
Posts: 907
Joined: Tue May 26, 2009 2:11 pm

Re: coloring status bar text

Post by Lord »

Hi ccode!
ccode wrote:...
The variable "Result" is unknown in your procedure.
...
That's it! :idea:
Thank you for pointing this out.
Image
User avatar
Lord
Addict
Addict
Posts: 907
Joined: Tue May 26, 2009 2:11 pm

Re: coloring status bar text

Post by Lord »

Sorry for disturbing again. :(

I still have a problem with coloring the text in a statusbar field.

The above shown code works...

...until you try to resize the window by dragging the bottom right corner.

When doing so, there is an IMA at

Code: Select all

DrawText_(*lpdis\hDC, *osbt\szText, -1, *lpdis\rcItem, #DT_SINGLELINE | #DT_VCENTER | #DT_RIGHT)
in the window callback.
*osbt\szText gets an value of 11 there. No valid pointer to text anymore.

Somebody any ideas?
Image
User avatar
Lord
Addict
Addict
Posts: 907
Joined: Tue May 26, 2009 2:11 pm

Re: coloring status bar text

Post by Lord »

Hi!

The problem with field #3 is still there. :cry:

Even when replacing *osbt\szText with @"3 " in WindowCallback()

Code: Select all

DrawText_(*lpdis\hDC, @"3   ", -1, *lpdis\rcItem, #DT_SINGLELINE | #DT_VCENTER | #DT_RIGHT)
or defining a variable Text.s="3 " and replacing *osbt\szText with @Text

Code: Select all

Protected Text.s="3   "
DrawText_(*lpdis\hDC, @Text, -1, *lpdis\rcItem, #DT_SINGLELINE | #DT_VCENTER | #DT_RIGHT)
the field #3 in statusbar has still only a black rectangle when resizing the window.
But in this two variants the IMA is gone.

I know, Rashad has posted a canvas based solution:
viewtopic.php?f=12&t=71093
and I could use this, but I want to know, what went wrong here.
Image
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4955
Joined: Sun Apr 12, 2009 6:27 am

Re: coloring status bar text

Post by RASHAD »

Hi Lord
;Posted by Lord
;Modified by RASHAD

Code: Select all

; Title:  Custom Statusbar Text Colors
; Author: Fluid Byte
; Date:   December 28, 2006

Structure ODSBTEXT
   szText.s;   changed from .l to .i otherwise there is an IMA on DrawText_()
   clrFront.i
   clrBack.i
EndStructure

Global Result,osbt.ODSBTEXT

Procedure WindowCallback(hWnd,uMsg,wParam,lParam)
  Select uMsg
    Case #WM_DRAWITEM
      *lpdis.DRAWITEMSTRUCT = lParam
     
      ClassName$ = Space(19) : GetClassName_(*lpdis\hwndItem, ClassName$,19)
     
      If ClassName$ = "msctls_statusbar32"
        *osbt.ODSBTEXT = *lpdis\itemData
       
        SetTextColor_(*lpdis\hDC, *osbt\clrFront)
        SetBkColor_(*lpdis\hDC, *osbt\clrBack)
       
        SendMessage_(*lpdis\hwndItem, #SB_GETBORDERS, 0, aBorders.RECT)
        InflateRect_(*lpdis\rcItem, -aBorders\right / 2, 0)     
        DrawText_(*lpdis\hDC, *osbt\szText, -1, *lpdis\rcItem, #DT_SINGLELINE | #DT_VCENTER | #DT_RIGHT)
      EndIf
  EndSelect
 
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

Procedure myStatusBarText(Field, Text.s, fCol, bCol)
  Text+"   "
  osbt.ODSBTEXT
  osbt\szText = Text
  osbt\clrFront = fCol
  osbt\clrBack  = bCol
  SendMessage_(Result,#SB_SETTEXT, Field | #SBT_OWNERDRAW, osbt)
EndProcedure 

OpenWindow(0,0,0,350,100,"Custom Statusbar Text Colors",#WS_OVERLAPPEDWINDOW | 1)

SetWindowCallback(@WindowCallback())

Result = CreateStatusBar(0,WindowID(0))

AddStatusBarField(100) : StatusBarText(0, 0, "1", #PB_StatusBar_Right)
AddStatusBarField(100) : StatusBarText(0, 1, "2", #PB_StatusBar_Right)
AddStatusBarField(100) :

Test=0;  <--- Change here to #False to see the the difference

Text.s="3"
Field=2
fCol.l=#Red
bCol.l=$F1EDED

If Test=#False
  myStatusBarText(Field, Text, fCol, bCol)
Else
  Text+"   "
  osbt.ODSBTEXT
  osbt\szText = Text
  osbt\clrFront = fCol
  osbt\clrBack  = bCol
  SendMessage_(Result,#SB_SETTEXT, Field | #SBT_OWNERDRAW, osbt)
EndIf

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
More control :

Code: Select all

; Title:  Custom Statusbar Text Colors
; Author: Fluid Byte
; Date:   December 28, 2006

Structure ODSBTEXT
   szText.s;   changed from .l to .i otherwise there is an IMA on DrawText_()
   clrFront.i
   ;clrBack.i
   hBrush.i
EndStructure

Global Result
Global Dim osbt.ODSBTEXT(5)

Procedure WindowCallback(hWnd,uMsg,wParam,lParam)
  Select uMsg
    Case #WM_DRAWITEM
      If wParam = GetDlgCtrlID_(Result)
        *lpdis.DRAWITEMSTRUCT = lParam  
        *osbt.ODSBTEXT = *lpdis\itemData       
        SetTextColor_(*lpdis\hDC, *osbt\clrFront)
        SetBkMode_(*lpdis\hDC, #TRANSPARENT)
        ;SetBkColor_(*lpdis\hDC, *osbt\clrBack)
        FillRect_(*lpdis\hDC, *lpdis\rcItem, *osbt\hBrush)
        ;SendMessage_(*lpdis\hwndItem,#SB_GETBORDERS,0,aBorders.RECT)
        ;InflateRect_(*lpdis\rcItem,-aBorders\right / 2,0)  
        DrawText_(*lpdis\hDC, *osbt\szText, -1, *lpdis\rcItem, #DT_SINGLELINE | #DT_VCENTER | #DT_RIGHT)
      EndIf
  EndSelect
 
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

Procedure myStatusBarText(Field, Text.s, fCol, bCol)
  Text+"   "
  osbt(Field)\szText = Text
  osbt(Field)\clrFront = fCol
  osbt(Field)\hBrush  = CreateSolidBrush_(bCol)
  SendMessage_(Result,#SB_SETTEXT, Field | #SBT_OWNERDRAW, osbt(Field))
EndProcedure

OpenWindow(0,0,0,350,100,"Custom Statusbar Text Colors",#WS_OVERLAPPEDWINDOW | 1)

SetWindowCallback(@WindowCallback())

Result = CreateStatusBar(0,WindowID(0))

AddStatusBarField(100); : StatusBarText(0, 0, "1", #PB_StatusBar_Right)
AddStatusBarField(100); : StatusBarText(0, 1, "2", #PB_StatusBar_Right)
AddStatusBarField(100); :

SendMessage_(Result, #SB_SETMINHEIGHT, 40, 0)
SendMessage_(Result, #WM_SIZE, 0,0)
Test=0;  <--- Change here to #False to see the the difference

Text.s="1"
Field=0
fCol.l=#Green
bCol.l=#Gray
myStatusBarText(Field, Text, fCol, bCol)

Text.s="2"
Field=1
fCol.l=#Yellow
bCol.l=#Blue
myStatusBarText(Field, Text, fCol, bCol)

Text.s="3"
Field=2
fCol.l=#Red
bCol.l=#Yellow
myStatusBarText(Field, Text, fCol, bCol)

; If Test=#False
;   myStatusBarText(Field, Text, fCol, bCol)
; Else
;   Text+"   "
;   osbt.ODSBTEXT
;   osbt\szText = Text
;   osbt\clrFront = fCol
;   osbt\clrBack  = bCol
;   SendMessage_(Result,#SB_SETTEXT, Field | #SBT_OWNERDRAW, osbt)
; EndIf

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Edit :Bugs fixed for PB x64
Last edited by RASHAD on Wed Jul 25, 2018 1:03 pm, edited 1 time in total.
Egypt my love
User avatar
Lord
Addict
Addict
Posts: 907
Joined: Tue May 26, 2009 2:11 pm

Re: coloring status bar text

Post by Lord »

Hi Rashad!

Thank you for looking at this problem.

The good news is:
The first code you posted is working stand alone as it should.

The bad news are:
If incorporated in my existing code, the your first example
always gives me a red "BZ" without trailing spaces.
Inside WindowsCallback() the text @**osbt\szText has changed.

The second code shows a statusbar of almost doubled height
and with no text in any field.

Thank you for your help, but I'm giving up on this.
This is a issue for you experts, but not for me.
Image
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: coloring status bar text

Post by Kwai chang caine »

Thanks RASHAD works like a charm 8)
ImageThe happiness is a road...
Not a destination
User avatar
Lord
Addict
Addict
Posts: 907
Joined: Tue May 26, 2009 2:11 pm

Re: coloring status bar text

Post by Lord »

Kwai chang caine wrote:Thanks RASHAD works like a charm 8)
That's interesting.
May I ask what Windows and what PB version you used to test?

I had in the just the chance to test the second version of Rashads code
on a Win81.1 with PB5.60(x64) while my sytem now is Win7, PB5.70(x64)beta.
On these the same result: not working.
Image
Post Reply