Designing the Application |
|
- Start Borland C++ Builder with the default form
- Change the Caption of the form to Traffic Light
- Change its BorderStyle property to bsDialog
- Change its dimensions to Height = 256 and Width = 112
- From the Statndard tab of the Component Palette, double-click the Panel
control
- Change the properties of the panel as follows:
BevelInner = bvNone
BevelOuter = bvLowered
BevelWidth = 5
Caption = Empty
Color =
clBlack
Height = 209
Left = 17
Top = 8
Width =
81
- While the panel is still selected on the form, from the Additional tab of
the Component Palette, double-click the Shape control
- Change the following properties for the new Shape:
Brush: Color = clGray
Brush: Style = bsSolid
Height
=
65
Left
= 8
Top
= 8
Width
= 65
- On the panel, click the newly added Shape to make sure it is selected.
Press Ctrl + C to copy it to the clipboard
- Click anywhere on the panel to select it.
- Press Ctrl + V to paste the Shape
- While the new Shape is still selected, change its Left value to 8 and its Top value to 72
- Click an empty area on the panel to select it and press Ctrl + V.
- While the new shape is still selected, change its Left value to 8 and its Top value to 136
- Click the panel on the form and ress Ctrl + V again
- Change the properties of the new shape as follows:
Brush: Color = clRed
Height =
57
Left
= 8
Name
= shpRed
Shape =
stCircle
Top
= 12
- On the form, click the red circle and press Ctrl + C. Then press Ctrl + V to add a new Shape
- Change its properties as follows:
Brush: Color = clSilver
Height =
57
Left
= 8
Name
= shpYellow
Shape
= stCircle
Top
= 76
- On the form, click the Panel and press Ctrl + V
- Change the properties of the new Shape to:
Brush: Color = clSilver
Height =
57
Left
= 8
Name =
shpGreen
Shape =
stCircle
Top
= 140
- From the System tab of the Component Palette, double-click Timer
- On the Object Inspector, click the Events tab
- Although the dialog box will be equipped with the system Close button, we
should provide our own meand of closing the application.
On the form, click the red circle.
- Press and hold Shift, then click the other two circles
- On the Object Inspector, double-click the empty area on the right side of
OnMouseDown
- Change the event as follows:
//---------------------------------------------------------------------------
void __fastcall TForm1::shpGreenMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
Close();
}
//---------------------------------------------------------------------------
|
- Press F12 to display the form.
- Double-click the Timer on the form to access its OnTimer event
- Change its code as follows:
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
// If the current color is red
if( shpRed->Brush->Color == clRed )
{
// Change the color to green
Timer1->Interval = 3500;
shpRed->Brush->Color = clSilver;
shpYellow->Brush->Color = clSilver;
shpGreen->Brush->Color = clGreen;
}
// But if the color is green
else if( shpGreen->Brush->Color == clGreen )
{
// Change the color to yellow
Timer1->Interval = 2000;
shpRed->Brush->Color = clSilver;
shpYellow->Brush->Color = clYellow;
shpGreen->Brush->Color = clSilver;
}
// Otherwise, if the color is yellow
else // if(shpYellow->Brush->Color == clYellow
{
// Change the color to red
Timer1->Interval = 5000;
shpRed->Brush->Color = clRed;
shpYellow->Brush->Color = clSilver;
shpGreen->Brush->Color = clSilver;
}
}
//---------------------------------------------------------------------------
|
- Test your program.
|
|