File "TextProgressBar.inc"

Path: /ComicDir2Cbr/inc/TextProgressBar.inc
File size: 4.01 KB
MIME-type:
Charset: utf-8

'====================================================================
' Very simple liteweight custom-colored progress bar with text
' Usage:
'  ADD_TEXT_PROGRESSBAR hDlg, %IDC_PROGRESSBAR, 4, 30, 192, 20
'  SET_TEXT_PROGRESSBAR hDlg, %IDC_PROGRESSBAR, 25, "Progress is 25%" [,%RGB_DARKGREEN ,%GREEN [,%WHITE ,%BLACK]]
'====================================================================
FUNCTION TPBCOLOR(BYVAL co AS BYTE) AS WORD
  FUNCTION = VAL("&H" + HEX$(co) + "00")
END FUNCTION
'====================================================================
SUB ADD_TEXT_PROGRESSBAR(hDlg AS DWORD, CtlId AS LONG, _
  x AS LONG, y AS LONG, w AS LONG, h AS LONG)
  CONTROL ADD GRAPHIC, hDlg, CtlId, "", x, y, w, h, %SS_SUNKEN
END SUB
'====================================================================
SUB SET_TEXT_PROGRESSBAR (BYVAL hDlg AS DWORD, BYVAL CtlId AS LONG, _
                          pct AS LONG, tx AS STRING, OPTIONAL _        ' all 4 following parameters are optional
                          BYVAL barcol1 AS LONG, BYVAL barcol2 AS LONG, _
                          BYVAL txtcol1 AS LONG, BYVAL txtcol2 AS LONG)
'--------------------------------------------------------------------
  STATIC bco1, bco2, tco1, tco2 AS LONG ' colors (static)
  LOCAL gRect AS GRADIENT_RECT
  LOCAL hDC, hCtrl AS DWORD
  DIM vert(3) AS TRIVERTEX
  LOCAL x, y, w, h AS LONG
  STATIC hFont AS DWORD ' font (static)
  LOCAL rc AS RECT

  ' First call
  IF hFont = 0 THEN FONT NEW "Lucida Console", 10, 0, %ANSI_CHARSET TO hFont

  ' Define bar colors
  IF ISFALSE (barcol1 OR barcol2 OR bco1 OR bco2) THEN
    bco1 = %RGB_DARKGREEN
    bco2 = %GREEN
  ELSEIF ISTRUE (barcol1 OR barcol2) THEN
    bco1 = barcol1
    bco2 = barcol2
  END IF

  ' Define text colors
  IF ISFALSE (txtcol1 OR txtcol2 OR tco1 OR tco2) THEN
    tco1 = %WHITE
    tco2 = %BLACK
  ELSEIF ISTRUE (txtcol1 OR txtcol2) THEN
    tco1 = txtcol1
    tco2 = txtcol2
  END IF

  ' Attach the custom progressbar handle
  CONTROL HANDLE hDlg, CtlId TO hCtrl
  GetClientRect hCtrl, rc
  GRAPHIC ATTACH hDlg, CtlId, REDRAW
  GRAPHIC GET DC TO hDC

  ' Define top-left and bottom-right trivertexes for first gradient (top half of the progress bar)
  vert(0).x      = 0
  vert(0).y      = 0
  vert(0).Red    = TPBCOLOR(GetRValue(bco1))
  vert(0).Green  = TPBCOLOR(GetGValue(bco1))
  vert(0).Blue   = TPBCOLOR(GetBValue(bco1))
  vert(0).Alpha  = &H0000

  vert(1).x      = rc.nRight * MIN(MAX(pct,0),100) \ 100
  vert(1).y      = rc.nBottom \ 2
  vert(1).Red    = TPBCOLOR(GetRValue(bco2))
  vert(1).Green  = TPBCOLOR(GetGValue(bco2))
  vert(1).Blue   = TPBCOLOR(GetBValue(bco2))
  vert(1).Alpha  = &H0000

  ' Define top-left and bottom-right trivertexes for second gradient (bottom half of the progress bar)
  vert(2).x      = 0
  vert(2).y      = rc.nBottom \ 2
  vert(2).Red    = TPBCOLOR(GetRValue(bco2))
  vert(2).Green  = TPBCOLOR(GetGValue(bco2))
  vert(2).Blue   = TPBCOLOR(GetBValue(bco2))
  vert(2).Alpha  = &H0000

  vert(3).x      = rc.nRight * MIN(MAX(pct,0),100) \ 100
  vert(3).y      = rc.nBottom
  vert(3).Red    = TPBCOLOR(GetRValue(bco1))
  vert(3).Green  = TPBCOLOR(GetGValue(bco1))
  vert(3).Blue   = TPBCOLOR(GetBValue(bco1))
  vert(3).Alpha  = &H0000

  gRect.UpperLeft  = 0
  gRect.LowerRight = 1

  GRAPHIC COLOR -1, -1 : GRAPHIC CLEAR

  ' Set text font and calculate its dimensions
  GRAPHIC SET FONT hFont
  GRAPHIC TEXT SIZE tx TO w, h
  x = (rc.nRight - w) \ 2
  y = (rc.nBottom - h) \ 2

  ' Draw text under the progress bar (in black)
  GRAPHIC COLOR tco2, -2
  GRAPHIC SET POS (x, y) : GRAPHIC PRINT tx

  ' Draw the progress bar
  GradientFill hDC, vert(0), 2, gRect, 1, %GRADIENT_FILL_RECT_V
  GradientFill hDC, vert(2), 2, gRect, 1, %GRADIENT_FILL_RECT_V

  ' Draw text on the progress bar (in white), limited to the progressbar rect
  IF rc.nRight * MIN(MAX(pct,0),100) \ 100 >= x THEN
      GRAPHIC SET POS (x, y)
      GRAPHIC COLOR tco1, -2
      rc.nRight  = rc.nRight * MIN(MAX(pct,0),100) \ 100
      DrawText hDC, (tx), -1, rc, %DT_SINGLELINE OR %DT_CENTER OR %DT_VCENTER
  END IF

  GRAPHIC REDRAW
END SUB