超难题请教高手!!form上可否添加背景图片?(50分)

L

lotusxp

Unregistered / Unconfirmed
GUEST, unregistred user!
我在mdi窗体上添加了一个image控件,用以显示背景图.
但是我发现,每次改变窗体的大小的时候,显示的背景图就乱了.
后来在窗体的onresize事件中加入了image控件的refresh方法,可是也没有用.
请问这个问题该如何解决?
另外,VB的窗体可以无须其他控件直接添加背景图片,delphi有没有这个功能?如果有,
应如何实现?
 
超级菜鸟回答超难题:
---------------------------------------------------------
来自:卷起千堆雪tyn, 时间:2002-3-6 10:48:00, ID:963377
应该在 OnPaint事件中实现

procedure TForm1.FormPaint(Sender: TObject);
var
Bmp :TBitmap;
begin
Bmp :=TBitmap.Create;
Bmp.LoadFromFile('c:/10.bmp');
Canvas.Draw(0,0,Bmp);
Canvas.Refresh;
Bmp.Free;
end;
---------------------------------------------------
来自:DNChen, 时间:1999-10-24 2:10:00, ID:145496
我一般是这么写的,
Bitmap := TBitmap.Create;
try
Bitmap.LoadFromFile('MyBitmap.bmp');
Canvas.Brush.Bitmap := Bitmap;
Canvas.FillRect(ClientRect);
finally
Form1.Canvas.Brush.Bitmap := nil;
Bitmap.Free;
end;
-----------------------------------------------------
来自:tom518, 时间:1999-9-20 10:07:00, ID:135883
FClientInstance, FPrevClientProc : TFarProc;
procedure ClientWndProc(var Message: TMessage);
//定义变量与过程

procedure TForm1.FormCreate(Sender: TObject);
begin
FClientInstance:= MakeObjectInstance(ClientWndProc);
FPrevClientProc:= Pointer(GetWindowLong(ClientHandle, GWL_WNDPROC));
SetWindowLong(ClientHandle, GWL_WNDPROC, LongInt(FClientInstance));
end;

procedure TForm1.ClientWndProc(var Message: TMessage);
var
MyDC:hDC;
Ro,Co:Word;
begin //在窗体上画背景
if message.Msg<>WM_ERASEBKGND then begin
message.Result := CallWindowProc(FPrevClientProc,ClientHandle, message.Msg,message.wParam,message.lParam);
exit;
end;
MyDC := TWMEraseBkGnd(Message).DC;
for Ro := 0 to ClientHeight div Image1.Picture.Height do begin
for Co := 0 TO ClientWIDTH div Image1.Picture.Width do begin
BitBlt(MyDC, Co*Image1.Picture.Width, Ro*Image1.Picture.Height,
Image1.Picture.Width, Image1.Picture.Height,
Image1.Picture.Bitmap.Canvas.Handle, 0, 0, SRCCOPY);
message.Result:= 1;
end;
end;

end;
-----------------------------------------------------------
......
 
另一种方法:
unit Unit3;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;

type
TForm3 = class(TForm)
procedure FormPaint(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form3: TForm3;
Bitmap:TBitmap;

implementation

{$R *.DFM}

procedure TForm3.FormPaint(Sender: TObject);
var
x,y:integer;
begin
y:=0;
while y<form3.Height do
begin
x:=0;
while x<form3.Width do
begin
canvas.Draw(x,y,Bitmap);
//用canvas.Draw函数在指定位置画图
x:=x+Bitmap.Width;
//x步长为位图宽
end;
y:=y+Bitmap.Height;
//y步长为位图高
end;


end;

procedure TForm3.FormCreate(Sender: TObject);
var
sPath:string;
begin
sPath := ExtractFilePath(Application.ExeName);
Bitmap:=TBitmap.Create;
Bitmap.LoadFromFile(sPath+'./BACKBMP1.bmp');

end;

procedure TForm3.FormDestroy(Sender: TObject);
begin
Bitmap.Free;
end;

end.

此方法不用image控件。图像文件可以很少。不影响系统的运行效率。
 
可以 ! 最好是使用构件 方便适用 。 如果需要 我这给你一个。
 
半疯半仙:
谢谢!@发到我邮箱里吧!
lotus133@163.com
 
顶部