1

この質問とその非常に役立つ回答に続いて、ケンの回答のバリエーションを大きなプロジェクトに実装しました。そこでは、同様のコードが多くの形式で表示されていました。しかし、次のような DrawColumnCell ハンドラーを持つフォームもいくつかあることに気付きました

procedure TEditDocket.DBGrid1DrawColumnCell(Sender: TObject;
           const Rect: TRect; DataCol: Integer; Column: TColumn;
           State: TGridDrawState);
var
 DrawRect: TRect;

begin
 if column.Index  = 3 then
  begin
   DrawRect:= Rect;
   drawrect.left:= rect.left + 24;
   InflateRect (DrawRect, -1, -1);
   dbgrid1.Canvas.FillRect (Rect);
   DrawFrameControl (dbgrid1.Canvas.Handle, DrawRect, DFC_BUTTON,
                 ISChecked[Column.Field.AsInteger]);
  end
 else dbgrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State);
end;

上記のコードを一般的な OnDrawColumnCell ハンドラとどのように組み合わせることができますか? 追加のパラメーター (列インデックスになります。-1 の場合、上記のコードは実行されません) を使用してジェネラル ハンドラーを定義することは可能ですか? そのようなパラメーターをハンドラーに渡すにはどうすればよいですか?

4

3 に答える 3

1

(コメント チェーンから) ジェネリックを呼び出すか、(場合によっては) チェックボックス描画のコードも呼び出したいことがわかります。

汎用の 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
于 2014-02-28T16:20:55.897 に答える