Лабораторная работа № 3
Задание: создание собственного класса на базе TPaintBox
Создать новый класс TAngle, наследник класса TpaintBox.
Задание аналогично заданиям из лабораторной работы № 2
Код программы:
Unit 1:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, ComCtrls, Unit2;
type
TMyForm = class(TForm)
ColorDialog1: TColorDialog;
Edit1: TEdit;
Button_1: TButton;
Edit2: TEdit;
Button_2: TButton;
Button_3: TButton;
Button_5: TButton;
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
UpDown1: TUpDown;
RadioGroup1: TRadioGroup;
procedure FormCreate(Sender: TObject);
procedure Button_1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button_5Click(Sender: TObject);
procedure UpDown1ChangingEx(Sender: TObject; var AllowChange: Boolean;
NewValue: Smallint; Direction: TUpDownDirection);
procedure Button_2Click(Sender: TObject);
procedure Button_3Click(Sender: TObject);
procedure RadioGroup1Click(Sender: TObject);
private
public
end;
{ Public declarations }
var
MyForm: TMyForm;
XPos,YPos:real;
Angle:TAngle;
Angle2:TAngle;
c2:Tcolor;
nazh, nazh2:boolean;
colorCheck:boolean;
implementation
{$R *.dfm}
procedure TMyForm.FormCreate(Sender: TObject);
begin
Angle:=TAngle.Create(0,0,130,130);
Angle.Parent:=MyForm;
Angle2:=TAngle.Create(0,0,130,130);
Angle2.Parent:=MyForm;
end;
procedure TMyForm.Button_1Click(Sender: TObject);
begin
if (RadioGroup1.ItemIndex = 0) {and (nazh = true)} then
begin
Angle.SetValue(strtoint(Edit1.Text));
Angle.Draw(Sender);
end;
if (RadioGroup1.ItemIndex = 1) then
begin
Angle2.SetValue(strtoint(Edit1.Text));
Angle2.Draw(sENDER);
end;
end;
procedure TMyForm.Button1Click(Sender: TObject);
begin
if (radiogroup1.ItemIndex = 0) and (ColorDialog1.Execute = true) then
begin
Angle.C:=ColorDialog1.Color;
Angle.Draw(Sender);
end;
if (radiogroup1.ItemIndex = 1) and (ColorDialog1.Execute = true) then
begin
Angle2.C:=ColorDialog1.Color;
Angle2.Draw(Sender);
end;
end;
procedure TMyForm.Button_5Click(Sender: TObject);
begin
Canvas.Brush.Color:=clwhite;
Canvas.Rectangle(0,0,130,130);
Canvas.MoveTo(0, 65);
Canvas.LineTo(130, 65);
Canvas.MoveTo(65, 0);
Canvas.LineTo(65, 130);
Canvas.MoveTo(120,62);
Canvas.LineTo(127, 65);
Canvas.MoveTo(120, 68);
Canvas.LineTo(127, 65);
end;
procedure TMyForm.UpDown1ChangingEx(Sender: TObject;
var AllowChange: Boolean; NewValue: Smallint;
Direction: TUpDownDirection);
begin
if (Direction = updUP) and (strtoint(Edit2.Text) >= 0) then
Edit2.Text:=inttostr(strtoint(Edit2.Text)+1);
if (Direction = updDown) and (strtoint(Edit2.Text) > 0) then
Edit2.Text:=inttostr(strtoint(Edit2.Text)-1);
end;
procedure TMyForm.Button_2Click(Sender: TObject);
begin
Edit1.Text:=inttostr(strtoint(Edit2.Text)+strtoint(Edit1.Text));
Button_1Click(Sender);
end;
procedure TMyForm.Button_3Click(Sender: TObject);
begin
Edit1.Text:=inttostr(strtoint(Edit1.Text)-strtoint(Edit2.Text));
Button_1Click(Sender);
end;
procedure TMyForm.RadioGroup1Click(Sender: TObject);
begin
if (RadioGroup1.ItemIndex = 0) {and (nazh)} then
Angle.Draw(Sender);
if (RadioGroup1.ItemIndex = 1) {and (nazh2)} then
Angle2.Draw(Sender);
end;
end.
Unit 2:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, ComCtrls;
Type TAngle=class (TPaintBox)
private
ColorDialog1: TColorDialog;
Value:integer;
public
C:Tcolor;
constructor Create(X,Y, W,H:integer);
procedure SetValue(NewValue:integer);
function Plus (Increment:integer):integer;
function Minus(Decrement:integer):integer;
procedure Draw(Sender:TObject);
destructor Destroy; override;
end;
var s15:string;
implementation
constructor TAngle.Create(X,Y,W,H:integer);
begin
inherited Create(nil);
top:=x;
Left:=Y;
Width:=W;
Height:=H;
OnPaint:=Draw;
Value:=0;
c:=clYellow;
end;
function TAngle.Plus (Increment:integer):integer;
begin
Value:= (Value + Increment) mod 360;
result:=Value;
end;
function TAngle.Minus (Decrement:integer):integer;
begin
Value:= (Value - Decrement) mod 360;
result:=Value;
end;
procedure TAngle.SetValue(NewValue:integer);
begin
Value:=(NewValue) mod 360;
End;
destructor TAngle.Destroy;
begin
inherited Destroy;
end;
procedure TAngle.Draw(Sender: TObject);
var w,h,w2,h2:integer;
begin
w:=Width;
H:=Height;
W2:=round(Width/2);
H2:=round(Height/2);
Canvas.Brush.Color:=clwhite;
Canvas.Rectangle(0,0,W,H);
Canvas.Brush.Color:=C;
Canvas.Pie(0,0,W,H,
W2+round(W2*cos(Value*pi/180)),H2+round(H2*sin(Value*pi/180)),W,H2+1);
Canvas.MoveTo(0, 65);
Canvas.LineTo(130, 65);
Canvas.MoveTo(65, 0);
Canvas.LineTo(65, 130);
Canvas.MoveTo(120,62);
Canvas.LineTo(127, 65);
Canvas.MoveTo(120, 68);
Canvas.LineTo(127, 65);
if (value = 0) or (value = 1) then
begin
Canvas.Brush.Color:=clwhite;
Canvas.Rectangle(0,0,W,H);
Canvas.MoveTo(0, 65);
Canvas.LineTo(130, 65);
Canvas.MoveTo(65, 0);
Canvas.LineTo(65, 130);
Canvas.MoveTo(120,62);
Canvas.LineTo(127, 65);
Canvas.MoveTo(120, 68);
Canvas.LineTo(127, 65);
end;
s15:=inttostr(value);
Canvas.TextOut(1,1,s15);
end;
end.
Скриншот незапущенной программы:
Скриншот запущенной программы: