Announcement

Collapse
No announcement yet.

DBGrid selber zeichnen

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • DBGrid selber zeichnen

    Hallo alle zusammen,

    anbei meinen Source zum selber zeichnen des Gridinhaltes mit dem Ereigniss OnDrawColumnCell.

    Das Zeichnen einer gesamten Zeile (dgRowSelect, dgAlwaysShowSelection, dgMultiSelect), welches im Normalfall vom Grid erledigt wird ist bei Verwendung von OnDrawColumnCell mit dem normalen Grid nicht möglich, da die benötigten Propertys dazu private oder protected sind.

    Deswegen habe ich die Komponente TdmDBGrid von TDBGrid abgeleitet und um die benötigten Propertys erweitert.

    type
    TdmDBGrid = class(TDBGrid)
    protected
    iDrawCol : Longint;
    iDrawRow : Longint;
    procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
    public
    constructor Create(AOwner: TComponent); override;
    property Col;
    property Row;
    property DrawCol : Longint read iDrawCol;
    property DrawRow : Longint read iDrawRow;
    end;

    constructor TdmDBGrid.Create(AOwner: TComponent);
    begin
    iDrawCol := -1;
    iDrawRow := -1;

    inherited;
    end;
    //------------------------------------------------------------------------------

    procedure TdmDBGrid.DrawCell(ACol, ARow: Integer; ARect: TRect; AState: TGridDrawState);
    begin
    iDrawCol := ACol;
    iDrawRow := ARow;

    inherited;
    end;
    //------------------------------------------------------------------------------

    procedure GridsDrawColumnCell(Sender: TObject;
    const Rect: TRect; DataCol: Integer; Column: TColumn;
    State: TGridDrawState; Canvas : TCanvas;
    FontColor, BrushColor : TColor);
    var
    sText : String;
    iTextWidth : Integer;
    iOffset : Integer;
    Index: Integer;
    begin
    sText := '';
    if Assigned(Column.Field) then
    begin
    sText := Column.Field.DisplayText;
    end;

    iTextWidth := Canvas.TextWidth(sText);
    iOffset := 2;

    case Column.Alignment of
    taCenter : iOffset := (Rect.Right - Rect.Left - iTextWidth) div 2;
    taRightJustify : iOffset := (Rect.Right - Rect.Left - iTextWidth) - 2;
    end;

    if (State = [gdFixed]) then
    begin
    BrushColor := TDBGrid(Sender).FixedColor;
    end;

    // Farbänderung im Grid ermöglichen
    if (Column.Color <> Column.Grid.Brush.Color) then
    begin
    BrushColor := Column.Color;
    end;

    if (State = [gdSelected, gdFocused]) then
    begin
    FontColor := clWhite;
    BrushColor := clNavy;
    end;

    if (Column.Grid is TdmDBGrid) then
    begin
    if (TdmDBGrid(Column.Grid).DrawRow = TdmDBGrid(Column.Grid).Row) and
    (((dgRowSelect in TDBGrid(Column.Grid).Options) and (TDBGrid(Column.Grid).Focused)) or
    (dgAlwaysShowSelection in TDBGrid(Column.Grid).Options)) then
    begin
    FontColor := clWhite;
    BrushColor := clNavy;
    end;
    end;

    if TDBGrid(Column.Grid).SelectedRows.CurrentRowSelect ed then
    begin
    FontColor := clWhite;
    BrushColor := clNavy;
    end;

    Canvas.Font.Color := FontColor;
    Canvas.Brush.Color := BrushColor;
    Canvas.TextRect(Rect, Rect.Left + iOffset, Rect.Top + 2, sText);
    end;
    //------------------------------------------------------------------------------

    procedure GridsDrawColumnCell_Ext(Sender: TObject;
    const Rect: TRect; DataCol: Integer; Column: TColumn;
    State: TGridDrawState; Canvas : TCanvas;
    FontColor, BrushColor, ReadOnlyColor, ToggleColor : TColor);
    var
    iRecNo : Longint;
    begin
    if Assigned(Column.Field) then
    begin
    if (ToggleColor <> clNone) then
    begin
    iRecNo := Column.Field.DataSet.RecNo;

    {$IFDEF DB_ORACLE}
    if (iRecNo < 1) then
    begin
    if Assigned(Column.Field.DataSet.FindField('ROWNUM')) then
    begin
    iRecNo := Column.Field.DataSet.FieldByName('ROWNUM').AsInteg er;
    end;
    end;
    {$ENDIF}

    if (iRecNo mod 2 = 0) then
    begin
    BrushColor := ToggleColor;
    end;
    end;

    if (ReadOnlyColor <> clNone) then
    begin
    if (Column.Field.ReadOnly) or (Column.ReadOnly) then
    begin
    BrushColor := ReadOnlyColor;

    if (FontColor = clWindowText) then
    begin
    FontColor := clGrayText;
    end;
    end;
    end;
    end;

    GridsDrawColumnCell(Sender, Rect, DataCol, Column, State,
    TDBGrid(Sender).Canvas, FontColor, BrushColor);
    end;
    //------------------------------------------------------------------------------

    Verwendet wird es z.B.:

    procedure TmeinFormular.dmDBGrid1DrawColumnCell(Sender: TObject;
    const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
    var
    FontColor : TColor;
    BrushColor : TColor;
    begin
    FontColor := TDBGrid(Sender).Font.Color;
    BrushColor := TDBGrid(Sender).Color;

    if Assigned(Column.Field.DataSet.FindField('Droge_Che mikalie')) then
    begin
    if (Column.Field.DataSet.FieldByName('Droge_Chemikali e').AsString = 'D') then
    begin
    BrushColor := cclDROGE;
    end;
    end;

    if Assigned(Column.Field.DataSet.FindField('BTM')) then
    begin
    if (Column.Field.DataSet.FieldByName('BTM').AsString = 'B') then
    begin
    BrushColor := cclBETEUBUNGSMITTEL;
    end;
    end;

    if Assigned(Column.Field.DataSet.FindField('Vertriebs status')) then
    begin
    if (Column.Field.DataSet.FieldByName('Vertriebsstatus ').AsString = 'AV') then
    begin
    BrushColor := cclVERTRIEBSSTATUS;
    end;
    end;

    GridsDrawColumnCell(Sender, Rect, DataCol, Column, State,
    TDBGrid(Sender).Canvas, FontColor, BrushColor);
    end;
    //------------------------------------------------------------------------------

    mfg. Dietmar Maier
    (maier-soft)
Working...
X