简体   繁体   中英

DBGrid XE3 background color

在此输入图像描述

{*-----------------------------------------------------------------------------
 Unit Name: TopFormU
 @Author Mr. Arch Brooks, Software Engineer, Brooks Computing Systems LLC
 @Version 1.0
 Date:      04-Jan-2014
 Purpose:
 History:
 -----------------------------------------------------------------------------}

unit TopFormU;

interface

uses
  BCSXE3Utilsdp, System.Classes, System.SysUtils, System.Variants,
  TopFormdmU, Vcl.ComCtrls, Vcl.Controls, Vcl.DBCtrls, Vcl.Dialogs,
  Vcl.ExtCtrls, Vcl.Forms, Vcl.Graphics, Vcl.Grids, Vcl.Menus, Vcl.StdCtrls,
  Vcl.TabNotBk, Winapi.Messages, Winapi.Windows, Vcl.DBGrids;

type

  /// Tab Sheet Class
  TTabSheet = class(Vcl.ComCtrls.TTabSheet)
  private
    /// Tab Control Color
    FColor: TColor;
    procedure SetColor(Value: TColor);
    procedure WMEraseBkGnd(var Msg: TWMEraseBkGnd); message WM_ERASEBKGND;
  public
    constructor Create(aOwner: TComponent); override;
    property Color: TColor read FColor write SetColor;
  end;

  /// TopForm Primary Class
  TTopFormC = class(TForm)
    /// BCS XE3 Utilities Component
    BCSXE3UtilsCmp1: TBCSXE3UtilsCmp;
   /// TopForm Color Dialog
    TopFormColor: TColorDialog;
    /// TopForm Colors Menu Item
    TopFormColors1: TMenuItem;
    /// TopForm DB Navigator
    TopFormDBNavigator1: TDBNavigator;
    /// TopForm DB Memo
    TopFormDBMemo1: TDBMemo;
    /// TopForm SB Grid
    TopFormDBGrid1: TDBGrid;
    /// BCSPageColor Main Menu
    TopFormMainMenu1: TMainMenu;
   /// Main Page Control
    TopFormPageControl1: TPageControl;
    /// Help Menu Item
    TopFormHelp1: TMenuItem;
    /// Status Panel For Dialog
    TopFormStatusPanel1: TStatusBar;
    /// Tab sheet 1 for page control
    TopFormTabSheet1: TTabSheet;
    /// Tab sheet 2 for page control
    TopFormTabSheet2: TTabSheet;
    /// Timer for Dialog
    TopFormTimer1: TTimer;

    procedure TopFormColors1Click(Sender: TObject);
    procedure TopFormCreate(Sender: TObject);
    procedure TopFormDrawTab(Control: TCustomTabControl; TabIndex: Integer;
      const Rect: TRect; Active: Boolean);
    procedure TopFormFormActivate(Sender: TObject);
    procedure TopFormGridColor;
    procedure TopFormHelp1Click(Sender: TObject);
    procedure TopFormStatusBar1DrawPanel(StatusBar: TStatusBar;
      Panel: TStatusPanel; const Rect: TRect);
    procedure TopFormTimer1Timer(Sender: TObject);
  public
    {Public declarations}
  end;

var
  /// TopForm Dialog Pointer
  TopFormC: TTopFormC;

implementation

{$R *.dfm}

var
  /// TimeStamp Variable
  ftime: String;
  /// Item Index
  i: Integer;

{*-----------------------------------------------------------------------------
  Procedure: Create
  Date:      04-Jan-2014
  @Param     aOwner: TComponent
  @Return    None

 -----------------------------------------------------------------------------}

constructor TTabSheet.Create(aOwner: TComponent);
begin
  inherited;
  FColor := clWhite;
end;

{*-----------------------------------------------------------------------------
 Procedure: SetColor
 Date:      04-Jan-2014
 @Param     Value: TColor
 @Return    None

 -----------------------------------------------------------------------------}

