{***********************************************************}
{ }
{ Chou's Delphi VCL Extensions (CX Lib) }
{ }
{ Copyright (c) 1999 Jean-Christopher Chou }
{ }
{ ------------------------------------------------------- }
{ }
{ Class Name: TCxWallpaper }
{ Package: CwCtrls.dpk }
{ Writor: Jean-Christopher Chou }
{ Create Date: 1999-08-27 }
{ Description: Put it on your form and give it a bitmap }
{ then on runtime the form's background }
{ will be filled by a tiled bitmap. }
{ }
{***********************************************************}
constructor TCxWallpaper.Create(AOwner: TComponent);
var
i: Integer;
begin
inherited Create(AOwner);
FBitmap := TBitmap.Create;
Randomize;
for i := 0 to 9 do
FMargins[i] := Random(100);
end;
destructor TCxWallpaper.Destroy;
begin
FBitmap.Free;
inherited Destroy;
end;
procedure TCxWallpaper.Loaded;
begin
inherited Loaded;
if not (csDesigning in ComponentState) then
begin
if not (Owner is TForm) then
raise Exception.Create('我只能在Form上用!')
else
begin
FForm := Owner as TForm;
if FForm.FormStyle = fsMDIForm then
FHandle := FForm.ClientHandle
else
FHandle := FForm.Handle;
FClientInstance := MakeObjectInstance(ClientWndProc);
FPrevClientProc := Pointer(GetWindowLong(FHandle, GWL_WNDPROC));
SetWindowLong(FHandle, GWL_WNDPROC, LongInt(FClientInstance));
end;
end;
end;
procedure TCxWallpaper.ClientWndProc(var Message: TMessage);
var
DC: HDC;
W, H, Mrg, Row, Col: Integer;
begin
with Message do
case Msg of
WM_ERASEBKGND:
if not FBitmap.Empty then
begin
DC := TWMEraseBkGnd(Message).DC;
W := FBitmap.Width;
H := FBitmap.Height;
for Row := 0 to FForm.ClientHeight div H do
begin
Mrg := FMargins[Row mod 10];
for Col := 0 to (FForm.ClientWidth + Mrg) div W + 3 do
BitBlt(DC, Col * W - Mrg, Row * H, W, H,
FBitmap.Canvas.Handle,
0, 0, SRCCOPY);
end;
Result := 1;
end;
else
Result := CallWindowProc(FPrevClientProc,
FHandle, Msg, wParam, lParam);
end;
end;
procedure TCxWallpaper.SetBitmap(const Value: TBitmap);
var
i: Integer;
begin
FBitmap.Assign(Value);
if not FBitmap.Empty then
begin
Randomize;
for i := 0 to 9 do
FMargins[i] := Random(FBitmap.Width);
end;
end;