DBGrid - desenhando uma imagem tipo checkbox em uma coluna da grid e clicando para marcar mesmo com rowselect

Top  Previous  Next

 

 

  object Image1: TImage [0// checked

    Left = 12

    Top = 164

    Width = 16

    Height = 16

    AutoSize = True

    Center = True

    Picture.Data = {

      07544269746D6170F6000000424DF60000000000000076000000280000001000

      0000100000000100040000000000800000000000000000000000100000000000

      0000000000000000800000800000008080008000000080008000808000008080

      8000C0C0C0000000FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFF

      FF0088888888888888888888888888888888866666666666668886FFFFFFFFFF

      F68886FFFFFFFFFFF68886FFFF2FFFFFF68886FFF222FFFFF68886FF22222FFF

      F68886FF22F222FFF68886FF2FFF222FF68886FFFFFFF22FF68886FFFFFFFF2F

      F68886FFFFFFFFFFF68886FFFFFFFFFFF6888666666666666688888888888888

      8888}

    Visible = False

  end

  object Image2: TImage [1// unchecked

    Left = 28

    Top = 164

    Width = 16

    Height = 16

    AutoSize = True

    Center = True

    Picture.Data = {

      07544269746D6170F6000000424DF60000000000000076000000280000001000

      0000100000000100040000000000800000000000000000000000100000000000

      0000000000000000800000800000008080008000000080008000808000008080

      8000C0C0C0000000FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFF

      FF0088888888888888888888888888888888866666666666668886FFFFFFFFFF

      F68886FFFFFFFFFFF68886FFFFFFFFFFF68886FFFFFFFFFFF68886FFFFFFFFFF

      F68886FFFFFFFFFFF68886FFFFFFFFFFF68886FFFFFFFFFFF68886FFFFFFFFFF

      F68886FFFFFFFFFFF68886FFFFFFFFFFF6888666666666666688888888888888

      8888}

    Visible = False

  end

 

 

// desenhando o check

procedure TMainFotoForm.GridDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);

var

  PosX: Integer;

  Cor : TColor;

begin

  if Column.Field = TbMarca then

  begin

    PosX := Rect.Left + (Column.Width div 2) - (Image1.Picture.Bitmap.Width div 2);

    if (gdSelected in State) then Cor := clHighlight else Cor := clWindow;

    Grid.Canvas.FillRect(Rect);

    if TbMarca.AsBoolean then

      DrawTransparent(PosX, Rect.Top + 1, Image1.Picture.Bitmap, Grid.Canvas, Cor)

    else

      DrawTransparent(PosX, Rect.Top + 1, Image2.Picture.Bitmap, Grid.Canvas, Cor);

  end;

end;

 

// para marcar o check quando o rowselect = true

 

function OnColumn(const Grd: TDBGrid; const Campo: TField): Boolean;

var

  Pt  : TPoint;

  W, I: Integer;

begin

  GetCursorPos(Pt);

  Windows.ScreenToClient(GetParentForm(Grd).Handle, Pt); // Converte para a posicao relativa ao form

  W := Grd.Left;

  for I := 0 to Grd.Columns.Count-1 do

    if Grd.Columns[I].Field = Campo then

      Break

    else

      W := W + Grd.Columns[I].Width;

  Result := (Pt.X >= W) and (Pt.X <= W + Grd.Columns[I].Width);

end;

 

procedure TMainFotoForm.GridCellClick(Column: TColumn);

begin

  if OnColumn(Grid, TbMarca) then GridDblClick(Grid);

end;