Title: Transparent Grid
Question: How to make a grid or panel transparent?
Answer:
Transparent Grid
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 procedure, 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.
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
public
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;
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);
DBGrid1.Canvas.Font.Style := [fsBold];
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.
Khaled Shagrouni.