题目: 电梯仿真程序设计
目录
课程设计报告文档 1
题目: 电梯仿真程序设计 1
一.引言 1
二.任务的描述 1
三.任务设计 2
(1)类1:调度队列类(PlanQuene)接口: 5
(2)类2:电梯类(MyElevator)接口: 5
(3)类3:按钮类(CMyButton)接口: 5
(4)类4:显示对话框类(CElevatorDlg)接口: 5
四.编写代码 6
1.问题1 6
2.问题2 6
3. 问题3 6
4.问题4 6
五.运行结果与分析 7
六、感想认识 8
一.引言
1.编写目的:根据实际电梯的运行状况进行需求分析,运用面向对象的思想,结合MFC设计一个仿真电梯。电梯能完成正常的运作,多楼层请求。面对一个现实世界的电梯,将其抽象成一个或多个类,每个类编写相应的接口,体会面向对象的封装性、继承性、多态性。
2.定义:View(前台视图层)、Bean(后台业务逻辑层)、PlanQueue(调度队列) 、 Timer(计时器)
3.参考资料:1、《C++语言程序设计》(清华大学出版社)郑莉(著)
2、《深入浅出MFC》(华中科技大学出版社)侯俊杰(著)
3、《C++ primer中文版》(人民邮电出版社)李师贤(译)
4、《UML面向对象建模与设计》pdf版
二.任务的描述
1.目标:1)、根据实际电梯的运行状况进行需求分析;
2)、抽象电梯运行算法;
3)、设计电梯运行程序及人机交互界面;
4)、模块功能满足要求、界面友好、具有一定的健壮性;
2.功能描述:电梯可以接受各个楼层的向上、向下请求。人在电梯内,可以 通过电梯内的按钮到达想要去的层数。当电梯向上运行时,接受 当前电梯所在楼层之上的向上请求,在相应的楼层处停下。当电 梯向下运行时,接受当前楼层之下的向下请求。电梯门开,然后 进出人,电梯最大承载人数为15,不能超载。分别用了两个Timer 来控制电梯的运行和电梯门的开关。
3.性能描述
(1)数据精确度:不存在复杂数据之间的转换,精确度100%
(2)时间特性:更新处理时间:电梯上下40ms,开关门30ms
4.运行环境:硬件:装有Windows XP的计算机。
软件:Visual C++ 6.0
5.条件与限制:同时具备硬件和软件条件
三.任务设计
1.类的划分:本系统一共有四个类:电梯类(MyElevator)、按钮类(CMyButton)、调度队列类(PlanQuene)、显示对话框类(CElevatorDlg)。
系统分为两层结构:View层、Bean层。大致框架如图:
// MyButton.cpp : implementation file
//
#include "stdafx.h"
#include "Elevator.h"
#include "MyButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CMyButton
CMyButton::CMyButton()
{
b_InRect = false;
b_Flag = false;
m_ForeColor = RGB(0,0,0); //文字颜色
m_BkColor = RGB(243,243,243); //背景色
m_LockForeColor = GetSysColor(COLOR_GRAYTEXT); //锁定按钮的文字颜色
}
CMyButton::~CMyButton()
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
//{{AFX_MSG_MAP(CMyButton)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CMyButton message handlers
//画正常按钮
void CMyButton::NormalButton(CDC *pDC)
{
CBrush Brush;
Brush.CreateSolidBrush( m_BkColor );
pDC->SelectObject( &Brush );
pDC->RoundRect(&m_ButRect,CPoint(5,5)); //画圆角矩形的外框
CPen Pen;
Pen.CreatePen(PS_SOLID, 1, RGB(208,235,249) );
pDC->SelectObject( &Pen );
pDC->RoundRect(m_ButRect.left+2, m_ButRect.top+2,
m_ButRect.right-2, m_ButRect.bottom-2, 5, 5); //画圆角矩形的内边
pDC->SetTextColor( m_ForeColor ); //画文字
pDC->SetBkMode( TRANSPARENT );
pDC->TextOut( m_textPt.x, m_textPt.y, m_strText );
}
//画鼠标经过时的按钮
void CMyButton::PassButton(CDC *pDC)
{
CBrush Brush;
Brush.CreateSolidBrush( m_BkColor );
pDC->SelectObject( &Brush );
pDC->RoundRect(&m_ButRect,CPoint(5,5)); //画圆角矩形的外框
CPen Pen;
Pen.CreatePen(PS_SOLID, 2, RGB(255,208,100) );
pDC->SelectObject( &Pen );
pDC->RoundRect(m_ButRect.left+2, m_ButRect.top+2,
m_ButRect.right-2, m_ButRect.bottom-2, 5, 5); //画圆角矩形的内边
pDC->SetTextColor( m_ForeColor ); //画文字
pDC->SetBkMode( TRANSPARENT );
pDC->TextOut( m_textPt.x, m_textPt.y, m_strText );
}
//画锁定的按钮
void CMyButton::LockButton(CDC *pDC)
{
CBrush Brush;
Brush.CreateSolidBrush( m_BkColor );
pDC->SelectObject( &Brush );
pDC->RoundRect(&m_ButRect,CPoint(5,5)); //画圆角矩形的外框
pDC->SetBkMode( TRANSPARENT ); //画文字
pDC->SetTextColor( m_LockForeColor );
pDC->TextOut( m_textPt.x, m_textPt.y, m_strText );
}
//设置文本颜色
void CMyButton::SetForeColor(COLORREF color)
{
m_ForeColor = color;
}
//设置背景颜色
void CMyButton::SetBkColor(COLORREF color)
{
m_BkColor = color;
}
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CDC *pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
m_ButRect = lpDrawItemStruct->rcItem; //获取按钮尺寸
GetWindowText( m_strText ); //获取按钮文本
CPoint m_ptCentre = m_ButRect.CenterPoint(); //求按钮中心点
CSize Extent = pDC->GetTextExtent( m_strText ); //求文本尺寸
m_textPt = CPoint( m_ptCentre.x - Extent.cx/2,
m_ptCentre.y - Extent.cy/2 ); //设置文本坐标
int nSavedDC = pDC->SaveDC();
VERIFY( pDC );
if( !(::GetWindowLong(m_hWnd,GWL_STYLE) & WS_DISABLED) )
{
if( !b_Flag )
{
NormalButton( pDC ); //画正常按钮
}
else
{
PassButton( pDC ); //画鼠标经过时的按钮
}
}
else
{
LockButton( pDC ); //画锁定的按钮
}
pDC->RestoreDC( nSavedDC );
}
void CMyButton::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
CButton::PreSubclassWindow();
ModifyStyle( 0, BS_OWNERDRAW ); //设置按钮属性为自画式
}
void CMyButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
b_Flag = false;
if (GetFocus()!=this)
{
this->SetFocus();
}
CButton::OnLButtonDown( nFlags, point );
Invalidate(); //重绘按钮
}
void CMyButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
b_Flag = true;
if (GetFocus()!=this)
{
this->SetFocus();
}
CButton::OnLButtonUp( nFlags, point );
Invalidate(); //重绘按钮
}
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CButton::OnMouseMove(nFlags, point);
if( !b_InRect || GetCapture()!=this ) //鼠标进入按钮
{
b_InRect = true;
SetCapture();
b_Flag = true;
Invalidate(); //重绘按钮
}
else
{
CRect rc;
this->GetClientRect( &rc );
if ( !rc.PtInRect(point) ) //鼠标离开按钮
{
b_InRect = false;
ReleaseCapture();
b_Flag = false;
Invalidate(); //重绘按钮
}
}
}