ребят пишу светофор. впринципе все работает. есть небольшие тонкости, которые я все еще не знаю. Помогите пожалуйста.
Вот код компонента:
Код:
unit UnitLightSwitcher;
interface
uses
SysUtils, Classes, Controls, ExtCtrls, Graphics, Windows, Dialogs;
type
TMyPaintBox = class(TPaintBox)
private
MySpeed: integer;
MyBgColor, MyGreenColor, MyYellowColor, MyRedColor: TColor;
GreenYellow, YellowRed, RedYellow, YellowGreen: boolean;
Timer1: TTimer;
FOnRed: TNotifyEvent;
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent);
procedure Paint; override;
procedure SwitchYellow();
procedure SwitchRed();
procedure SwitchGreen();
procedure MyTimerEventHandler(Sender: TObject);
procedure SetMySpeed(arg: integer);
published
property Speed: integer write SetMySpeed default 1000;
property OnRed: TNotifyEvent read FOnRed write FOnRed;
property Height default 300;
property Width default 100;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [TMyPaintBox]);
end;
constructor TMyPaintBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
MyBgColor:=clBlack;
MyGreenColor:=RGB(0, 255, 0);//clGreen;
MyYellowColor:=RGB(190, 150, 0);
MyRedColor:=RGB(190, 43, 43);
MySpeed:=100;
Timer1:=TTimer.Create(Self);
Timer1.Interval:=MySpeed;
Timer1.Enabled := true;
Timer1.OnTimer := MyTimerEventHandler;
YellowGreen:=true; // ecia?aeuii ai?eo caeaiiue, oi anou yeiau i?iecioei ia?aee??aiea n ?aeoiai ia caeaiue
end;
procedure TMyPaintBox.Paint;
begin
inherited;
Canvas.Brush.Color:=MyBgColor;
Canvas.FillRect(Rect(0,0,100,300));
Canvas.Brush.Color:=MyRedColor;
Canvas.Ellipse(Rect(0, 0, 100, 100));
Canvas.Brush.Color:=MyYellowColor;
Canvas.Ellipse(Rect(0, 100, 100, 200));
Canvas.Brush.Color:=MyGreenColor;
Canvas.Ellipse(Rect(0, 200, 100, 300));
end;
procedure TMyPaintBox.SwitchYellow();
begin
MyGreenColor:=clGreen;
MyYellowColor:=clYellow;
MyRedColor:=RGB(190, 43, 43);
Paint;
end;
procedure TMyPaintBox.SwitchRed();
begin
if Assigned(OnRed) then OnRed(Self);
MyGreenColor:=clGreen;
MyYellowColor:=RGB(190, 150, 0);
MyRedColor:=clRed;
Paint;
end;
procedure TMyPaintBox.SwitchGreen();
begin
MyGreenColor:=RGB(0, 255, 0);//clGreen;
MyYellowColor:=RGB(190, 150, 0);
MyRedColor:=RGB(190, 43, 43);
Paint;
end;
procedure TMyPaintBox.MyTimerEventHandler(Sender: TObject);
begin
if (YellowGreen=true) then // anee iineaaiee ?ac aue ia?aoia n ?aeoiai ia caeaiue,
begin // oi iaai naaeaou ia?aoia n caeaiiai ia ?aeoue
YellowGreen:=false; // noe?aai eioi?iaoe? i oii, ?oi iineaaiee ?ac ia?aoia aue n ?aeoiai ia caeaiue
GreenYellow:=true; // iiia?aai ?oi nae?an i?iecaaaai ia?aoia n caeaiiai ia ?aeoue
SwitchYellow();
end
else if(YellowRed=true) then // anee ?a iineaaiee ia?aoia i?iecaiaeeny n ?aeoiai ia e?aniue,
begin // oi aaeaai ia?aoia n e?aniiai ia ?aeoue
YellowRed:=false;
RedYellow:=true; // iiia?aai ?oi nae?an i?iecieaao ia?aoia n e?aniiai ia ?aeoue
SwitchYellow();
end
else if (GreenYellow=true) then // anee iineaaiee ia?aoia aue n caeaiiai ia ?aeoue, oi eiaai i?aai ca?a?u e?aniue, eia?a ie?aai ia aaeaai
begin
GreenYellow:=False; // noe?aai eioi?iaoe? i iineaaiai ia?aoiaa
YellowRed:=true; // iiia?aai ?oi nae?an naaeaai ia?aoia n ?aeoiai ia e?aniue
SwitchRed();
end
else if (RedYellow=true) then
begin
RedYellow:=false;
YellowGreen:=true;
SwitchGreen();
end;
end;
procedure TMyPaintBox.SetMySpeed(arg: integer);
begin
MySpeed:=arg;
Timer1.Interval:=arg;
end;
end.
Если вы заметили. то я свойству Width И Heigth задал дефолт-значение 100 и 300. Но когда я помещаю пэйнтбокс на форму, ее размеры по прежнему 105 на 105, как у "папы"-пэйнтбокса.
И событие OnRed не срабатывает. Не знаю почему... Спасибо.