使用TStringGrid的OnDrawCell事件绘制进度条

时间:2012-11-02 13:14:20

标签: delphi delphi-2010

  

可能重复:
  Grid progressbar or animation
  Add graphical bar to a StringGrid col

使用Delphi 2010,我有一个包含5列的TStringGrid

ID,开始,结束,持续时间以及在每个单元格中绘制进度条的列。

第5列宽度(例如:60)由选项对话框中的条宽旋转编辑字段设置。

鉴于持续时间是(结束 - 开始)* 1440(例如:0.39分钟),我需要将进度条绘制为总条宽的百分比。 (即39/60 = 65%)因此,条形图应涂在整个单元格的65%。它还需要显示栏中心的百分比。 (海军蓝条和白色文字)。

任何人都可以帮我画这个进度条吗?

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;

1 个答案:

答案 0 :(得分:1)

var
   percent:Double;


procedure DrawTheText(const hDC: HDC; const Font: TFont; var Text: string; aRect:TRect);
var
 lRect:Trect;
begin
  with TBitmap.Create do
    try

      Width := aRect.Right - aRect.Left;
      Height := aRect.Bottom - aRect.Top;
      LRect :=Rect(0,0,width,height);
      Canvas.Font.Assign(Font);
      Canvas.Brush.Color := clBlack;
      Canvas.FillRect(Lrect);
      Canvas.Font.Color := clWhite;
      Canvas.TextRect(Lrect,Text,[tfCenter ,tfVerticalCenter,tfSingleLine]);
      BitBlt(hDC, aRect.Left, aRect.Top, Width, Height, Canvas.Handle, 0, 0, SRCINVERT);
    finally
      Free;
    end;
end;

procedure TForm3.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  LRect:Trect;
  s:String;
  c:TCanvas;
begin
   //.....yout code
      percent := 0.5;//Random(2) / 60;
   //.... case of wished Colum
   c := DrawGrid1.Canvas;
   LRect := Rect;
   LRect.Right := Round(LRect.Left + (LRect.Right - LRect.Left)*percent);
   inflaterect(LRect,-1,-1);
   c.Brush.Color := clNavy;
   c.Brush.Style := bsSolid;
   c.Pen.Color := clBlack;
   C.FillRect(LRect);
   s := FormatFloat('0.00 %' , percent * 100 );
   DrawTheText(c.Handle,DrawGrid1.font,s,rect);
end;