Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do a proper owner-draw of Full Row Selected TDBGrid when TDBGrid.DefaultDrawing is false?

Tags:

delphi

vcl

dbgrid

When you have a TDBGrid, full row selection, and always show selection, even when not focused, and you want to fully owner-draw it, you have a choice of a deprecated event OnDrawDataCell , and a new event DrawColumnCell, I chose the latter and try this:

procedure TDbGridTestForm.mygridDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if  gdSelected in State then begin
      //      mygrid.DrawCellHighlight(Rect, State, Col, Row);
  mygrid.Canvas.Brush.Color := clHighlight;
  mygrid.Canvas.Font.Color := clHighlightText;
  mygrid.Canvas.FillRect(Rect);
  end;


  mygrid.DefaultDrawColumnCell(Rect, DataCol, Column, State);

end;

And what is driving me crazy is that focus indication (the highlight color and focus rectangle) are not drawn by the DefaultDrawColumnCell code, and I'm sure I should be invoking DrawCellHighlight instead of doing the FillRect hack I'm doing above.

If I turn on DefaultDrawing, I get one set of glitches (odd font painting issues) and if I turn it off, I get another set of glitches (no highlight even when gdSelected).

The code in DBGrids.pas DefaultDrawColumnCell does nothing other than paint the text. So clearly, you're supposed to do more, if you take over all the drawing code yourself. However, the highlight drawing code built into Grids.pas, upon which DBGrids.pas depends, is not designed to be invoked from this context. I can't figure out if I should be invoking DrawCellHighlight directly (shown commented out above), and figuring out Col and Row values, or if I should be writing my own complex version of TCustomGrid.DrawCellHighlight that handles all the various cases manually.

Surely this is so simple, and obvious, and I'm just overlooking it. NOte that I must leave DefaultDrawing off, and so I must completely paint from within my owner draw event, and that I must be able to run when theme services are not available, and I must use theme services when they are available. So I need to invoke TCustomGrid.DrawCellHighlight directly (and I don't know how to), or I need to reimplement it entirely.

like image 817
Warren P Avatar asked Feb 27 '12 16:02

Warren P


1 Answers

You're better off invoking DrawCellHighlight due to the theme support that you'd need to implement yourself. Even though the row number is not provided to OnDrawColumnCell it doesn't look like it's even used by the DefaultDrawColumnCell code so you don't have to try to calculate it internally:

type
  tHackGrid = class(tDBGrid);

procedure TTDbGridTestForm.myGridDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if gdSelected in State then begin
    tHackGrid(mygrid).DrawCellHighlight(Rect, State, Column.Index, 0);
  end;
  mygrid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
like image 116
Mike W Avatar answered Nov 15 '22 10:11

Mike W