線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2236
推到 Plurk!
推到 Facebook!

怎麽在窗體中動態貼圖片?

答題得分者是:pedro
mp394681143
一般會員


發表:64
回覆:40
積分:20
註冊:2009-10-11

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-10-30 02:43:44 IP:61.136.xxx.xxx 訂閱
我想在窗體中動態貼滿圖片,不知怎麼實現?試驗了幾次未能成功?
[code delphi]
procedure TForm1.FormCreate(Sender: TObject);
var
tu:Timage;
i:integer;
begin
for i:=0 to 200 do
begin
tu:=Timage.Create(self);
tu.Picture.Parent :=self;
tu.Left :=0;
tu.Top:=1 i*2;
tu.Width:= 150;
tu.Height:=149;
end;
end;

[/code]
sryang
尊榮會員


發表:39
回覆:762
積分:920
註冊:2002-06-27

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-10-30 07:59:25 IP:124.10.xxx.xxx 訂閱
你沒有載入圖檔,怎麼看得出來?
------
歡迎參訪 "腦殘賤貓的備忘錄" http://maolaoda.blogspot.com/
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-10-30 08:42:34 IP:203.70.xxx.xxx 未訂閱
以下程式直接在TForm.Canvas上作貼圖
提供你參考...
[code delphi]
unit DKXTestMain_2;

interface

uses
Windows,
SysUtils,
Graphics,
Controls,
Forms,
Jpeg;

type

TDrawKind =(dkStretch,dkTile);

TfrmTest2Main = class(TForm)
procedure FormPaint(Sender: TObject);
procedure FormResize(Sender: TObject);
private
function GetGraphic(fn: string; var g: TGraphic): Boolean;
function GraphicType(fn: string): Integer; overload;
function GraphicType(g: TGraphic): Integer; overload;
procedure Draw(c: TCanvas; r: TRect; fn: string; DrawKind: TDrawKind); overload;
procedure DrawStretch(c: TCanvas; r: TRect; g: TGraphic); overload;
procedure DrawTile(c: TCanvas; r: TRect; g: TGraphic); overload;
end;

var
frmTest2Main: TfrmTest2Main;

implementation

{$R *.dfm}

procedure TfrmTest2Main.Draw(c: TCanvas; r: TRect; fn: string; DrawKind: TDrawKind);
var
g: TGraphic;
begin
if GetGraphic(fn,g) then
try
case DrawKind of
dkStretch: DrawStretch(c,r,g);
dkTile: DrawTile(c,r,g);
end;
finally
FreeAndNil(g);
end;
end;

procedure TfrmTest2Main.DrawStretch(c: TCanvas; r: TRect; g: TGraphic);
begin
if g <> nil then
begin
c.Lock;
try
c.StretchDraw(r,g);
finally
c.Unlock;
end;
end;
end;

procedure TfrmTest2Main.DrawTile(c: TCanvas; r: TRect; g: TGraphic);
var
X,Y: Integer;
begin
if (g <> nil) and (g.Width > 0) and (g.Height > 0) then
begin
c.Lock;
try
Y := r.Top;
while Y < r.Bottom do
begin
X := r.Left;
while X < r.Right do
begin
c.Draw(X,Y,g);
Inc(X,g.Width);
end;
Inc(Y,g.Height);
end;
finally
c.Unlock;
end;
end;
end;

function TfrmTest2Main.GetGraphic(fn: string; var g: TGraphic): Boolean;
begin
g := nil;
if FileExists(fn) then
begin
case GraphicType(fn) of
0: g := TBitmap.Create;
1: g := TJPEGImage.Create;
end;
if g <> nil then g.LoadFromFile(fn);
end;
Result := g <> nil;
end;

function TfrmTest2Main.GraphicType(fn: string): Integer;
var
S: string;
begin
S := UpperCase(fn);
Result := -1;
if Pos('.BMP',S) > 0 then Result := 0
else if Pos('.JPG',S) > 0 then Result := 1
else if Pos('.JPEG',S)> 0 then Result := 1;
end;

function TfrmTest2Main.GraphicType(g: TGraphic): Integer;
begin
Result := -1;
if g <> nil then
begin
if g is TBitmap then Result := 0
else if g is TJPEGImage then Result := 1;
end;
end;

