close

Вход

Забыли?

вход по аккаунту

?

Отчет№2

код для вставкиСкачать

Рисунок 1 - Окна приложения Пример№1
Код основного модуля
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ExtCtrls, StdCtrls,MoveThread,syncobjs;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
PaintBox1: TPaintBox;
TrackBar1: TTrackBar;
TrackBar2: TTrackBar;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
procedure TrackBar2Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
Thread1,
Thread2 :TMoveThread;
c:TCriticalSection;
{ Public declarations }
end;
var
Form1: TForm1;
Taxi00, Taxi01,rtaxi00,rtaxi01: TBitmap;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if Thread1.Suspended=true then Thread1.Resume;
if Thread2.Suspended=true then Thread2.Resume;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if Thread1.Suspended=false then Thread1.Suspend;
if Thread2.Suspended=false then Thread2.Suspend;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
c:=TCriticalsection.Create;
taxi00:=tbitmap.Create;
taxi00.LoadFromFile('taxi00.bmp');
rtaxi00:=tbitmap.Create;
rtaxi00.LoadFromFile('rtaxi00.bmp');
taxi01:=tbitmap.Create;
taxi01.LoadFromFile('taxi01.bmp');
rtaxi01:=tbitmap.Create;
rtaxi01.LoadFromFile('rtaxi01.bmp');
thread1:=TMovethread.Create(taxi00,taxi01,PaintBox1,c);
thread1.Priority:=tplowest;
thread2:=TMovethread.Create(rtaxi00,rtaxi01,PaintBox1,c);
thread2.Priority:=tplowest;
end;
procedure TForm1.TrackBar1Change(Sender: TObject);
var I: integer;
priority:TThreadPriority;
begin
priority:=tpIdle;
for i:=0 to ((sender as ttrackbar).position -1) do inc(priority);
if sender=trackbar1 then thread1.priority:=priority
else thread2.Priority:=priority;
end;
procedure TForm1.TrackBar2Change(Sender: TObject);
var I: integer;
priority:TThreadPriority;
begin
priority:=tpIdle;
for i:=0 to ((sender as ttrackbar).position -1) do inc(priority);
if sender=trackbar1 then thread2.priority:=priority
else thread1.Priority:=priority;
end;
end.
Код модуля MoveThread
unit MoveThread;
interface
uses
Classes,Graphics, ExtCtrls,syncobjs;
type
TMoveThread = class(TThread)
private
FBox: TPaintBox; // Где воспроизводится картинка
b0,b1: tbitmap; // Сами картинки
i:integer; // Для определения координат картинки
k1:TCriticalSection;
procedure DoVisualSwap; // Одна процедура двигает вправо,
procedure DoVisual; // Другая влево
{ Private declarations }
protected
procedure Execute; override;
public
constructor Create(a,b: TBitmap; c:TPaintBox;k:TCriticalSection);
// destructor Destroy; override;
end;
implementation
constructor TMoveThread.Create(a,b:tbitmap; c:TPaintBox;k:TCriticalSection);
begin
b0:=a;b1:=b;Fbox:=c;i:=0;k1:=k;
inherited Create(False);
end;
destructor TMoveThread.Destroy;
begin
b0.free;
b1.free;
inherited Destroy;
end;
procedure TMoveThread.DoVisualSwap;
begin
{ Двигает картинку вправо }
with FBox do
begin
canvas.Draw(i,0,b0);
inc(i);
end;
end;
procedure TMoveThread.DoVisual;
begin
{ Двигает картинку влево }
with FBox do
begin
canvas.Draw(i,0,b1);
dec(i);
end;
end;
{ TMoveThread }
procedure TMoveThread.Execute;
begin
while true do
begin
k1.Enter;
try
while i<=(fbox.Width+b0.Width) do Synchronize(DoVisualSwap);
finally k1.Leave;
end;
k1.Enter;
try
while i>=(0-b0.Width) do Synchronize(DoVisual);
finally k1.Leave;
end;
end;
end;
end.
Пример№3.Событие
Рисунок - Окна приложения Пример№3
Код основного модуля
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls, StdCtrls, Spin, Mythread;
type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
PaintBox2: TPaintBox;
Button1: TButton;
Button2: TButton;
SpinEdit1: TSpinEdit;
SpinEdit2: TSpinEdit;
SpinEdit3: TSpinEdit;
Label1: TLabel;
Label2: TLabel;
TrackBar1: TTrackBar;
TrackBar2: TTrackBar;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
procedure TrackBar2Change(Sender: TObject);
procedure SpinEdit3Change(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Thread1, Thread2:TMoveThread;
taxi00,taxi01,rtaxi00,rtaxi01:TBitMap;
Time: Integer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if Thread1.Suspended=true then Thread1.Resume;
if Thread2.Suspended=true then Thread2.Resume
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if Thread1.Suspended=false then Thread1.Suspend;
if Thread2.Suspended=false then Thread2.Suspend;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Taxi00.free;
Taxi01.free;
inherited Destroy;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
taxi00:=tbitmap.Create;
taxi00.LoadFromFile('taxi01.bmp');
rtaxi00:=tbitmap.Create;
rtaxi00.LoadFromFile('rtaxi00.bmp');
rtaxi01:=tbitmap.Create;
rtaxi01.LoadFromFile('rtaxi01.bmp');
taxi01:=tbitmap.Create;
taxi01.LoadFromFile('taxi00.bmp');
thread1:=TMovethread.Create(taxi00,taxi01,PaintBox1);
thread1.Priority:=tplowest;
thread2:=TMovethread.Create(rtaxi00,rtaxi01,PaintBox2);
thread2.Priority:=tplowest;
Time:=1000; SpinEdit3.Value:=Time;
SpinEdit1.Value:=50;SpinEdit2.Value:=90;
end;
procedure TForm1.SpinEdit3Change(Sender: TObject);
begin
Time:=SpinEdit3.Value;
Timer1.Interval := Time;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var m:integer;
begin
if (random<=SpinEdit1.Value/100) then
begin
thread1.count:=1;
thread1.e.SetEvent;
Label1.Caption:='Пассажиры - '+IntToStr(Thread1.count);
end
else
begin
thread1.count:=0;
Label1.Caption:='Пассажиры - '+IntToStr(Thread1.count);
end;
if (random<=SpinEdit2.Value/100) then
begin
thread2.count:=1;
thread2.e.SetEvent;
Label2.Caption:='Пассажиры - '+IntToStr(Thread2.count);
end
else
begin
thread2.count:=0;
Label2.Caption:='Пассажиры - '+IntToStr(Thread2.count);
end;
end;
procedure TForm1.TrackBar1Change(Sender: TObject);
var I: integer;
priority:TThreadPriority;
begin
priority:=tpIdle;
for i:=0 to ((sender as ttrackbar).position -1) do inc(priority);
if sender=trackbar2 then thread2.priority:=priority
else thread1.Priority:=priority;
end;
procedure TForm1.TrackBar2Change(Sender: TObject);
var I: integer;
priority:TThreadPriority;
begin
priority:=tpIdle;
for i:=0 to ((sender as ttrackbar).position -1) do inc(priority);
if sender=trackbar1 then thread1.priority:=priority
else thread2.Priority:=priority;
end;
end.
Код модуля Mythread
unit Mythread;
interface
uses
Classes,Graphics, ExtCtrls,SYNCOBJS;
type
TMoveThread = class(TThread)
private
{ Private declarations }
FBox: TPaintBox; // Где воспроизводится картинка
b0,b1,staxi00: tbitmap; // Сами картинки
j:integer; // Для определения координат картинки
procedure DoVisualSwap; // Одна процедура двигает вправо,
procedure DoVisual; // Другая влево
protected
procedure Execute; override;
public
i,count:integer;
e:TEvent;
constructor Create(a,b: TBitmap; c:TPaintBox);
destructor Destroy; override;
end;
implementation
procedure TMoveThread.DoVisualSwap;{Двигает картинку вправо}
begin
with FBox do
begin
canvas.Draw(j,0,b0);
inc(j);
end;
end;
procedure TMoveThread.DoVisual; { Двигает картинку влево }
begin
with FBox do
begin
canvas.Draw(j,0,b1);
dec(j);
end;
end;
constructor TMoveThread.Create(a,b:tbitmap; c:TPaintBox);
begin
b0:=a;b1:=b;Fbox:=c;//i:=0;
inherited Create(False);
end;
destructor TMoveThread.Destroy;
begin
b0.free;
b1.free;
inherited Destroy;
end;
{ TMoveThread }
procedure TMoveThread.Execute;
begin
{ Place thread code here }
e:=tevent.create(nil,True,false,'e');
while true do
begin
if count=0 then
begin
e.ResetEvent;
e.WaitFor(1000);
end
else
begin
while j<=(fbox.Width+b0.Width) do Synchronize(DoVisualSwap);
while j>=(0-b0.Width) do Synchronize(DoVisual);
end;
end;
e.Free;
end;
end.
Документ
Категория
Рефераты
Просмотров
17
Размер файла
166 Кб
Теги
отчет
1/--страниц
Пожаловаться на содержимое документа