☆DBGridのDrawCell
カスタムコンポーネントでのDrawCellの一例です。
今回は次のような描画をしてみます。
今回は次のような描画をしてみます。
1.Excelのように選択セルの行(インジゲーター)と列(タイトル)部分に色を付ける。 2.InplaceEditorの背景に色を付ける。
※フォームやツールバーは、Delphi標準のコンポーネントではなく、TMS Software製TMS ToolBar Application Wizardから新規に作成したままの状態です。
type TDummyEditor = class(TCustomEdit); TMyDBGrid = class(TDBGrid) private procedure WMCommand(var Message: TWMCommand); message WM_COMMAND; protected procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override; procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override; procedure KeyDown(var Key: Word; Shift: TShiftState); override; end;
uses GraphUtil; { TMyDBGrid } const ActiveColor = $009FEBFD; ActiveColorTo = $0056B4FE; BackColor = $00FDEADA; BackColorTo = $00E4AE88; EditColor = $00D3F8FF; var Old_SelectedField: Integer; procedure DrawBackground(ACanvas: TCanvas; ARect: TRect; Focused: Boolean); begin if Focused then GradientFillCanvas(ACanvas, ActiveColor, ActiveColorTo, ARect, gdVertical) else GradientFillCanvas(ACanvas, BackColor, BackColorTo, ARect, gdVertical); end; procedure TMyDBGrid.DrawCell(ACol, ARow: Integer; ARect: TRect; AState: TGridDrawState); const AlignFlags : array [TAlignment] of Integer = (DT_LEFT, DT_RIGHT, DT_CENTER); var Text: String; Flags: LongInt; Check: Boolean; begin inherited DrawCell(ACol,ARow,ARect,AState); InflateRect(ARect, 1, 1); if (ACol = 0) then begin Check := (ARow >= 0) and (ARow-1 = Datalink.ActiveRecord); DrawBackground(Canvas, ARect, Check); end else if (ARow = 0) then begin // 背景の描画 SetBkMode(Canvas.Handle, TRANSPARENT); Check := (SelectedIndex = ACol- 1); DrawBackground(Canvas, ARect, Check); // カラム間の線 Canvas.Pen.Color := clSilver; Canvas.MoveTo(ARect.Left, ARect.Top+1); Canvas.LineTo(ARect.Left, ARect.Bottom-1); // タイトルの描画 Canvas.Font.Assign(TitleFont); Text := Columns[ACol-1].Title.Caption; Flags := DT_SINGLELINE or DT_VCENTER or AlignFlags[Columns[ACol-1].Title.Alignment]; InflateRect(ARect, -2, -2); DrawText(Canvas.Handle, PChar(Text), Length(Text), ARect, Flags); end; end; procedure TMyDBGrid.KeyDown(var Key: Word; Shift: TShiftState); begin inherited KeyDown(Key, Shift); // 選択フィールドのカラムに色をつける処理 if SelectedField.Index <> Old_SelectedField then begin Old_SelectedField := SelectedField.Index; Invalidate; end; end; procedure TMyDBGrid.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin inherited MouseDown(Button, Shift, X, Y); // 選択フィールドのカラムに色をつける処理 if SelectedField.Index <> Old_SelectedField then begin Old_SelectedField := SelectedField.Index; Invalidate; end; end; // InplaceEditorの背景色を設定する。 procedure TMyDBGrid.WMCommand(var Message: TWMCommand); begin with Message do begin if (InplaceEditor <> nil) and (Ctl = InplaceEditor.Handle) then begin if TDummyEditor(InplaceEditor).Color <> EditColor then TDummyEditor(InplaceEditor).Color := EditColor; end; end; inherited; end;
| 固定リンク