procedure TfrmTest2Main.FormPaint(Sender: TObject);
begin
Draw(Canvas,ClientRect,'C:\BACK1.JPG',dkTile);
end;

procedure TfrmTest2Main.FormResize(Sender: TObject);
begin
Invalidate;
end;

end.

[/code]

mp394681143
一般會員


發表:64
回覆:40
積分:20
註冊:2009-10-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2009-10-30 21:08:12 IP:61.136.xxx.xxx 訂閱
謝謝二樓,但是我加了這句,提示類型不兼容 !難道動態用這個控件就一定實現不了嗎?
tu.Picture.assign('e:\loao.bmp'); //提示類型不兼容

===================引 用 sryang 文 章===================
你沒有載入圖檔,怎麼看得出來?
編輯記錄
mp394681143 重新編輯於 2009-10-30 21:08:58, 註解 無‧
pedro
尊榮會員


發表:152
回覆:1187
積分:892
註冊:2002-06-12

發送簡訊給我
#5 引用回覆 回覆 發表時間:2009-10-30 21:28:31 IP:59.112.xxx.xxx 未訂閱
assign應該要同類型才能賦值
你的語意,想要載入圖檔,
應該採前前面大大所提的,用LoadFrom.....Method

===================引 用 mp394681143 文 章===================
謝謝二樓,但是我加了這句,提示類型不兼容 !難道動態用這個控件就一定實現不了嗎?
tu.Picture.assign('e:\loao.bmp');? //提示類型不兼容

===================引 用 sryang 文 章===================
你沒有載入圖檔,怎麼看得出來?
mp394681143
一般會員


發表:64
回覆:40
積分:20
註冊:2009-10-11

發送簡訊給我
#6 引用回覆 回覆 發表時間:2009-10-31 03:47:23 IP:61.136.xxx.xxx 訂閱
tu.Picture.LoadFromFile('e:\loao.bmp');
可還是不行,圖片並沒有循環?語法不對?
===================引 用 pedro 文 章===================
assign應該要同類型才能賦值
你的語意,想要載入圖檔,
應該採前前面大大所提的,用LoadFrom.....Method

===================引 用 mp394681143 文 章===================
謝謝二樓,但是我加了這句,提示類型不兼容 !難道動態用這個控件就一定實現不了嗎?
tu.Picture.assign('e:\loao.bmp');? //提示類型不兼容

===================引 用 sryang 文 章===================
你沒有載入圖檔,怎麼看得出來?
pedro
尊榮會員


發表:152
回覆:1187
積分:892
註冊:2002-06-12

發送簡訊給我
#7 引用回覆 回覆 發表時間:2009-10-31 10:04:55 IP:59.112.xxx.xxx 未訂閱
您的程式中,並未指定動態產生Image的parent

下面是我測試OK的

[code delphi]
var
tu:Timage;
i:Integer;
begin
for i:=0 to 5 do
begin
tu:=Timage.Create(self);
tu.Parent:=Self;
tu.Stretch:=True;
tu.Left :=200;
tu.Top:=1 i*150;
tu.Width:= 150;
tu.Height:=149;
tu.Picture.LoadFromFile('c:\a.bmp');
end;
end;
[/code]


===================引 用 mp394681143 文 章===================
tu.Picture.LoadFromFile('e:\loao.bmp');
可還是不行,圖片並沒有循環?語法不對?
===================引 用 pedro 文 章===================
assign應該要同類型才能賦值
你的語意,想要載入圖檔,
應該採前前面大大所提的,用LoadFrom.....Method

===================引 用 mp394681143 文 章===================
謝謝二樓,但是我加了這句,提示類型不兼容 !難道動態用這個控件就一定實現不了嗎?
tu.Picture.assign('e:\loao.bmp');? //提示類型不兼容

===================引 用 sryang 文 章===================
你沒有載入圖檔,怎麼看得出來?
mp394681143
一般會員


發表:64
回覆:40
積分:20
註冊:2009-10-11

發送簡訊給我
#8 引用回覆 回覆 發表時間:2009-10-31 22:52:36 IP:61.136.xxx.xxx 訂閱
謝謝大俠
系統時間:2024-04-16 22:01:14
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!