Eigenes Progressbargadget

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
a14xerus
Beiträge: 1440
Registriert: 14.12.2005 15:51
Wohnort: Aachen

Eigenes Progressbargadget

Beitrag von a14xerus »

Ich hab in letzter Zeit immer wider gelesen, das welche nicht mit dem XP-SkinGadget zufrieden sind, also hab ich mich mal hingesetzt und schnell was kleines programmiert:

Code: Alles auswählen

EnableExplicit
; ##################################################################################################
; ###########################      ProgressbarGadget by Alexander N      ###########################
; ###########################      -> http://www.alexander-n.de/ <-      ###########################
; ###########################      for PB 4.xx   [03.06.2007 14:14]      ###########################
; ##################################################################################################

Structure ProgressbarGadgetOwn
  created.l
  image.l
  ImageGadget.l
  x.l
  y.l
  width.d
  height.d
  Minimum.d
  Maximum.d
  backgroundcolor.l
  frontcolor.l
  ShowPercent.l
  textcolor.l
  state.d
EndStructure

#ProgressbarGadgetOwnMax = 99

Global Dim ProgressBarGadgetOwnData.ProgressbarGadgetOwn(#ProgressbarGadgetOwnMax)

Procedure RedrawProgressbarGadgetOwn(ProgressbarGadgetOwn.l)
  Protected tmp$
  If ProgressbarGadgetOwn <= #ProgressbarGadgetOwnMax
    With ProgressBarGadgetOwnData(ProgressbarGadgetOwn)
      If \created
        If StartDrawing(ImageOutput(\image))
          Protected tmp.l = Abs((\width/(\Maximum-\Minimum)*\state))
          Box(0,0,\width,\height,\backgroundcolor)
          Box(0,0,tmp,\height,\frontcolor)
          DrawingMode(#PB_2DDrawing_Transparent)
          tmp$ = Str(tmp/\width*100)+"%"
          DrawText(\width/2-TextWidth(tmp$)/2,\height/2-TextHeight(tmp$)/2,tmp$,\textcolor)
          DrawingMode(#PB_2DDrawing_Outlined)
          Box(0,0,\width,\height,0)
          StopDrawing()
          SetGadgetState(\ImageGadget,ImageID(\image))
          ProcedureReturn #True
        EndIf
      EndIf
    EndWith
  EndIf
EndProcedure
Procedure SetProgressbarGadgetOwnState(ProgressbarGadgetOwn.l, state.l)
  If ProgressbarGadgetOwn <= #ProgressbarGadgetOwnMax
    If ProgressBarGadgetOwnData(ProgressbarGadgetOwn)\created
      ProgressBarGadgetOwnData(ProgressbarGadgetOwn)\state = state
      RedrawProgressbarGadgetOwn(ProgressbarGadgetOwn)
      ProcedureReturn #True
    EndIf
  EndIf
EndProcedure
Procedure GetProgressbarGadgetOwnState(ProgressbarGadgetOwn.l)
  If ProgressbarGadgetOwn <= #ProgressbarGadgetOwnMax
    If ProgressBarGadgetOwnData(ProgressbarGadgetOwn)\created
      ProcedureReturn ProgressBarGadgetOwnData(ProgressbarGadgetOwn)\state
    EndIf
  EndIf
EndProcedure
Procedure SetProgressbarGadgetOwnAttribute(ProgressbarGadgetOwn.l,Attribute.l,Value.l)
  If ProgressbarGadgetOwn <= #ProgressbarGadgetOwnMax
    With ProgressBarGadgetOwnData(ProgressbarGadgetOwn)
      If \created
        If Attribute = #PB_ProgressBar_Maximum
          \Maximum = Value
          ProcedureReturn #True 
        ElseIf Attribute = #PB_ProgressBar_Minimum
          \Minimum = Value
          ProcedureReturn #True 
        EndIf
      EndIf
    EndWith 
  EndIf
EndProcedure
Procedure GetProgressbarGadgetOwnAttribute(ProgressbarGadgetOwn.l,Attribute.l)
  If ProgressbarGadgetOwn <= #ProgressbarGadgetOwnMax
    With ProgressBarGadgetOwnData(ProgressbarGadgetOwn)
      If \created
        If Attribute = #PB_ProgressBar_Maximum
          ProcedureReturn \Maximum
        ElseIf Attribute = #PB_ProgressBar_Minimum
          ProcedureReturn \Minimum
        EndIf
      EndIf
    EndWith 
  EndIf
EndProcedure
Procedure ResizeProgressbarGadgetOwn(ProgressbarGadgetOwn.l,x.l,y.l,width.l,height.l)
  If ProgressbarGadgetOwn <= #ProgressbarGadgetOwnMax
    With ProgressBarGadgetOwnData(ProgressbarGadgetOwn)
      If \created
        If x <> #PB_Ignore
          \x = x
        EndIf
        If y <> #PB_Ignore
          \y = y
        EndIf
        If width <> #PB_Ignore
          \width = width
        EndIf
        If height <> #PB_Ignore
          \height = height
        EndIf
        ResizeImage(\image,\width,\height,#PB_Image_Smooth)
        ResizeGadget(\ImageGadget,\x,\y,\width,\height)
        RedrawProgressbarGadgetOwn(ProgressbarGadgetOwn)
        ProcedureReturn #True
      EndIf
    EndWith
  EndIf
EndProcedure
Procedure ProgressBarGadgetOwn(ProgressbarGadgetOwn.l, x.l, y.l, width.l, height.l, Minimum.l, Maximum.l, backgroundcolor.l=$FFFFFF, frontcolor.l=16711680, ShowPercent.l= 0, textcolor.l=0)
  Protected image.l, Gadget.l, i.l
  If ProgressbarGadgetOwn = #PB_Any
    For i = 0 To #ProgressbarGadgetOwnMax
      If ProgressBarGadgetOwnData(i)\created = 0
        ProgressbarGadgetOwn = i
        Break
      EndIf
    Next
  EndIf
  image = CreateImage(-1, width, height, 32)
  If image
    Gadget.l = ImageGadget(-1, x, y, width, height, ImageID(image))
    If Gadget
      If ProgressbarGadgetOwn <= #ProgressbarGadgetOwnMax
        With ProgressBarGadgetOwnData(ProgressbarGadgetOwn)
          \created          = 1
          \ImageGadget      = Gadget
          \image            = image
          \x                = x
          \y                = y
          \width            = width
          \height           = height
          \Minimum          = Maximum
          \Maximum          = Minimum
          \backgroundcolor  = backgroundcolor
          \frontcolor       = frontcolor
          \ShowPercent      = ShowPercent
          \textcolor        = textcolor
          \state            = 0
        EndWith
        RedrawProgressbarGadgetOwn(ProgressbarGadgetOwn)
        ProcedureReturn ProgressbarGadgetOwn
      EndIf
    EndIf
  EndIf
EndProcedure
Procedure FreeProgressbarGadgetOwn(ProgressbarGadgetOwn.l)
  If ProgressbarGadgetOwn <= #ProgressbarGadgetOwnMax
    With ProgressBarGadgetOwnData(ProgressbarGadgetOwn)
      If \created
        FreeImage(\image)
        FreeGadget(\ImageGadget)
        \created          = 0
        \ImageGadget      = 0
        \image            = 0
        \x                = 0
        \y                = 0
        \width            = 0
        \height           = 0
        \Minimum          = 0
        \Maximum          = 0
        \backgroundcolor  = 0
        \frontcolor       = 0
        \state            = 0
        ProcedureReturn #True
      EndIf
    EndWith 
  EndIf
EndProcedure

Procedure test(i)
  For i = 0 To 100
    SetProgressbarGadgetOwnState(0,i)
    If i = 20
      ResizeProgressbarGadgetOwn(0,50,50,200,#PB_Ignore)
    EndIf
    If i = 70
      ResizeProgressbarGadgetOwn(0,80,#PB_Ignore,500,20)
    EndIf
    Delay(100)
  Next
EndProcedure

OpenWindow(0,0,0,800,600,"")
CreateGadgetList(WindowID(0))
ProgressBarGadgetOwn(0,5,5,500,20,0,100,$FFFFFF,$FF0000,1)
CreateThread(@test(),0)
While WaitWindowEvent() <> 16 : Wend 
Zuletzt geändert von a14xerus am 03.06.2007 16:17, insgesamt 6-mal geändert.
Benutzeravatar
ts-soft
Beiträge: 22292
Registriert: 08.09.2004 00:57
Computerausstattung: Mainboard: MSI 970A-G43
CPU: AMD FX-6300 Six-Core Processor
GraKa: GeForce GTX 750 Ti, 2 GB
Memory: 16 GB DDR3-1600 - Dual Channel
Wohnort: Berlin

Beitrag von ts-soft »

:allright: funktioniert gut, wenn man den Democode leicht ändert,
läuft es auch unter Linux:

Code: Alles auswählen

Procedure test(i)
  For i = 0 To 1000
    SetProgressbarGadgetOwnState(0,i)
    Delay(10)
  Next
  ;SendMessage_(WindowID(0),#PB_Event_CloseWindow,0,0)
EndProcedure

OpenWindow(0,0,0,800,600,"")
CreateGadgetList(WindowID(0))
ProgressBarGadgetOwn(0,5,5,500,20,0,1000)
CreateThread(@test(),0)
While WaitWindowEvent(100) <> #PB_Event_CloseWindow : Wend
PureBasic 5.73 LTS | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Nutella hat nur sehr wenig Vitamine. Deswegen muss man davon relativ viel essen.
Bild
a14xerus
Beiträge: 1440
Registriert: 14.12.2005 15:51
Wohnort: Aachen

Beitrag von a14xerus »

Oh, das hatte ich ganz übersehen, war noch von meinen Testdurchläufen drin ;)

Ich habe noch eine Resizefunktion und #PB_any unterstützung dazugetan.
Wer lust hat kann ja auch noch weitere Funktionen hinzufügen, vll mach ich auch nochetwas..
Das Prinzip sollte man ja leicht erkennen
Antworten