Slicker:
好象是不行的。
我的例子是:在MDI Form上放一个Button上两个Image:Image1,Image2。
然后我如你上面所说写两个ClientWndProc:ClientWndProc1和ClientWndProc2。其内容
只相差在Image1和Image2上。
在OnClick的响应事件ButtonClick中,我交替ClientWndProc1和ClientWndProc2。但背景
仍不能改变。
你能否看看问题在哪里?谢谢了。
代码如下:
unit Ex1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Buttons, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
Image2: TImage;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SelfWndProc1(var SelfMsg: TMessage);//window procedure
procedure SelfWndProc2(var SelfMsg: TMessage);//window procedure
procedure FillBitmap(aDC:HDC; aImage:TImage);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var
PrevProc, SelfProc : TFarProc;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
Tag := (Tag + 1) mod 2;
if Tag=0 then SelfProc:=MakeObjectInstance(SelfWndProc1)
else SelfProc:=MakeObjectInstance(SelfWndProc2);
SetWindowLong(ClientHandle,GWL_WNDPROC,LongInt(SelfProc));
PostMessage(Handle, WM_ERASEBKGND, ClientHandle, 0);
Refresh;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
PrevProc:=TFarProc(GetWindowLong(ClientHandle,GWL_WNDPROC));
SelfProc:=MakeObjectInstance(SelfWndProc1);
SetWindowLong(ClientHandle,GWL_WNDPROC,LongInt(SelfProc));
end;
procedure TForm1.FillBitmap(aDC:HDC; aImage:TImage);
var
aCanvas : TCanvas;
i, j, bH, bW, cH, cW : Word;
begin
bH := aImage.Picture.Height; cH := ClientHeight DIV bH;
bW := aImage.Picture.Width; cW := ClientWidth DIV bW;
aCanvas := aImage.Picture.Bitmap.Canvas;
for i := 0 TO cH DO
for j := 0 TO cW DO
BitBlt(aDC, j*bW, i*bH, bW, bH, aCanvas.Handle, 0, 0, SRCCOPY);
end;
procedure TForm1.SelfWndProc1(var SelfMsg:TMessage);
begin
with SelfMsg do
case Msg of
WM_ERASEBKGND:
begin
FillBitmap(TWMEraseBkGnd(SelfMsg).DC, Image1);
Result := 1;
end
else
Result := CallWindowProc(PrevProc, ClientHandle, Msg, wParam, lParam);
end;
end;
procedure TForm1.SelfWndProc2(var SelfMsg:TMessage);
begin
with SelfMsg do
case Msg of
WM_ERASEBKGND:
begin
FillBitmap(TWMEraseBkGnd(SelfMsg).DC, Image2);
Result := 1;
end
else
Result := CallWindowProc(PrevProc, ClientHandle, Msg, wParam, lParam);
end;
end;
end.