(コメント チェーンから) ジェネリックを呼び出すか、(場合によっては) チェックボックス描画のコードも呼び出したいことがわかります。
汎用の 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 に対して行ったこのコメントを見た後:
1 つのグリッドでは、チェックボックスとして描画する必要があるのは列 4 であるのに対し、列 3 である可能性があります。
上記のコードで、これに対処する 1 つの方法を提供しました ( のコメントを参照してくださいGenericDrawColumnCell
)。TDBGrid.Tag
別の方法 (各グリッドにチェックボックスが必要な列が 1 つしかない場合) は、プロパティでチェックボックスを含む列を示すことです。
if (Column.Index = Grid.Tag) then