title: Drawing a Progress Bar using TStringGrid's OnDrawCell event
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5149432/grid-progressbar-or-animation">Grid progressbar or animation</a><br>
    <a href="https://stackoverflow.com/questions/13032146/add-graphical-bar-to-a-stringgrid-col">Add graphical bar to a StringGrid col</a>  </p>
  </blockquote>
  
  
  
  <p>Using Delphi 2010, I have a TStringGrid with 5 columns</p>
  
  <p>ID, Start, End, Duration, and a column to draw a progress bar in each cell.</p>
  
  <p>column 5 width (example: 60) is set by the Bar width spin edit field in options dialog. </p>
  
  <p>Given that duration is (end - start) * 1440  (example: 0.39 minutes), I need to draw the progress bar as a percentage of the total bar width. (i.e.  39/60 = 65%) therefore the bar should be painted 65% accross the cell. It also needs to show the percentage centered in the bar. (navy blue bar &amp; white text).</p>
  
  <p>can anyone help me to paint this progress bar ? </p>
  
  <pre><code>procedure Tphasedata.grdMainDrawCell(Sender: TObject; ACol, ARow: Integer;
    Rect: TRect; State: TGridDrawState);
  var
    LStrCell: string;
    LRect: TRect;
  begin
   with (Sender as TStringGrid) do
    begin
      // Don't change color for first Column, first row
      if (ACol = 0) or (ARow = 0) then
        Canvas.Brush.Color := clBtnFace
      else
      begin
        case ACol of
          0: Canvas.Font.Color := clBlack;
          1: Canvas.Font.Color := clBlue;
          2: Canvas.Font.Color := clBlue;
          3: Canvas.Font.Color := clRed;
        end;
        // Draw the Band
        if ARow mod 2 = 0 then
          Canvas.Brush.Color := $00E1FFF9
        else
          Canvas.Brush.Color := $00FFEBDF;
        Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top + 2, cells[acol, arow]);
        Canvas.FrameRect(Rect);
  
        //center the duration text
        if ACol = 3 then
        begin
          LStrCell := Cells[ACol, ARow]; // grab cell text
          Canvas.FillRect(Rect); // clear the cell
          LRect := Rect;
          LRect.Top := LRect.Top + 3; // adjust top to center vertical
          // draw text
          DrawText(Canvas.Handle, PChar(LStrCell), Length(LStrCell), LRect, DT_CENTER);
        end;
  
       i ACol = 4 then
       begin
        // draw progress bar here 
       end; 
      end;
    end;
  </code></pre>