Shagrouni

Transparent Grid

Khaled Shagrouni, February 26, 2001

Last updated: June, 22, 2002

Being transparent.. being a child.. being there.

Delphi Papers

Published in  Delphi3000.com

 

This is a quick experiment of how to make a grid transparent and showing any underline image, the example below is a full unit code, the controls layout of the form is as the following:

  • ADOTable1: (you can use a normal TTable) showing what ever you want, and let it be Active.
  • DataSource1: Connected to the table above.
  • Image1: Aligned to alClient to cover the entire form, with a Bitmap picture (bmp. Not jpg).
  • Panel1: Large enough to host our grid.
  • DbGrid1: As a child inside Panel1, make it aligned to alBottom, leave some space for the upper part of the panel to be visible. Don't forget to link it to DataSource1.

The code bellow has two procedures, the first is to handle the event OnDrawDataCell of the DBGrid, which also contain the real work to make the grid transparent.

The second procedure handle the OnMouseDown event of Panel1, this will move the Panel and make it transparent also to the image beneath.


Transparent DBGrid

Download: transgrid.zip (56 KB)

fTransparentGrid Example:

unit fTransparentGrid;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls,
        Forms, Dialogs, Db, Grids, DBGrids, ADODB, ExtCtrls;

type
  TForm1 = class(TForm)
    ADOTable1: TADOTable;
    DataSource1: TDataSource;
    Image1: TImage;
    Panel1: TPanel;
    DBGrid1: TDBGrid;
    procedure DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
      Field: TField; State: TGridDrawState);
    procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject;
const Rect: TRect; Field: TField; State: TGridDrawState); var Text: string; Rct: TRect; begin Text := Field.AsString; Rct:= Rect; DBGrid1.Canvas.Brush.Color := clWhite; DBGrid1.Canvas.FillRect (Rct); BitBlt(DBGrid1.Canvas.handle, Rct.left, Rct.top, Rct.right - Rct.left, Rct.bottom - Rct.top, Image1.Canvas.Handle, Rct.left + DBGrid1.Left + Panel1.Left, Rct.Top + DBGrid1.Top + Panel1.Top, SRCCOPY); SetBkModE(DBGrid1.Canvas.Handle, TRANSPARENT); inc(Rct.Top ,2); inc(Rct.Left ,2); DBGrid1.Canvas.Font.Color := clBlack; DrawtextEx(DBGrid1.Canvas.Handle, PChar(Text), Length(Text), Rct, DT_WORDBREAK, nil); end; procedure TForm1.Panel1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin ReleaseCapture; Panel1.Perform(WM_SYSCOMMAND, $F012, SC_MOVE); Application.ProcessMessages ; BitBlt(GetDc(Panel1.Handle), 0, 0, Panel1.Width, Panel1.Height, Image1.Canvas.Handle , Panel1.Left, Panel1.Top, SRCAND); DBGrid1.refresh; end; end.

 

 

Shagrouni 2001 Khaled Shagrouni  khaled@shagrouni.com