我看到(从评论链)你想要调用通用或(在某些情况下)也调用复选框绘图的代码。
通用 OnDrawColumnCell 处理程序处理单元格的背景颜色。如何扩展它以创建一个处理程序,该处理程序也将在需要时绘制复选框?顺便说一句,通用代码必须在复选框代码之前调用,而不是之后。
这其实很简单。使用正确的签名定义这两种方法(通用方法和复选框方法)(未经测试的代码!)。不过,仅将通用事件连接到TDBGrid.OnDrawColumnCell
事件 - 如果需要,将链接复选框:
// Generic (from my other post) - notice method name has changed
procedure TDataModule1.GenericDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
const
RowColors: array[Boolean] of TColor = (clSilver, clDkGray);
var
OddRow: Boolean;
Grid: TDBGrid;
begin
if (Sender is TDBGrid) then
begin
Grid := TDBGrid(Sender);
OddRow := Odd(Grid.DataSource.DataSet.RecNo);
Grid.Canvas.Brush.Color := RowColors[OddRow];
Grid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
// If you want the check box code to only run for a single grid,
// you can add that check here using something like
//
// if (Column.Index = 3) and (Sender = DBGrid1) then
//
if (Column.Index = 3) then //
CheckBoxDrawColumCell(Sender, Rect, DataCol, Column, State)
end;
end;
// Checkbox (from yours) - again, notice method name change.
procedure TEditDocket.CheckBoxDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
DrawRect: TRect;
Grid: TDBGrid;
begin
// Don't use DBGrid1, because it makes the code specific to a single grid.
// If you need it for that, make sure this code only gets called for that
// grid instead in the generic handler; you can then use it for another
// grid later (or a different one) without breaking any code
if column.Index = 3 then
begin
Grid := TDBGrid(Sender);
DrawRect:= Rect;
Drawrect.Left := Rect.Left + 24;
InflateRect (DrawRect, -1, -1);
Grid.Canvas.FillRect (Rect);
DrawFrameControl (Grid.Canvas.Handle, DrawRect, DFC_BUTTON,
ISChecked[Column.Field.AsInteger]); // Don't know what ISChecked is
end;
// The below should no longer be needed, because DefaultDrawColumnCell has
// been called by the generic handler already.
//
// else
// Grid.DefaultDrawColumnCell (Rect, DataCol, Column, State);
end;
看到您对 Sertac 的评论后:
在一个网格中,它可能是第 3 列,而它可能是需要绘制为复选框的第 4 列。
我在上面的代码中提供了一种方法来解决这个问题(请参阅 中的注释GenericDrawColumnCell
)。另一种选择(假设您在每个网格中只有一列需要复选框)是在属性中指示包含复选框的列TDBGrid.Tag
:
if (Column.Index = Grid.Tag) then