{***********************************************************} { } { 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. } { } {***********************************************************}unit CxWallpaper;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type TCxWallpaper = class(TComponent) private FForm: TForm; FHandle: HWND; FMargins: array [0..9] of Integer; FClientInstance: TFarProc; FPrevClientProc: TFarProc; FBitmap: TBitmap; procedure ClientWndProc(var Message: TMessage); procedure SetBitmap(const Value: TBitmap); protected procedure Loaded; override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; published property Bitmap: TBitmap read FBitmap write SetBitmap; end;implementationconstructor 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;end.
{ }
{ 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. }
{ }
{***********************************************************}unit CxWallpaper;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type
TCxWallpaper = class(TComponent)
private
FForm: TForm;
FHandle: HWND;
FMargins: array [0..9] of Integer;
FClientInstance: TFarProc;
FPrevClientProc: TFarProc;
FBitmap: TBitmap;
procedure ClientWndProc(var Message: TMessage);
procedure SetBitmap(const Value: TBitmap);
protected
procedure Loaded; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Bitmap: TBitmap read FBitmap write SetBitmap;
end;implementationconstructor 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;end.