:*看谁能解答的奇怪问题* (100分)

  • 主题发起人 HoneyCow
  • 开始时间
H

HoneyCow

Unregistered / Unconfirmed
GUEST, unregistred user!
在窗体上放5个Image,一个Button。看看下面程序的奇怪现象:当第二次按BUTTON时五个IMAGE的图象都一样了,为什么?请帮忙解答!
email:gqg@21cn.com

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
Image2: TImage;
Image3: TImage;
Image4: TImage;
Image5: TImage;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
MG: TBitmap;
MG1: TBitmap;
MG2: TBitmap;
MG3: TBitmap;
MG4: TBitmap;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
Width,Height, fheight,fwidth:integer;

x:integer;
y:integer;
x1:integer;
y1:integer;
bkcolor:Tcolor;
rotorcolor:Tcolor;
const pi=3.1415926;

begin
Width:=32;
Height:=32;
fheight:=height-1;
fwidth:=width-1;
bkcolor:=claqua;
rotorcolor:=clred;
if (assigned(MG)) then
MG.Destroy ;
MG:=nil;
MG:=Tbitmap.Create ;
MG.Height :=height;
MG.Width :=width;
if (assigned(MG1)) then
MG.Destroy ;
MG1:=nil;
MG1:=Tbitmap.Create ;
MG1.Height :=height;
MG1.Width :=width;
if (assigned(MG2)) then
MG.Destroy ;
MG2:=nil;
MG2:=Tbitmap.Create ;
MG2.Height :=height;
MG2.Width :=width;
if (assigned(MG3)) then
MG.Destroy ;
MG3:=nil;
MG3:=Tbitmap.Create ;
MG3.Height :=height;
MG3.Width :=width;
if (assigned(MG4)) then
MG.Destroy ;
MG4:=nil;
MG4:=Tbitmap.Create ;
MG4.Height :=height;
MG4.Width :=width;
with MG do
begin
canvas.brush.Color:=clWhite;
canvas.FloodFill(trunc(fwidth/2),trunc(fheight/4),Canvas.Pixels[trunc(fwidth/2),trunc(fheight/4)],fsSurface);
canvas.Pen.Color:=clblack;
Canvas.Arc(0,0,fwidth,fheight,0,trunc(fheight/2),0,trunc(fheight/2));
canvas.brush.Color:=bkcolor;
canvas.FloodFill(trunc(fwidth/2),trunc(fheight/2),Canvas.Pixels[trunc(fwidth/2),trunc(fheight/2)],fsSurface);
x:=trunc(fWidth/2*cos(pi/6)+fwidth/2);
y:=trunc(fWidth/2*sin(pi/6)+fwidth/2);
x1:=trunc(fWidth/2*cos(8*pi/12)+fwidth/2);
y1:=trunc(fWidth/2*sin(8*pi/12)+fwidth/2);
Canvas.MoveTo (x1,y1);
Canvas.LineTo (x,y);
x:=trunc(fWidth/2*cos(-pi/6)+fwidth/2);
y:=trunc(fWidth/2*sin(-pi/6)+fwidth/2);
x1:=trunc(fWidth/2*cos(-8*pi/12)+fwidth/2);
y1:=trunc(fWidth/2*sin(-8*pi/12)+fwidth/2);
Canvas.MoveTo (x1,y1);
Canvas.LineTo (x,y);
end; //with
with MG1 do
begin
canvas.Draw (0,0,MG);
canvas.Pen.Color:=bkcolor;
x:=trunc(fWidth/4*cos(-pi*3/4)+fwidth/2);
y:=trunc(fWidth/4*sin(-pi*3/4)+fwidth/2);
x1:=trunc(fWidth/4*cos(pi/4)+fwidth/2);
y1:=trunc(fWidth/4*sin(pi/4)+fwidth/2);
Canvas.MoveTo (x1,y1);
Canvas.LineTo (x,y);
canvas.Pen.Color:=rotorcolor;
canvas.Pen.Width :=2;
x:=trunc(fWidth/4*cos(pi/2)+fwidth/2);
y:=trunc(fWidth/4*sin(pi/2)+fwidth/2);
x1:=trunc(fWidth/4*cos(-pi/2)+fwidth/2);
y1:=trunc(fWidth/4*sin(-pi/2)+fwidth/2);
Canvas.MoveTo (x1,y1);
Canvas.LineTo (x,y);
canvas.Pen.Color:=rotorcolor;
Canvas.Arc(trunc(fwidth/2)-3,trunc(fheight/2)-3,trunc(fwidth/2)+3,trunc(fheight/2)+3,trunc(fwidth/2)-3,trunc(fheight/2),trunc(fwidth/2)-3,trunc(fheight/2));
TransparentMode:=tmAuto;
Transparent := true;
end;
with MG2 do
begin
canvas.Draw (0,0,MG);
canvas.Pen.Color:=bkcolor;
x:=trunc(fWidth/4*cos(pi/2)+fwidth/2);
y:=trunc(fWidth/4*sin(pi/2)+fwidth/2);
x1:=trunc(fWidth/4*cos(-pi/2)+fwidth/2);
y1:=trunc(fWidth/4*sin(-pi/2)+fwidth/2);
Canvas.MoveTo (x1,y1);
Canvas.LineTo (x,y);
canvas.Pen.Color:=rotorcolor;
canvas.Pen.Width :=2;
x:=trunc(fWidth/4*cos(pi*3/4)+fwidth/2);
y:=trunc(fWidth/4*sin(pi*3/4)+fwidth/2);
x1:=trunc(fWidth/4*cos(-pi/4)+fwidth/2);
y1:=trunc(fWidth/4*sin(-pi/4)+fwidth/2);
Canvas.MoveTo (x1,y1);
Canvas.LineTo (x,y);
canvas.Pen.Color:=rotorcolor;
Canvas.Arc(trunc(fwidth/2)-3,trunc(fheight/2)-3,trunc(fwidth/2)+3,trunc(fheight/2)+3,trunc(fwidth/2)-3,trunc(fheight/2),trunc(fwidth/2)-3,trunc(fheight/2));
TransparentMode:=tmAuto;
Transparent := true;
end;
with MG3 do
begin
canvas.Draw (0,0,MG);
canvas.Pen.Color:=bkcolor;
x:=trunc(fWidth/4*cos(pi*3/4)+fwidth/2);
y:=trunc(fWidth/4*sin(pi*3/4)+fwidth/2);
x1:=trunc(fWidth/4*cos(-pi/4)+fwidth/2);
y1:=trunc(fWidth/4*sin(-pi/4)+fwidth/2);
Canvas.MoveTo (x1,y1);
Canvas.LineTo (x,y);
Canvas.Pen.Color:=rotorcolor;
canvas.Pen.Width :=2;
x:=trunc(fWidth/4*cos(pi)+fwidth/2);
y:=trunc(fWidth/4*sin(pi)+fwidth/2);
x1:=trunc(fWidth/4*cos(0)+fwidth/2);
y1:=trunc(fWidth/4*sin(0)+fwidth/2);
Canvas.MoveTo (x1,y1);
Canvas.LineTo (x,y);
canvas.Pen.Color:=rotorcolor;
Canvas.Arc(trunc(fwidth/2)-3,trunc(fheight/2)-3,trunc(fwidth/2)+3,trunc(fheight/2)+3,trunc(fwidth/2)-3,trunc(fheight/2),trunc(fwidth/2)-3,trunc(fheight/2));
TransparentMode:=tmAuto;
Transparent := true;
end;
with MG4 do
begin
canvas.Draw (0,0,MG);
canvas.Pen.Color:=bkcolor;
x:=trunc(fWidth/4*cos(pi)+fwidth/2);
y:=trunc(fWidth/4*sin(pi)+fwidth/2);
x1:=trunc(fWidth/4*cos(0)+fwidth/2);
y1:=trunc(fWidth/4*sin(0)+fwidth/2);
Canvas.MoveTo (x1,y1);
Canvas.LineTo (x,y);
Canvas.Pen.Color:=rotorcolor;
canvas.Pen.Width :=2;
x:=trunc(fWidth/4*cos(-pi*3/4)+fwidth/2);
y:=trunc(fWidth/4*sin(-pi*3/4)+fwidth/2);
x1:=trunc(fWidth/4*cos(pi/4)+fwidth/2);
y1:=trunc(fWidth/4*sin(pi/4)+fwidth/2);
Canvas.MoveTo (x1,y1);
Canvas.LineTo (x,y);
canvas.Pen.Color:=rotorcolor;
Canvas.Arc(trunc(fwidth/2)-3,trunc(fheight/2)-3,trunc(fwidth/2)+3,trunc(fheight/2)+3,trunc(fwidth/2)-3,trunc(fheight/2),trunc(fwidth/2)-3,trunc(fheight/2));
TransparentMode:=tmAuto;
Transparent := true;
end;
image1.Picture.Bitmap :=MG1;
image2.Picture.Bitmap :=MG2;
image3.Picture.Bitmap :=MG3;
image4.Picture.Bitmap :=MG4 ;
image5.Picture.Bitmap :=MG;



end;


end.

 
其实图像并不一样,只是图像都重叠在了一起,显示的是最后一幅图像而已!
 
本程序在DELPHI5下调试。经过调试发现第二次按BUTTON 时MG.HANDLE,MG1.HANDLE
,MG2.HANDLE,MG3.HAND.E,MG4.HANDLE相等了!(当然,第一次它们是不相等的)。所以
JAMS的回答不正确!
 
太粗心了!
if (assigned(MG1)) then
MG.Destroy ; //----> MG1.Destroy ????
MG1:=nil;
MG1:=Tbitmap.Create ;
MG1.Height :=height;
MG1.Width :=width;
if (assigned(MG2)) then
MG.Destroy ; //----> MG2.Destroy ????
...
...



 
最好用Free方法而不是Destroy,这是帮助文件中说的
 
一个过客说的对!可我迷惑的是为什么,经过调试发现第二次按BUTTON 时MG.HANDLE,MG1.HANDLE
,MG2.HANDLE,MG3.HAND.E,MG4.HANDLE相等了!(当然,第一次它们是不相等的)。还是给你
100分吧!谁然不是我要的最后的答案。
 

Similar threads

顶部