4个菜单项、颜色对话框
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus;
type
TMainForm = class(TForm)
MainMenu1: TMainMenu;
sadf1: TMenuItem;
N11: TMenuItem;
N21: TMenuItem;
N31: TMenuItem;
N41: TMenuItem;
ColorDialog1: TColorDialog;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure N11Click(Sender: TObject);
procedure N21Click(Sender: TObject);
procedure N31Click(Sender: TObject);
procedure N41Click(Sender: TObject);
private
{ Private declarations }
ThreadList :TList;
public
{ Public declarations }
end;
TDrawThread=Class(TThread)
private
FColor :TColor;
FForm :TForm;
public
Constructor Create(AForm :TForm;
AColor :TColor);
procedure Execute;override;
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
Constructor TDrawThread.Create(AForm :TForm;
AColor :TColor);
begin
FColor :=AColor;
FForm :=AForm;
Inherited Create(False);
end;
procedure TDrawThread.Execute;
var
p1,p2 :TPoint;
procedure GetRandomColor;
var
MaxX,MaxY :Integer;
begin
MaxX :=FForm.ClientWidth;
MaxY :=FForm.ClientHeight;
p1.x :=Random(MaxX);
p1.y :=Random(MaxY);
p2.x :=Random(MaxX);
p2.y :=Random(MaxY);
end;
begin
FreeOnTerminate :=True;
While not (Terminated or Application.Terminated)do
begin
GetRandomColor;
with FForm.Canvasdo
begin
Lock;
Pen.Color :=FColor;
MoveTo(p1.x,p1.y);
LineTo(p2.x,p2.y);
unLock;
end;
end;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
ThreadList :=TList.Create;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
N41Click(nil);
ThreadList.Free;
end;
procedure TMainForm.N11Click(Sender: TObject);
begin
if ColorDialog1.Execute then
ThreadList.Add(TDrawThread.Create(Self,ColorDialog1.Color));
end;
procedure TMainForm.N21Click(Sender: TObject);
begin
TDrawThread(ThreadList[ThreadList.Count-1]).Terminate;
ThreadList.Delete(ThreadList.Count-1);
end;
procedure TMainForm.N31Click(Sender: TObject);
var
I :Integer;
begin
for I :=1 to 30do
ThreadList.Add(TDrawThread.Create(Self,Random(MaxInt)));
end;
procedure TMainForm.N41Click(Sender: TObject);
var
I :Integer;
begin
Cursor :=crHourGlass;
try
for I :=ThreadList.Count-1do
wnto 0do
begin
TDrawThread(ThreadList).Terminate;
TDrawThread(ThreadList).WaitFor;
end;
ThreadList.Clear;
finally
Cursor :=crDefault;
end;
end;
initialization
Randomize;
end.