procedure TTabSheet.SetColor(Value: TColor);
begin
  if FColor <> Value then
  begin
    FColor := Value;
    Invalidate;
  end;
end;

{*-----------------------------------------------------------------------------
 Procedure: WMEraseBkGnd
 Date:      04-Jan-2014
 @Param     var Msg: TWMEraseBkGnd
 @Return    None

 -----------------------------------------------------------------------------}

procedure TTabSheet.WMEraseBkGnd(var Msg: TWMEraseBkGnd);
begin
  if FColor = clBtnFace then
    inherited
  else
  begin
    Brush.Color := FColor;
    FillRect(Msg.dc, ClientRect, Brush.Handle);
    Msg.Result := 1;
  end;
end;

{*-----------------------------------------------------------------------------
 Procedure: TopFormColors1Click
 Date:      04-Jan-2014
 @Param     Sender: TObject
 @Return    None

 -----------------------------------------------------------------------------}

procedure TTopFormC.TopFormColors1Click(Sender: TObject);
var
  ti: Integer;
begin
  if TopFormColor.Execute then
  begin
    Color := TopFormColor.Color;
    TopFormTabSheet1.Color := Color;
    TopFormTabSheet2.Color := Color;
    TopFormDBGrid1.Color := Color;
    TopFormStatusPanel1.Color := Color;
    TopFormGridColor;
    TopFormDBGrid1.Repaint;
  end;
end;

{*-----------------------------------------------------------------------------
 Procedure: TopFormCreate
 Date:      04-Jan-2014
 @Param     Sender: TObject
 @Return    None

 -----------------------------------------------------------------------------}

procedure TTopFormC.TopFormCreate(Sender: TObject);
begin
  Color := $C9FCFA;
  TopFormTabSheet1.Color := Color;
  TopFormTabSheet2.Color := Color;
  TopFormStatusPanel1.Color := Color;
end;

{*-----------------------------------------------------------------------------
 Procedure: TopFormDrawTab
 Date:      04-Jan-2014
 @Param     Control: TCustomTabControl; TabIndex: Integer; const Rect: TRect;
 Active: Boolean
 @Return    None

 -----------------------------------------------------------------------------}

procedure TTopFormC.TopFormDrawTab(Control: TCustomTabControl;
  TabIndex: Integer; const Rect: TRect; Active: Boolean);
var
  AText: string;
  APoint: TPoint;
begin
  with (Control as TPageControl).Canvas do
  begin
    Brush.Color := Color;
    FillRect(Rect);
    AText := TPageControl(Control).Pages[TabIndex].Caption;
    with Control.Canvas do
    begin
      APoint.x := (Rect.Right - Rect.Left) div 2 - TextWidth(AText) div 2;
      APoint.y := (Rect.Bottom - Rect.Top) div 2 - TextHeight(AText) div 2;
      TextRect(Rect, Rect.Left + APoint.x, Rect.Top + APoint.y, AText);
    end;
  end;
end;

{*-----------------------------------------------------------------------------
 Procedure: FormActivate
 Date:      05-Jan-2014
 @Param     Sender: TObject
 @Return    None

 -----------------------------------------------------------------------------}

procedure TTopFormC.TopFormFormActivate(Sender: TObject);
begin
  TopFormDBGrid1.Invalidate;
  TopFormDBGrid1.Color := Color;
  TopFormDBGrid1.Canvas.Brush.Color := Color;
  TopFormDBGrid1.Canvas.Refresh;
  TopFormGridColor;
end;

{*-----------------------------------------------------------------------------
 Procedure: TopFormGridColor
 Date:      05-Jan-2014
 @Param     None
 @Return    None

 -----------------------------------------------------------------------------}

procedure TTopFormC.TopFormGridColor;
begin
  i := 0;
  repeat
    TopFormDBGrid1.Columns[i].Color := Color;
    Inc(i);
  until i > TopFormDBGrid1.Columns.Count - 1;
  TopFormDBGrid1.Repaint;
