Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   ProgressBar en un celda de DBGrid (https://www.clubdelphi.com/foros/showthread.php?t=54573)

MaMu 24-03-2008 09:06:44

ProgressBar en un celda de DBGrid
 
Es posible dibujar un ProgressBar en un celda de DBGrid?, y me refiero al DBGrid standard de Delphi. (Esta el Next Grid que si lo permite y es freeware).

He visto formas de dibujarlo en un StatusBar, pero quiciera saber si se puede en un DBGrid, para visualizar campos integer.

Saludos

MaMu 24-03-2008 09:21:08

Bueno, me voy a autoresponer, porque encontré un artículo interesante "googleando" por otros pagos:

Usando el evento OnDrawColumnCell, se pueden utilizar estas rutinas genéricas para pintar un ProgressBar en cualquier TCanvas (El segundo permite pintar un texto entero,y el primero, además una figura opcional del porcentaje). Esta rutina invierte el color del texto de modo que el texto sea siempre legible:


Código Delphi [-]
procedure PaintProgressBar(Canvas: TCanvas; const Rect: TRect; Progress: 
integer; PaintNumber: boolean = true; BarColor: TColor = clBlue; BackColor: 
TColor = clWhite); overload; 
  procedure PaintProgressBar(Canvas: TCanvas; const Rect: TRect; Progress: 
integer; const Text: string; TextAlign: TAlignment; BarColor: TColor = 
clBlue; BackColor: TColor = clWhite); overload; 


implementation 

procedure SetClipRect(Canvas: TCanvas; R: PRect); 
var 
  Rgn    : HRGN; 
begin 
  if R = nil then 
    SelectClipRgn(Canvas.Handle, 0) 
  else begin 
    With Canvas do begin 
      Rgn:=CreateRectRgn(R.Left, R.Top, R.Right, R.Bottom); 
      try 
        SelectClipRgn(Canvas.Handle, Rgn); 
      finally 
        DeleteObject(Rgn); 
      end; 
    end; 
  end; 
end; 

procedure PaintProgressBar(Canvas: TCanvas; const Rect: TRect; Progress: 
integer; PaintNumber: boolean = true; BarColor: TColor = clBlue; BackColor: 
TColor = clWhite); 
begin 
  if Progress = -1 then 
    PaintProgressBar(Canvas, Rect, Progress, '', taCenter, BarColor, 
BackColor) 
  else begin 
    if PaintNumber then 
      PaintProgressBar(Canvas, Rect, Progress, i2s(Progress)+' %', taCenter, 
BarColor, BackColor) 
    else 
      PaintProgressBar(Canvas, Rect, Progress, '', taCenter, BarColor, 
BackColor); 
  end; 
end; 
procedure PaintProgressBar(Canvas: TCanvas; const Rect: TRect; Progress: 
integer; const Text: string; TextAlign: TAlignment; BarColor: TColor = 
clBlue; BackColor: TColor = clWhite); overload; 
var 
  ARect: TRect; 
  R    : TRect; 
  FLAGS: Word; 
begin 
  FLAGS:=0; 
  with Canvas do begin 
    Case TextAlign of 
      taLeftJustify : FLAGS:=DT_LEFT; 
      taRightJustify: FLAGS:=DT_RIGHT; 
      taCenter      : FLAGS:=DT_CENTER; 
    end; 
    FLAGS:=FLAGS or DT_VCENTER or DT_SINGLELINE; 
    ARect:=Rect; 
    InflateRect(ARect, -2, 0); 
    R:=Rect; 
    if Progress <> -1 then begin 
//    InflateRect(R, -1, -1); 
      R.Right:=Round(Progress*(Rect.Right-Rect.Left)/100); 
      Brush.Color:=BarColor; 
      FillRect(R); 
      if Text <> '' then begin 
        SetClipRect(Canvas, @R); 
        Font.Color:=BackColor; 
        DrawText(Handle, PChar(Text), -1, ARect, FLAGS); 
      end; 
      R.Left:=R.Right+1; 
      R.Right:=Rect.Right-1; 
    end; 
    Brush.Color:=BackColor; 
    if Text <> '' then 
      SetClipRect(Canvas, @R); 
    FillRect(R); 
    if Text <> '' then begin 
      Font.Color:=BarColor; 
      DrawText(Handle, PChar(Text), -1, ARect, FLAGS); 
      SetClipRect(Canvas, nil); 
    end; 
  end; 
end;

Y bueno, voy a ver si interpreto como usar todo esto. Lo postié porque me pareció muy interesamte.

Saludos

MaMu 24-03-2008 09:50:45

Bueno, estoy probando, aunque por ahora está demaciado "frutal"

Código Delphi [-]
if (datacol=10)
 then begin
    PaintProgressBar(canvas,rect,10,'10%',taCenter,clBlue,clLime);
 end
 else begin
    defaultdrawing:= true;
 end;

No es ohhh la barra de progreso, pero es una alternativa muy buena.

Saludos

MaMu 24-03-2008 10:33:39

Volviendo al control clásico del ProgressBar, yo puedo hacer lo siguiente

Código Delphi [-]
//OnDrawColumnCell
pb:=TProgressBar.Create(DBGrid1);
with pb do
 begin
   Parent := DBGrid1;
   Height := 16;
   Width := DBGrid1.Columns[10].Width;
   Top := 1;
   Left := 1;
   Position := 13;
   Smooth:=True;
end;

Creo el ProgressBar dentro del DBGrid y todo muy lindo, pero... como la posiciono en una determinada celda?

Saludos

juanlaplata 25-03-2008 13:25:43

mas que en una celda creo que tendrias que pensar a que registro de la tabla se la tendrias que asignar, por que, imagina que logras pintarla en la celda [2,2], siempre sera en esa, sin importar los datos, si en tal caso se borrara el registro 2 la prox. ves el reg.3 estara en la posicion del 2 y con la barra.
Tal ves lo esto lo tienes contemplado y no lo he entendido asi.
Otra cosita, cuando haces el OnDrawColumncell en el encabezado del mismo te viene la columna que se esta pintando, solo deberias preg si es la 10 (segun tu ejemplo), o por el nombre; y si es asi pintar, de lo contrario por cada columna esta pintando la 10. No es problema ni da error, es solo no hacer lo que no queremos.
Suerte y luego verifico este codigo para tenerlo en cuenta en algun proyecto.

MaMu 26-03-2008 20:20:19

Si Juan, es tal cual lo que estoy pensando.


La franja horaria es GMT +2. Ahora son las 05:41:02.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi