text in progressbar

Just starting out? Need help? Post your questions and find answers here.
Micko
Enthusiast
Enthusiast
Posts: 244
Joined: Thu May 24, 2007 7:36 pm
Location: Senegal
Contact:

text in progressbar

Post by Micko »

hi Guys !
i'm trying to make one thing here
put text in this progressbar but it is not easy

Code: Select all

Enumeration
  #Window_0
EndEnumeration
Enumeration
  #ProgressBar_0
EndEnumeration
Define.l Event, EventWindow, EventGadget, EventType, EventMenu


  If OpenWindow(#Window_0, 591, 251, 251, 49, "Text in progressbar", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
    If CreateGadgetList(WindowID(#Window_0))
      ProgressBarGadget(#ProgressBar_0, 5, 10, 240, 30, 0, 100, #PB_ProgressBar_Smooth)
      SetGadgetState(#ProgressBar_0,50)
    EndIf
  EndIf

Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_Gadget
      EventGadget = EventGadget()
      EventType = EventType()
      If EventGadget = #ProgressBar_0
      EndIf
    Case #PB_Event_CloseWindow
      EventWindow = EventWindow()
      If EventWindow = #Window_0
        CloseWindow(#Window_0)
        Break
      EndIf
  EndSelect
ForEver
thanks in advance
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Just a quick kick at the cat:

Code: Select all

CreateImage(0, 240,30,#PB_Image_DisplayFormat)

Enumeration 
  #Window_0 
EndEnumeration 
Enumeration 
  #ProgressBar_0 
EndEnumeration 
Define.l Event, EventWindow, EventGadget, EventType, EventMenu 

Procedure PGProc(hwnd, msg, wparam, lparam)
  oldproc = GetProp_(hwnd, "oldproc")
  Select msg
    Case #WM_PAINT
      BeginPaint_(hwnd, ps.paintstruct)
      hdcout = ps\hdc
      hdcin = StartDrawing(ImageOutput(0))
        Box(0,0,240,30,GetSysColor_(#COLOR_BTNFACE))
        width.d = 240/1000*GetGadgetState(#ProgressBar_0)
        Box(0,0,width,30,RGB(180,180,180))
        DrawingMode(#PB_2DDrawing_Transparent)
        DrawingFont(GetStockObject_(#DEFAULT_GUI_FONT))
        txt$ = "Progress Bar"
        tw = TextWidth(txt$)
        th = TextHeight(txt$)
        DrawText(GadgetWidth(#ProgressBar_0)/2-0.5*tw,GadgetHeight(#ProgressBar_0)/2-0.5*th,txt$)
        BitBlt_(hdcout,0,0,240,30,hdcin,0,0,#SRCCOPY)
      StopDrawing()
      EndPaint_(hwnd, ps)
      ProcedureReturn 0
    Case #WM_NCDESTROY
      RemoveProp_(hwnd, "oldproc")
  EndSelect
  ProcedureReturn CallWindowProc_(oldproc, hwnd, msg, wparam, lparam)
EndProcedure


  If OpenWindow(#Window_0, 591, 251, 251, 49, "Text in progressbar", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar) 
    If CreateGadgetList(WindowID(#Window_0)) 
      ProgressBarGadget(#ProgressBar_0, 5, 10, 240, 30, 0, 1000, #PB_ProgressBar_Smooth) 
      SetProp_(GadgetID(#ProgressBar_0),"oldproc",SetWindowLong_(GadgetID(#ProgressBar_0),#GWL_WNDPROC,@PGProc()))      
    EndIf 
  EndIf 


cc=0
Repeat 
  Event = WaitWindowEvent(1) 
  If cc<1000:cc+2:EndIf
  SetGadgetState(#ProgressBar_0, cc)
  Select Event 
    Case #PB_Event_Gadget 
      EventGadget = EventGadget() 
      EventType = EventType() 
      If EventGadget = #ProgressBar_0 
      EndIf 
    Case #PB_Event_CloseWindow 
      EventWindow = EventWindow() 
      If EventWindow = #Window_0 
        CloseWindow(#Window_0) 
        Break 
      EndIf 
  EndSelect 
ForEver
Last edited by netmaestro on Fri Feb 15, 2008 2:18 am, edited 1 time in total.
BERESHEIT
Micko
Enthusiast
Enthusiast
Posts: 244
Joined: Thu May 24, 2007 7:36 pm
Location: Senegal
Contact:

Post by Micko »

hi Boss !
nice to talk you netmaestro, this is big kick at the cat :D.
it is possible to fixe the text ? if i want to define "Progressbar" like the default text
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Sure, sample is edited to fix the text and center it in the gadget.
BERESHEIT
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Thanks.
A little variation :

Code: Select all

;
; ProgressBar With Text
;
Enumeration 
  #Window_0 
EndEnumeration 
Enumeration 
  #ProgressBar_0 
  #ProgressBar_1
  #Text_0
EndEnumeration 
;
Procedure ProcTextProgressBarGadget(hwnd, msg, wParam, lParam) 
  Protected OldProc.l, ImgID.l, ImgID2.l, hdcOut.l, hdcIn.l, ps.PAINTSTRUCT, GadgetNumber.l
  Protected Text.s, BarColor.l, Progression.d, BackColor.l, ProgWidth.d, TextColor2.l
  OldProc = GetProp_(hwnd, "OldProc") 
  TextColor = GetProp_(hwnd, "TextColor") 
  TextColor2 = GetProp_(hwnd, "TextColor2") 
  BackColor = GetProp_(hwnd, "BackColor") 
  BarColor = GetProp_(hwnd, "BarColor") 
  Select msg 
    Case #WM_PAINT 
      GadgetNumber = GetDlgCtrlID_(hwnd)
      Progression = (GetGadgetState(GadgetNumber) - GetGadgetAttribute(GadgetNumber, #PB_ProgressBar_Minimum)) / (GetGadgetAttribute(GadgetNumber, #PB_ProgressBar_Maximum) - GetGadgetAttribute(GadgetNumber, #PB_ProgressBar_Minimum))
      ProgWidth = GadgetWidth(GadgetNumber)* Progression
      Text = StrD(Progression * 100, 0) + "%"
      BeginPaint_(hwnd, @ps) ;>
        hdcOut = ps\hdc 
        ImgID2 = CreateImage(#PB_Any, GadgetWidth(GadgetNumber), GadgetHeight(GadgetNumber), #PB_Image_DisplayFormat) 
        StartDrawing(ImageOutput(ImgID2)) 
          DrawingMode(#PB_2DDrawing_Transparent) 
          DrawingFont(GetStockObject_(#DEFAULT_GUI_FONT)) 
          Box(0, 0, GadgetWidth(GadgetNumber), GadgetHeight(GadgetNumber), BarColor) 
          FrontColor(TextColor)
          DrawText(GadgetWidth(GadgetNumber) / 2 - 0.5 * TextWidth(Text), GadgetHeight(GadgetNumber) / 2 - 0.5 * TextHeight(Text), Text) 
        StopDrawing() 
        ImgID = CreateImage(#PB_Any, GadgetWidth(GadgetNumber), GadgetHeight(GadgetNumber), #PB_Image_DisplayFormat) 
        hdcIn = StartDrawing(ImageOutput(ImgID)) 
          DrawingMode(#PB_2DDrawing_Transparent) 
          DrawingFont(GetStockObject_(#DEFAULT_GUI_FONT)) 
          Box(0, 0, GadgetWidth(GadgetNumber), GadgetHeight(GadgetNumber), BackColor) 
          FrontColor(TextColor2)
          DrawText(GadgetWidth(GadgetNumber) / 2 - 0.5 * TextWidth(Text), GadgetHeight(GadgetNumber) / 2 - 0.5 * TextHeight(Text), Text) 
          If ProgWidth > 0
            ImgTmpID = GrabImage(ImgID2 , #PB_Any, 0, 0, ProgWidth, GadgetHeight(GadgetNumber))        
            DrawImage(ImageID(ImgTmpID), 0, 0) 
            FreeImage(ImgTmpID)
          EndIf 
          BitBlt_(hdcOut, 0, 0, GadgetWidth(GadgetNumber), GadgetHeight(GadgetNumber), hdcIn, 0, 0, #SRCCOPY) 
        StopDrawing() 
      EndPaint_(hwnd, ps) ;<
      FreeImage(ImgID)
      FreeImage(ImgID2)
      ProcedureReturn #Null
    Case #WM_NCDESTROY 
      RemoveProp_(hwnd, "OldProc") 
      RemoveProp_(hwnd, "TextColor")
      RemoveProp_(hwnd, "TextColor2")
      RemoveProp_(hwnd, "BackColor")
      RemoveProp_(hwnd, "BarColor")
  EndSelect 
  ProcedureReturn CallWindowProc_(OldProc, hwnd, msg, wParam, lParam) 
EndProcedure 
Procedure CreateTextProgressBarGadget(GadgetNumber.l, X.l, Y.l, width.l, Height.l, Minimum.l, Maximum.l, FirstTextColor.l = #PB_Ignore, SecondTextColor.l = #PB_Ignore, BarColor.l = #PB_Ignore, BackColor.l = #PB_Ignore)
  Protected ImgID.l, ImgID2.l
  If FirstTextColor = #PB_Ignore
    FirstTextColor = GetSysColor_(#COLOR_WINDOWTEXT)
  EndIf
  If SecondTextColor = #PB_Ignore
    SecondTextColor = GetSysColor_(#COLOR_HIGHLIGHTTEXT)
  EndIf
  If BackColor = #PB_Ignore
    BackColor = GetSysColor_(#COLOR_BTNFACE)
  EndIf
  If BarColor = #PB_Ignore
    BarColor = GetSysColor_(#COLOR_HIGHLIGHT)
  EndIf
  ProgressBarGadget(GadgetNumber, X, Y, width, Height, Minimum, Maximum)
  SetProp_(GadgetID(GadgetNumber), "BackColor", BackColor)      
  SetProp_(GadgetID(GadgetNumber), "BarColor", BarColor)      
  SetProp_(GadgetID(GadgetNumber), "TextColor", SecondTextColor)      
  SetProp_(GadgetID(GadgetNumber), "TextColor2", FirstTextColor)      
  SetProp_(GadgetID(GadgetNumber), "OldProc", SetWindowLong_(GadgetID(GadgetNumber), #GWL_WNDPROC, @ProcTextProgressBarGadget()))      
EndProcedure
;
; ----------- T E S T ----------
;
If OpenWindow(#Window_0, 591, 251, 251, 110, "Text in progressbar", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar) 
  If CreateGadgetList(WindowID(#Window_0)) 
    CreateTextProgressBarGadget(#ProgressBar_0, 5, 10, 240, 30, 0, 2000)
    CreateTextProgressBarGadget(#ProgressBar_1, 5, 50, 200, 30, 0, 1000, #Blue, #Red, #White, #Green)
    TextGadget(#Text_0, 5, 90, 245, 20, "", #PB_Text_Center)
  EndIf 
EndIf 
;
cc=0 
Repeat 
  Event = WaitWindowEvent(1) 
  If cc<2000:cc+2
    SetGadgetState(#ProgressBar_0, cc) 
    SetGadgetState(#ProgressBar_1, cc) 
    SetGadgetText(#Text_0, Str(cc) + " / 2000")
  EndIf 
  Select Event 
    Case #PB_Event_CloseWindow 
      CloseWindow(#Window_0) 
      Break 
  EndSelect 
ForEver
EDIT : updated with latest NetMaestro code
Last edited by gnozal on Fri Feb 12, 2010 4:51 pm, edited 2 times in total.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

I had two thoughts...

1 create a sprite of the text to display and constantly creat and destroy the sprite in the proper place... and this...

A simple drawtext. Unless the Progressbar is updated over the text it is muddled until overdrawn. There may be another way around this. I haven't figured it out yet. Right now you have to set the Progressbar state to 100 and then back to whateve it is supposed to be.

Mine uses all PB Code and no Windows. :D I think mine is shorter too???

Code: Select all


Enumeration
  #Window_0
EndEnumeration

Enumeration
  #ProgressBar_0
EndEnumeration

Structure VisualDesignerGadgets
  Gadget.l
  EventFunction.l
EndStructure

Global NewList EventProcedures.VisualDesignerGadgets()
Global t ; value to start the progress bar

Procedure drawit()
x = 182
y = 36
t = t + 1

If t > 100 
  t = 0
EndIf

SetGadgetState(#ProgressBar_0,100) ; effectively clears the PBAR
SetGadgetState( #ProgressBar_0, t) ; set progress bar value = to percentage reported

T$ = Str(t)

Text$ = T$+" %"

OutputID = WindowOutput(#Window_0)

StartDrawing(OutputID)
    DrawingMode(#PB_2DDrawing_Transparent | #PB_2DDrawing_Default)
       DrawText(x, y, Text$)
StopDrawing() 

EndProcedure

Procedure ProgressBar_0_Event(Window, Event, Gadget, Type)
  Debug "#ProgressBar_0"
EndProcedure

Procedure RegisterGadgetEvent(Gadget, *Function)
  
  If IsGadget(Gadget)
    AddElement(EventProcedures())
    EventProcedures()\Gadget        = Gadget
    EventProcedures()\EventFunction = *Function
  EndIf
  
EndProcedure

Procedure CallEventFunction(Window, Event, Gadget, Type)
  
  ForEach EventProcedures()
    If EventProcedures()\Gadget = Gadget
      CallFunctionFast(EventProcedures()\EventFunction, Window, Event, Gadget, Type)
      LastElement(EventProcedures())
    EndIf
  Next
  
EndProcedure

Procedure Open_Window_0()
  
  If OpenWindow(#Window_0, 5, 5, 400, 173, "Window 0",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
    If CreateGadgetList(WindowID(#Window_0))
      ProgressBarGadget(#ProgressBar_0, 10, 35, 365, 20, 0, 100, #PB_ProgressBar_Smooth)
      RegisterGadgetEvent(#ProgressBar_0, @ProgressBar_0_Event())
      
    EndIf
  EndIf
EndProcedure

Open_Window_0()

SetGadgetColor(#ProgressBar_0, #PB_Gadget_FrontColor, RGB(0, 213, 3))

Repeat
  
  Event  = WaitWindowEvent(500) ; waits for 1/2 a second (500 milliseconds)
  Gadget = EventGadget()
  Type   = EventType()
  Window = EventWindow()
  
drawit() ; calls the drawit() procedure to update progressbar... 
; *** you could have this send the actual number to set the progressbar to
; *** by changing the procedure to expect a variable. drawit(amount) would
; *** then be the call. and your procedure would be changed somewhat.

  Select Event
    Case #PB_Event_Gadget
      CallEventFunction(Window, Event, Gadget, Type)
      
  EndSelect

Until Event = #PB_Event_CloseWindow

End
I had a problem with the text overdrawing itself. This was fixed by setting the progressbar to 100% aand back to whatever % it was supposed to be on at that moment. 8)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

The new procedure wold look like this:

Code: Select all

Procedure drawit(amount) 
x = 182 
y = 36 

; *** just making sure we don't go over 100%
If amount > 100 
  amount = 0 
EndIf 

SetGadgetState(#ProgressBar_0,100) ; effectively clears the PBAR 
SetGadgetState( #ProgressBar_0, amount) ; set progress bar value = to percentage reported 

T$ = Str(amount) 

Text$ = T$+" %" 

OutputID = WindowOutput(#Window_0) 

StartDrawing(OutputID) 
    DrawingMode(#PB_2DDrawing_Transparent | #PB_2DDrawing_Default) 
       DrawText(x, y, Text$) 
StopDrawing() 

EndProcedure 
And you can call this in you code by setting
amount = ##
drawit(amount)
:D
Last edited by Rook Zimbabwe on Sun Feb 17, 2008 4:44 pm, edited 1 time in total.
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Very nice stuff.

Any flag that can be used in the bitblt to make the text change colour in the parts overlapping the changed background colour?

I tried options to xor, then to or, then to and. Then went ballistic with the #SRCCOPY flag parameter to get some interesting - and unwanted results :) - but nothing that modded the text colour where it covered a different background colour).

(Not important BTW, I just got sidetracked on an impulse there).
Dare2 cut down to size
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Any flag that can be used in the bitblt to make the text change colour in the parts overlapping the changed background colour?
I had tried the XOR as well, no luck in PB AFAIK... 8)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

hm, seems like I can't manage it without cheating, though there should be a more direct way. I must be missing something.

Code: Select all

CreateImage(0, 240,30,#PB_Image_DisplayFormat) 
CreateImage(1, 240,30,#PB_Image_DisplayFormat)

StartDrawing(ImageOutput(1)) 
  Box(0,0,240,30,RGB(180,180,180)) 
  DrawingMode(#PB_2DDrawing_Transparent) 
  DrawingFont(GetStockObject_(#DEFAULT_GUI_FONT)) 
  txt$ = "Progress Bar" 
  tw = TextWidth(txt$) 
  th = TextHeight(txt$) 
  DrawText(240/2-0.5*tw,30/2-0.5*th,txt$,#White) 
StopDrawing()

Enumeration 
  #Window_0 
EndEnumeration 
Enumeration 
  #ProgressBar_0 
EndEnumeration 

Procedure PGProc(hwnd, msg, wparam, lparam) 
  oldproc = GetProp_(hwnd, "oldproc") 
  Select msg 
    Case #WM_PAINT 
      BeginPaint_(hwnd, ps.paintstruct) 
      hdcout = ps\hdc 
      hdcin = StartDrawing(ImageOutput(0)) 
        Box(0,0,240,30,GetSysColor_(#COLOR_BTNFACE)) 
        width.d = 240/1000*GetGadgetState(#ProgressBar_0) 
        DrawingMode(#PB_2DDrawing_Transparent)         
        DrawingFont(GetStockObject_(#DEFAULT_GUI_FONT)) 
        txt$ = "Progress Bar" 
        tw = TextWidth(txt$) 
        th = TextHeight(txt$)         
        DrawText(GadgetWidth(#ProgressBar_0)/2-0.5*tw,GadgetHeight(#ProgressBar_0)/2-0.5*th,"Progress Bar")
        GrabImage(1,2,0,0,width,30)        
        DrawImage(ImageID(2),0,0)
        BitBlt_(hdcout,0,0,240,30,hdcin,0,0,#SRCCOPY) 
      StopDrawing() 
      EndPaint_(hwnd, ps) 
      ProcedureReturn 0 
    Case #WM_NCDESTROY 
      RemoveProp_(hwnd, "oldproc") 
  EndSelect 
  ProcedureReturn CallWindowProc_(oldproc, hwnd, msg, wparam, lparam) 
EndProcedure 

  If OpenWindow(#Window_0, 591, 251, 251, 49, "Text in progressbar", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar) 
    If CreateGadgetList(WindowID(#Window_0)) 
      ProgressBarGadget(#ProgressBar_0, 5, 10, 240, 30, 0, 1000, #PB_ProgressBar_Smooth) 
      SetProp_(GadgetID(#ProgressBar_0),"oldproc",SetWindowLong_(GadgetID(#ProgressBar_0),#GWL_WNDPROC,@PGProc()))      
    EndIf 
  EndIf 

Repeat 
  Event = WaitWindowEvent(1) 
  If cc<1000:cc+2:EndIf 
  SetGadgetState(#ProgressBar_0, cc) 
  Select Event 
    Case #PB_Event_CloseWindow 
      CloseWindow(#Window_0) 
      Break 
  EndSelect 
ForEver
BERESHEIT
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Cheating?

I consider it "thinking outside the box".

That is terrific. Thanks mate! :)
Dare2 cut down to size
Micko
Enthusiast
Enthusiast
Posts: 244
Joined: Thu May 24, 2007 7:36 pm
Location: Senegal
Contact:

Post by Micko »

Hi guys sorry because I was caught by a headache :oops:
but now it's ok !
i see that this thread has moved :D

@ Rook Zimbabwe
your code is usefull :)

@gnozal
nice one ! :)

@netmaestro
exactely what i need. i will study your code and post some question

Thanks you Guys
:D
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

@ Rook Zimbabwe
your code is usefull
Well there is a first time for everything! :wink:

I hope it helps! Gnozal and Netmaestro have been two people on this board that have helped me learn about PB as well!!! 8)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Post Reply