end;

{*-----------------------------------------------------------------------------
 Procedure: TopFormHelp1Click
 Date:      04-Jan-2014
 @Param     Sender: TObject
 @Return    None

 -----------------------------------------------------------------------------}

procedure TTopFormC.TopFormHelp1Click(Sender: TObject);
begin
  BCSXE3UtilsCmp1.ShellExec('http://bcswebs.us/bcs002/');
end;

{*-----------------------------------------------------------------------------
 Procedure: TopFormStatusBar1DrawPanel
 Date:      04-Jan-2014
 @Param     StatusBar: TStatusBar; Panel: TStatusPanel; const Rect: TRect
 @Return    None

 -----------------------------------------------------------------------------}

procedure TTopFormC.TopFormStatusBar1DrawPanel(StatusBar: TStatusBar;
  Panel: TStatusPanel; const Rect: TRect);
begin
  with StatusBar.Canvas do
  begin
    FillRect(Rect);
    case Panel.Index of
      0: // fist panel
        begin
          Brush.Color := Color;
          Font.Color := clBlack;
          // Font.Style := [fsBold];
          TextRect(Rect, 2 + Rect.Left, 2 + Rect.Top, Panel.Text);
        end;
      1: // second panel
        begin
          Brush.Color := Color;
          Font.Color := clBlack;
          // Font.Style := [fsItalic];
          TextRect(Rect, 2 + Rect.Left, 2 + Rect.Top, Panel.Text);
        end;
      2: // Third panel
        begin
          Brush.Color := Color;
          Font.Color := clBlack;
          // Font.Style := [fsItalic];
          Panel.Text := ftime;
          TextRect(Rect, 2 + Rect.Left, 2 + Rect.Top, Panel.Text);
          TextOut(0, 0, ftime);
        end;
    end;
  end;
end;

{*-----------------------------------------------------------------------------
 Procedure: TopFormTimer1Timer
 Date:      04-Jan-2014
 @Param     Sender: TObject
 @Return    None

 -----------------------------------------------------------------------------}

procedure TTopFormC.TopFormTimer1Timer(Sender: TObject);
begin
  DateTimeToString(ftime, 'dddd, mmmm dd, yyyy hh:mm:ss     ', now);
  TopFormStatusPanel1.Panels[2].Text := ftime;
end;

end.

You can see the rows are non white. The background for the remainder of the grid is white. How do I fill the white area with a color?

To change the background color of the area not occupied by cells, you must set the Color property , but this only works if the DrawingStyle property has the gdsClassic Value. So in order to keep the themed dbgrid and change the background color you must override the Paint method.

try this sample which uses a interposer class

type
  TDBGrid = class(Vcl.DBGrids.TDBGrid)
  protected
    procedure Paint; override;
  end;

  TForm1 = class(TForm)
    DbGrid1: TDbGrid;
  ....
  ....

{ TDBGrid }


procedure TDBGrid.Paint;
var
  LDrawInfo: TGridDrawInfo;
begin
  inherited Paint;
  CalcDrawInfo(LDrawInfo);
  if LDrawInfo.Horz.GridBoundary < LDrawInfo.Horz.GridExtent then
  begin
    Canvas.Brush.Color := Color; //use the Color property to paint the background
    Canvas.FillRect(Rect(LDrawInfo.Horz.GridBoundary, 0, LDrawInfo.Horz.GridExtent, LDrawInfo.Vert.GridBoundary));
  end;
  if LDrawInfo.Vert.GridBoundary < LDrawInfo.Vert.GridExtent then
  begin
    Canvas.Brush.Color := Color;//use the Color property to paint the background
    Canvas.FillRect(Rect(0, LDrawInfo.Vert.GridBoundary, LDrawInfo.Horz.GridExtent, LDrawInfo.Vert.GridExtent));
  end;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM