I couldn't find a library for PB v4, so I made this one.
Code: Select all
; PercentBarGadget
;
; Author: breeze4me
; Date: 1st May, 2006
;
; For PureBasic v4.00 Beta 11
;
Structure PercentBar
;hPercentBar.l
img.i
Width.l
Height.l
Min.l
Max.l
Value.i
TextColor.l
FrontColor.l
BackColor.l
TextX.l
TextY.l
TextFont.i
EndStructure
Declare SetPercentBarValue(GadgetNumber, Value)
Procedure SetPercentBarColor(GadgetNumber, FrontColor, BackColor, TextColor=$FFFFFF)
Protected *mem, *prop.PercentBar
If IsGadget(GadgetNumber)
*mem = GetWindowLong_(GadgetID(GadgetNumber), #GWL_USERDATA)
*prop = *mem
*prop\TextColor = TextColor
If FrontColor<>#PB_Default : *prop\FrontColor = FrontColor : EndIf
If BackColor<>#PB_Default : *prop\BackColor = BackColor : EndIf
EndIf
EndProcedure
Procedure PercentBarGadget(GadgetNumber, x, y, Width, Height, Min, Max)
Protected *mem, *prop.PercentBar
Protected hPercentBar, img=CreateImage(#PB_Any, Width, Height)
Protected SysCol1, SysCol2
If Min>=Max : ProcedureReturn 0 : EndIf
If img
hPercentBar = ImageGadget(GadgetNumber, x, y, Width, Heigth, 0)
If hPercentBar
*mem = AllocateMemory(SizeOf(PercentBar))
SetWindowLong_(hPercentBar, #GWL_USERDATA, *mem)
*prop = *mem
;*prop\hPercentBar= hPercentBar
*prop\img = img
*prop\Max = Max
*prop\Min = Min
*prop\Width = Width
*prop\Height = Height
SysCol1 = GetSysColor_(#COLOR_3DHIGHLIGHT)
SysCol2 = GetSysColor_(#COLOR_3DSHADOW)
SetPercentBarColor(GadgetNumber,GetSysColor_(#COLOR_HIGHLIGHT),GetSysColor_(#COLOR_BTNFACE), GetSysColor_(#COLOR_CAPTIONTEXT))
If StartDrawing(ImageOutput(img))
*prop\TextX = (Width-1-TextWidth("100 %"))/2
*prop\TextY = (Height-TextHeight(" "))/2 ;+ 1
Box(0, 0, Width, Height, *prop\BackColor)
Line(0, 0, 0, Height, SysCol2)
Line(0, 0, Width, 0, SysCol2)
Line(0, Height-1, Width, 0, SysCol1)
Line(Width-1, 0, 0, Height, SysCol1)
StopDrawing()
EndIf
SetPercentBarValue(GadgetNumber, Min)
ProcedureReturn hPercentBar
EndIf
EndIf
EndProcedure
Procedure SetPercentBarValue(GadgetNumber, Value)
Protected *mem, *prop.PercentBar, ValueF.f
If IsGadget(GadgetNumber)
*mem = GetWindowLong_(GadgetID(GadgetNumber), #GWL_USERDATA)
*prop = *mem
If Value>*prop\Max : Value=*prop\Max : EndIf
If Value<*prop\Min : Value=*prop\Min : EndIf
*prop\Value = Value
ValueF = (Value-*prop\Min) * 100.0 / (*prop\Max-*prop\Min)
If StartDrawing(ImageOutput(*prop\img))
Box(1, 1, *prop\Width-2, *prop\Height-2, *prop\BackColor)
Box(1, 1, (*prop\Width-2) * ValueF / 100.0, *prop\Height-2 , *prop\FrontColor)
DrawingMode(#PB_2DDrawing_Transparent)
If *prop\TextFont : DrawingFont(*prop\TextFont) : EndIf
DrawText(*prop\TextX, *prop\TextY, Str(Int(ValueF))+" %", *prop\TextColor)
StopDrawing()
SetGadgetState(GadgetNumber, ImageID(*prop\img))
EndIf
EndIf
EndProcedure
Procedure SetPercentBarFont(GadgetNumber, FontName$, YSize, Flag=0)
Protected *mem, *prop.PercentBar
If IsGadget(GadgetNumber)
*mem = GetWindowLong_(GadgetID(GadgetNumber), #GWL_USERDATA)
*prop = *mem
*prop\TextFont = FontID(LoadFont(#PB_Any, FontName$, YSize, Flag))
If StartDrawing(ImageOutput(*prop\img))
DrawingFont(*prop\TextFont)
*prop\TextX = (*prop\Width-1-TextWidth("100 %"))/2
*prop\TextY = (*prop\Height-TextHeight(" "))/2 ;+ 1
StopDrawing()
EndIf
EndIf
EndProcedure
; Test.
hWnd=OpenWindow(0, 0, 0, 300, 150, "PercentBarGadget", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
If hwnd
PercentBarGadget(0, 10, 20, 280, 25, -100, 900)
PercentBarGadget(1, 10, 60, 280, 45, 1000, 1500)
PercentBarGadget(2, 10, 120, 280, 25, 0, 300)
SetPercentBarColor(0, $951225, $EBA6B1, $E07181)
SetPercentBarFont(1, "MS Sans Serif", 20, #PB_Font_Italic|#PB_Font_HighQuality)
SetPercentBarColor(2, #PB_Default, #PB_Default, $9922FF)
v=-100
w=1000
x=0
Repeat
e=WindowEvent()
v+1
w+1
x+1
SetPercentBarValue(0, v)
SetPercentBarValue(1, w)
SetPercentBarValue(2, x)
If v>=900 : v=-100 : EndIf
If w>=1500 : w=1000 : EndIf
If x>=300 : x=0 : EndIf
Delay(1)
Until e=#PB_Event_CloseWindow
EndIf
End