0

How can I draw a progress bar or gif animation in a grid cell ?

Thanks !

Rick
  • 1,301
  • 1
  • 16
  • 28
  • 8
    Which grid component are you using? – RRUZ Mar 01 '11 at 01:08
  • 1
    +1 RRUZ. @Roderick, there are dozens (if not hundreds) of possible grids. In Delphi alone, there are TStringGrid, TDBGrid, and TDrawGrid. Also, you don't give enough information about what you want to do - does the progress bar work for a single cell, or the entire grid, or...??? Your question is entirely too vague to even guess at an answer. Please rephrase and provide enough detail to make it an actual question, or it will most likely be closed as "Not a real question". – Ken White Mar 01 '11 at 01:34
  • Your question has two parts. (1) how to custom draw in a grid. You may find many interesting articles around, like this one http://delphi.about.com/od/vclusing/l/aa072203a.htm . Or google for yourself, if you're interested ofc. and (2) how to draw a progress bar. David has already posted a method. Try for yourself the techniques exposed there and if you still have questions or doubts, please come back with a more precise query. – PA. Mar 01 '11 at 09:06
  • 1
    you're SO member for almost two years!, I think it's time to read the [FAQ](http://stackoverflow.com/faq) and write a elaborate question! – jachguate Mar 01 '11 at 22:44

1 Answers1

4

Here's some code I use to draw a progress bar in a status bar panel:

  R := Rect;
  R.Right := MulDiv(Width, FProgressPercent, 100);
  inc(R.Right, R.Left);
  (* We prefer to draw our inline progress bar using the prevailing theme.  However, the theme API
     uses the wrong colour on XP so we only do this for Vista / Server 2008 and later. *)
  if (Win32MajorVersion>=6) and ThemeServices.ThemesEnabled then begin
    Details.Element := teProgress;
    Details.Part := PP_FILL;
    Details.State := PBFS_NORMAL;
    ThemeServices.DrawElement(Canvas.Handle, Details, R);
  end else begin
    Canvas.Brush.Color := clHighlight;
    Canvas.Brush.Style := bsSolid;
    Canvas.FillRect(R);
  end;

This code runs in an OnDrawPanel event handler, but you'd want to use something like the OnDrawCell event for a grid. Rect is the client area of the status bar panel in my code, you'd want the entire grid cell for your code.

I also draw a percentage text over the top by running this code after the code above.

  Text := Format('%d%%', [FProgressPercent]);
  Size := Canvas.TextExtent(Text);
  Left := Rect.Left+(Width-Size.cx) div 2;
  Top := Rect.Top+(Height-Size.cy) div 2;
  Canvas.Brush.Style := bsClear;
  Canvas.Font.Color := clHighlightText;
  Canvas.Font.Style := [fsBold];
  Canvas.TextRect(Rect, Left, Top, Text);

Not exactly the same as you want to do, but hopefully the ideas will carry across.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490