2008. január 11., péntek

Create a flat TDBGrid


Problem/Question/Abstract:

How to create a flat TDBGrid

Answer:

This is an approach for creating a flat TDBGrid. It has some problems with its scrollbar tuning, but you can take it at least as an example.

{ ... }
type
  TMyGBGrid = class(TDBGrid)
  protected
    FFlat: boolean;
    procedure CreateWnd; override;
    procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);
      override;
    procedure InitScrollBars;
    procedure SetFlat(AValue: boolean);
  public
    constructor Create(AOwner: TComponent); override;
  published
    property Flat: boolean read FFlat write SetFlat;
  end;

  { .... }

constructor TMyGBGrid.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FFlat := true;
end;

procedure TMyGBGrid.CreateWnd;
begin
  inherited CreateWnd;
  InitScrollBars;
end;

procedure TMyGBGrid.DrawCell(ACol, ARow: Longint; ARect: TRect; AState:
  TGridDrawState);

  procedure xXDrawBorder(var ABorderRect: TRect);
  begin
    InflateRect(ABorderRect, 1, 1);
    Frame3D(Canvas, ABorderRect, clBtnShadow, clBtnShadow, 1);
    Frame3D(Canvas, ABorderRect, clBtnHighlight, clBtnFace, 1);
  end;

begin
  inherited DrawCell(ACol, ARow, ARect, AState);
  if Flat and ((ACol = 0) or (ARow = 0)) then
  begin
    if (ARow = 0) and (dgTitles in Options) then
      xXDrawBorder(ARect)
    else if (ACol = 0) and (dgIndicator in Options) then
      xXDrawBorder(ARect);
  end
end;

procedure TMyGBGrid.InitScrollBars;
var
  XVerScrollInfo, XHorScrollInfo: TScrollInfo;
begin
  if FFlat then
  begin
    GetScrollInfo(Handle, SB_VERT, XVerScrollInfo);
    GetScrollInfo(Handle, SB_HORZ, XHorScrollInfo);
    InitializeFlatSB(Handle);
    FlatSB_SetScrollInfo(Handle, SB_VERT, XVerScrollInfo, true);
    FlatSB_SetScrollInfo(Handle, SB_HORZ, XHorScrollInfo, true);
    FlatSB_SetScrollProp(Handle, WSB_PROP_VSTYLE, FSB_ENCARTA_MODE, true);
    FlatSB_SetScrollProp(Handle, WSB_PROP_HSTYLE, FSB_ENCARTA_MODE, true);
    FlatSB_SetScrollProp(Handle, WSB_PROP_VBKGCOLOR, clGreen, true);
    FlatSB_SetScrollProp(Handle, WSB_PROP_HBKGCOLOR, clBlue, true);
  end;
end;

procedure TMyGBGrid.SetFlat(AValue: boolean);
begin
  if AValue <> FFlat then
  begin
    FFlat := AValue;
    RecreateWnd;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése