如何将一幅图片做成整个窗体的背影?(50分)

  • 主题发起人 主题发起人 阿三
  • 开始时间 开始时间

阿三

Unregistered / Unconfirmed
GUEST, unregistred user!
如何将一幅图片做成整个窗体的背影?[red][/red][:(]
 
最简单的方法就是用控件了,如果要自己做的话就在form的onpaint事件中画form.canvas。
 
把image设为整个窗体大小,然后设置stretch为真就行了。
 
一个透明窗体的例子.
假定背景色单一.
var
Form1: TForm1;
BackColor:TColor;

implementation

{$R *.DFM}
{$R Icon.res}
procedure TForm1.FormCreate(Sender: TObject);
begin
BackColor:=Image1.Canvas.Pixels[2,2];//假定背景颜色是 Image1.Canvas.Pixels[2,2]的颜色
SetWindowLong(Application.Handle,GWL_ExStyle,WS_EX_Toolwindow);
screen.Cursors[lovecursor]:=loadcursor(hInstance,'newcursor');
cursor:=lovecursor;
installIcon;
end;

procedure TForm1.FormShow(Sender: TObject);
var
MyRgn, ClientRgn, ButtonRgn,bmpRgn: THandle;
Margin, X, Y: Integer;
p : Tpoint;
begin
Margin := (Width - ClientWidth) div 2;
MyRgn := CreateRectRgn(0, 0, Width, Height);
X := Margin;
Y := Height - ClientHeight - Margin;
ClientRgn := CreateRectRgn(X, Y, X + ClientWidth, Y + ClientHeight);
CombineRgn(MyRgn, MyRgn, ClientRgn, RGN_XOR);

for x:=0 to Image1.width-1 do
for y:=0 to Image1.height-1 do
begin
if Image1.Canvas.Pixels[x,y]<> BackColor then //这里判断每个像素点
begin
bmpRgn:= CreateRectRgn(x,y,x+1,y+1);
CombineRgn(MyRgn,MyRgn,bmpRgn,RGN_XOR);
end;
end;

SetWindowRgn(Handle, MyRgn, True);

end;
 
unit Unit1;

interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
procedure FormPaint(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
TheGraphic: TBitmap; { Add this declaration for the graphic}
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}

procedure TForm1.FormPaint(Sender: TObject); { OnPaint event handler}
begin
Form1.Canvas.Draw(0, 0, TheGraphic); { Draw the graphic on the Canvas }
end;
procedure TForm1.FormCreate(Sender: TObject); { OnCreate event handler }
begin
TheGraphic := TBitmap.Create; { Create the bitmap object }
TheGraphic.LoadFromFile('C:/APP/BKGRND.BMP'); { Load the bitmap from a file}
end;
end.
 
不是,不是,我用的图片比较小,比窗体要小,我想把这张图片放大到整个窗体。另外能不能用Image控件来实现?
 
有一个专门的控件的
 
是什么控件?
 
Image1.Stretch :=true;
 
Image的 align 属性设置为 alClient
Stretch 属性设置为 true
 
将Form的FormStyle设置为fsMDIForm,加入一个TImage,把你背景图象文件放到TImage中。
在你的Form中的private如下定义:
FClientInstance,
FPrevClientPro: TFarProc;
procedure ClientWndProc(Var Message: TMessage);

procedure TForm1.ClientWndProc(VAR Message: TMessage);
var
MyDC: hDC;
Ro, Co: Word;
begin
with Message do
case Msg of
WM_ERASEBKGND:
begin
MyDC := TWMEraseBkGnd(Message).DC;
For Ro:= 0 to ClientHeight div Image1.Picture.Height do
For Co := 0 to ClientWidth div Image1.Picture.Width do
BitBlt(MyDC, Co*Image1.Picture.Width, Ro*Image1.Picture.Height,
Image1.Picture.Width, Image1.Picture.Height,
Image1.Picture.Bitmap.Canvas.Handle,0,0,SRCCOPY);

Result := 1;
end;
else
Result := CallWindowProc(FPrevClientPro,ClientHandle,Msg,wParam,lParam);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FClientInstance := MakeObjectInstance(ClientWndProc);
FPrevClientPro := Pointer(GetWindowLong(ClientHandle, GWL_WNDPROC));
SetWindowLong(ClientHandle, GWL_WNDPROC,LongInt(FClientInstance));
end;
 
这个绝对可以,我自己试了,快给分

unit Unit1;

interface

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

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

var
Form1: TForm1;
Bitmap: TBitmap;

implementation

{$R *.dfm}

procedure TForm1.FormPaint(Sender: TObject);
var
X, Y, W, H: LongInt;
begin
with Bitmap do begin
W := Width;
H := Height;
end;
Y := 0;
while Y < Height do begin
X := 0;
while X < Width do begin
Canvas.Draw(X, Y, Bitmap);
Inc(X, W);
end;
Inc(Y, H);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Bitmap := TBitmap.Create;
Bitmap.LoadFromFile('C:/test.BMP');
end;

end.
 
后退
顶部