使用vs2019C++MFC来 设计一个交通信号灯类,使得交通信号灯对象有红灯、绿灯、黄灯三个状态。信号灯的尺寸、状态可以设定,并能显示到指定的可视窗口中。 信号灯具有倒计时功能,能够设定三种状态的保持时间,启动后能够持续运行。
时间: 2024-02-22 16:57:36 浏览: 70
好的,我可以为您提供一个简单的交通信号灯类的设计方案。首先,您需要创建一个名为“TrafficLight”的类,并在头文件中定义以下成员变量和函数:
```c++
class TrafficLight {
public:
TrafficLight();
~TrafficLight();
void SetSize(int width, int height);
void SetPosition(int x, int y);
void SetColor(int color);
void SetTime(int redTime, int greenTime, int yellowTime);
void Draw(CDC* pDC);
private:
int m_x, m_y; // 信号灯的位置
int m_width, m_height; // 信号灯的尺寸
int m_color; // 信号灯的颜色,0表示红灯,1表示绿灯,2表示黄灯
int m_redTime, m_greenTime, m_yellowTime; // 信号灯三种状态的持续时间
int m_currentTime; // 当前状态的剩余时间
CBrush m_brush; // 画刷,用于绘制信号灯的颜色
};
```
在实现文件中,您需要定义构造函数和析构函数:
```c++
TrafficLight::TrafficLight()
{
m_x = 0;
m_y = 0;
m_width = 30;
m_height = 90;
m_color = 0;
m_redTime = 10;
m_greenTime = 20;
m_yellowTime = 5;
m_currentTime = m_redTime;
m_brush.CreateSolidBrush(RGB(255, 0, 0)); // 初始颜色为红色
}
TrafficLight::~TrafficLight()
{
m_brush.DeleteObject();
}
```
然后,您需要定义一些设置函数来设置信号灯的属性:
```c++
void TrafficLight::SetSize(int width, int height)
{
m_width = width;
m_height = height;
}
void TrafficLight::SetPosition(int x, int y)
{
m_x = x;
m_y = y;
}
void TrafficLight::SetColor(int color)
{
m_color = color;
switch (m_color) {
case 0:
m_brush.DeleteObject();
m_brush.CreateSolidBrush(RGB(255, 0, 0)); // 红色
m_currentTime = m_redTime;
break;
case 1:
m_brush.DeleteObject();
m_brush.CreateSolidBrush(RGB(0, 255, 0)); // 绿色
m_currentTime = m_greenTime;
break;
case 2:
m_brush.DeleteObject();
m_brush.CreateSolidBrush(RGB(255, 255, 0)); // 黄色
m_currentTime = m_yellowTime;
break;
}
}
void TrafficLight::SetTime(int redTime, int greenTime, int yellowTime)
{
m_redTime = redTime;
m_greenTime = greenTime;
m_yellowTime = yellowTime;
m_currentTime = m_redTime; // 初始化为红灯状态
}
```
最后,您需要定义一个绘制函数来绘制信号灯:
```c++
void TrafficLight::Draw(CDC* pDC)
{
CBrush* pOldBrush = pDC->SelectObject(&m_brush);
// 绘制信号灯外框
pDC->Rectangle(m_x, m_y, m_x + m_width, m_y + m_height);
// 绘制信号灯内部三个圆形
int radius = m_width / 3;
int centerX = m_x + m_width / 2;
int centerY = m_y + m_height / 6;
pDC->Ellipse(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
centerY = m_y + m_height / 2;
pDC->Ellipse(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
centerY = m_y + m_height * 5 / 6;
pDC->Ellipse(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
pDC->SelectObject(pOldBrush);
}
```
在主窗口中,您可以创建一个TrafficLight对象,并调用其函数来设置属性和绘制信号灯:
```c++
TrafficLight trafficLight;
trafficLight.SetSize(30, 90);
trafficLight.SetPosition(100, 100);
trafficLight.SetTime(10, 20, 5);
trafficLight.SetColor(0);
trafficLight.Draw(pDC);
```
最后,您需要使用定时器来实现倒计时功能。在主窗口的OnInitDialog()函数中,您可以添加以下代码:
```c++
// 创建一个ID为1的定时器,每秒钟触发一次
SetTimer(1, 1000, NULL);
```
然后,在主窗口的OnTimer()函数中,您可以添加以下代码:
```c++
void CMyDialog::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == 1) {
trafficLight.m_currentTime--;
if (trafficLight.m_currentTime == 0) {
switch (trafficLight.m_color) {
case 0: // 红灯状态结束,切换到绿灯状态
trafficLight.SetColor(1);
break;
case 1: // 绿灯状态结束,切换到黄灯状态
trafficLight.SetColor(2);
break;
case 2: // 黄灯状态结束,切换到红灯状态
trafficLight.SetColor(0);
break;
}
}
Invalidate();
}
CDialogEx::OnTimer(nIDEvent);
}
```
这样,您就可以在主窗口中创建一个交通信号灯对象,并在定时器的帮助下实现倒计时功能了。
阅读全文