Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Flotantes en campo DBGridEh mediante evento (https://www.clubdelphi.com/foros/showthread.php?t=88890)

wilcg 21-08-2015 01:38:07

Flotantes en campo DBGridEh mediante evento
 
Amigos del foro tengo este codigo en el evento OnDrawColumnCell de un DBGridEh por corregir algunos puntos.
Código Delphi [-]
procedure DBGridEhDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumnEh; State: TGridDrawState);
const
  // Windwos classic
  CtrlState: Array[Boolean] of Integer = (DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or
  DFCS_CHECKED);
  // Windows xp
  CtrlStateXP: Array [Boolean] of TThemedButton = (tbCheckBoxUncheckedNormal,
  tbCheckBoxCheckedNormal);
var
  R      : TRect;
  uFormat: LongWord;
  Details: TThemedElementDetails;
begin
  uFormat := DT_SINGLELINE or DT_VCENTER or DT_END_ELLIPSIS;
  case Column.Alignment of
    taLeftJustify : uFormat := uFormat or DT_LEFT;
    taRightJustify: uFormat := uFormat or DT_RIGHT;
    taCenter      : uFormat := uFormat or DT_CENTER;
  end;

  with TDBGridEh(Sender) do
  begin
    if Datasource.DataSet.RecNo mod 2 = 0 then
      Canvas.Brush.Color := $00F5F5F5
    else
      Canvas.Brush.Color := clWhite;
    Canvas.Font.Color := $00606060;

    if gdSelected in State then
   begin
     Canvas.Brush.Color := $00A6E8FF;
     Canvas.Font.Color := clHotLight;
     Canvas.Font.Name := 'Tahoma';
     Canvas.Font.Size := 9;
   end ;
    DefaultDrawColumnCell(rect,DataCol,Column,State);
    R := Rect;
   Canvas.FillRect(R);
   DrawText(Canvas.Handle, @Column.Field.AsString[1], -1, R, uFormat);
   // Campo Booleano
   if Column.Field.DataType = ftBoolean then
   begin
     Canvas.FillRect(Rect);
     if ThemeServices.ThemesEnabled then
     begin
       Details := ThemeServices.GetElementDetails(CtrlStateXP[Column.Field.AsBoolean]);
       ThemeServices.DrawElement(Canvas.Handle, Details, Rect);
     end else
     begin
       R.Left   := Rect.Left + 2;
       R.Right  := Rect.Right - 2;
       R.Top    := Rect.Top + 2;
       R.Bottom := Rect.Bottom - 2;
       DrawFrameControl(Canvas.Handle, R, DFC_BUTTON, CtrlState[Column.Field.AsBoolean]);
     end;
   end;
  end;
end;
Lo que hace es:
Primero ajusta el texto del campo a su longitud y muestra 3 puntitos
Segundo Dibuja un checkbox en cada campo booleano.

luego viene lo que quiero, es que formatee un campo numerico ejemplo S/. 123.00 lo he estado
haciendo en la propiedad del DisplayFormat del campo, aparentemente funciona sin el codigo. Luego con
el codigo en el evento OnDrawColumnCell del DBGridEh no funciona.
espero que me hayan entendido y me puedan ayudar.

ecfisa 21-08-2015 04:10:12

Hola wilcg.

Como creo haberte comentado no he usado el componente DBGridEh, pero te pongo un ejemplo con un TDBGrid haciendo el efecto de colores y formato, que creo es el que buscas. Tal vez puedas sacar alguna utilidad del código.
Código Delphi [-]
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
const
  CELCOL: array[Boolean] of TColor = ($00F5F5F5, $00606060);
var
  R      : TRect;
  uFormat: LongWord;
  G      : TDBGrid;
  currFld: string;
begin
  G := TDBGrid(Sender);
  R := Rect;
  uFormat := DT_SINGLELINE or DT_VCENTER or DT_END_ELLIPSIS;

  case Column.Alignment of
    taLeftJustify : uFormat := uFormat or DT_LEFT;
    taRightJustify: uFormat := uFormat or DT_RIGHT;
    taCenter      : uFormat := uFormat or DT_CENTER;
  end;

  G.Canvas.Brush.Color := CELCOL[Odd(G.DataSource.DataSet.RecNo)];
  G.Canvas.FillRect(Rect);

  if gdSelected in State then
  begin
    G.Canvas.Brush.Color := $00A6E8FF;
    G.Canvas.Font.Color  := clHotLight;
    G.Canvas.Font.Name   := 'Tahoma';
    G.Canvas.Font.Size   := 9;
  end;

  currFld := Column.Field.AsString;
  if Column.Field.DataType in [ftFloat, ftCurrency, ftBCD] then
    currFld:= FormatFloat('S/. 0.00', Column.Field.Value);

  DrawText(G.Canvas.Handle, PChar(currFld), -1, R, uFormat);
end;

Salida:


Saludos :)

wilcg 21-08-2015 17:57:35

Cita:

Empezado por ecfisa (Mensaje 495720)
Hola wilcg.

Como creo haberte comentado no he usado el componente DBGridEh, pero te pongo un ejemplo con un TDBGrid haciendo el efecto de colores y formato, que creo es el que buscas. Tal vez puedas sacar alguna utilidad del código.

hola ecfisa, disculpa la tardanza, efectivamente tengo muchos aportes tuyos y gracias por darnos una manito cuando lo necesitamos pasando al tema, lo que sucede que este código funciona bien mayormente en los campos flotantes pero en otros aspectos me cambia por completo la imagen visual que quiero mostrar del DBGridEh, en los campos booleano no me dibuja un checkbox solo me muestra el texto False o True y ademas tengo campos decimales y enteros que los formatea a uno solo.

esta imagen corresponde al dbgrideh usando la configuración de sus propiedades, al usar el código cambia totalmente ya no prevalece la configuración.



Lo que quiero es encontrar una manera que el código respete las propiedades configuradas en el dbgridEh especialmente el DisplayFormat para los campos numericos. espero puedas mostrarme un ejemplo tal como me mencionastes en este hilo.
http://www.clubdelphi.com/foros/showthread.php?p=493719

wilcg 22-08-2015 00:52:46

Después de tanto intentar encontré la solución, he usado una parte del código de ecfisa y cambiado el campo de mi tabla a Integer y ya queda tal como en la imagen. Gracias ecfisa por tu ayuda.
Código Delphi [-]
currFld := Column.Field.AsString;
   if Column.Field.DataType in [ftFloat, ftCurrency, ftBCD] then
     currFld:= FormatFloat('###,##0.00', Column.Field.Value);

   if Column.Field.DataType in [ftInteger] then
     currFld:= FormatFloat('###,##0.0000', Column.Field.Value);

   DrawText(Canvas.Handle, PChar(currFld), -1, R, uFormat);


La franja horaria es GMT +2. Ahora son las 18:37:04.

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