⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elevatordlg.cpp

📁 这是一个用c++编写的可以处理4台10层电梯的电梯调度系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ElevatorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Elevator.h"
#include "ElevatorDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/*####################################################################
  模块名称:	电梯箱体
  开发作者:	漫步阳光
  开发时间:	@2005.11
####################################################################*/

/////////////////////////////////////////////////////////////////////////////
// CElevatorDlg dialog

CElevatorDlg::CElevatorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CElevatorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CElevatorDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	 
	m_iMaxPassengerNum = 15;
	m_iCurPassengerNum = 0;
	m_iCurFloor = 1;
	m_iNextDestFloor = -1;
	m_eCurState = IDLE;

	m_cUpWaitQueue =new CUpWaitQueue();
	m_cDownWaitQueue =new CDownWaitQueue();

}

CElevatorDlg::CElevatorDlg(int iElevatorNo, int iMaxPassengerNum/*=15*/, int iIniFloor/*=1*/, CWnd* pParent/*=NULL*/)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_iElevatorNo = iElevatorNo;
	m_iMaxPassengerNum = iMaxPassengerNum;
	m_iCurPassengerNum = 0;
	m_iCurFloor = iIniFloor;//电梯开始时所在楼层
	m_iNextDestFloor = -1;
	m_eCurState = IDLE;	
	m_eBackupDirection = BAD_DIRE;
	m_bCanSchedule = TRUE;

	m_pParent=pParent;

	m_cUpWaitQueue =new CUpWaitQueue(FLOOR_NUM);
	m_cDownWaitQueue =new CDownWaitQueue(FLOOR_NUM);

	stateDespArr[0] = "空闲";
	stateDespArr[1] = "下行";
	stateDespArr[2] = "上行";
	stateDespArr[3] = "下行暂停";
	stateDespArr[4] = "上行暂停";
	stateDespArr[5] = "开门";
	stateDespArr[6] = "故障!";	
	stateDespArr[7] = "";

	str_arrElevatorName[0]='A';
	str_arrElevatorName[1]='B';
	str_arrElevatorName[2]='C';
	str_arrElevatorName[3]='D';

	CString tempStr;
	tempStr.Format("%d",iElevatorNo);
	tempStr = "logs\\log"+tempStr+".txt";

try{
	m_cLogFile.Open( tempStr, CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite\
		| CFile::typeText , &m_Except);//打开日志文件
	m_cLogFile.SeekToEnd();
	Write2Log("空闲");

}catch(CException* e){}


}

void CElevatorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CElevatorDlg)
	DDX_Control(pDX, IDC_STATIC_PSG_NUM, m_conPsgNum);
	DDX_Control(pDX, IDC_STATIC_STATE, m_conState);
	DDX_Control(pDX, IDC_STATIC_ELEVATOR1, m_ElevatorBox);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CElevatorDlg, CDialog)
	//{{AFX_MSG_MAP(CElevatorDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BTN_OPEN_DOOR, openDoor)
	ON_BN_CLICKED(IDC_BTN_CLOSE_DOOR, closeDoor)
	ON_BN_CLICKED(IDC_BTN_CREATE_FAULT, OnBtnCreateFault)
	ON_BN_CLICKED(IDC_BTN_CORRECT_FAULT, OnBtnCorrectFault)
	ON_BN_CLICKED(IDC_BTN_PSG_ENTER, OnBtnPsgEnter)
	ON_BN_CLICKED(IDC_BTN_PSG_LEAVE, OnBtnPsgLeave)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
	ON_COMMAND_RANGE(IDC_MYBUTTONS,IDC_MYBUTTONS+10,OnInnerBtnClk) 
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CElevatorDlg message handlers



/*####################################################################
// name:		OnInitDialog

// input1:		
				
// ouput:		BOOL
				
// 功能描述:	电梯箱体初始化
// 开发作者:	漫步阳光
// 开发时间:	@2005.11
// 版本说明:	v1.0
####################################################################*/
BOOL CElevatorDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here		

	CString TempString;
	
	//===========创建动态按钮
	CString strCaption;
	int iPanelLeft = 10;
	int iPanelTop = 40;
	int iRowHeight = 35;
	int iColWidth = 36;

	int iBtnWidth = 35;
	int iBtnHeight = 30;
	int i;

	try{

	for(i=FLOOR_NUM;i>0;i--)
	{
		strCaption.Format("%d",i);
		m_pButton[i-1]=new CButton;
		
		if(i%2==0)
		{
		m_pButton[i-1]->Create(strCaption,BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE,\
			CRect(\
			iPanelLeft,\
			iPanelTop+(10-i)*iRowHeight/2,\
			iPanelLeft+iBtnWidth,\
			iPanelTop+(10-i)*iRowHeight/2+iBtnHeight),\
			this,IDC_MYBUTTONS+i-1);//left,top,right,bottom
	
		}
		else
		{
		m_pButton[i-1]->Create(strCaption,BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE,\
			CRect(\
			iPanelLeft+iColWidth,\
			iPanelTop+(10-i-1)*iRowHeight/2,\
			iPanelLeft+iColWidth+iBtnWidth,\
			iPanelTop+(10-i-1)*iRowHeight/2+iBtnHeight),\
			this,IDC_MYBUTTONS+i-1);//left,top,right,bottom
		}
		
	}//for

	}catch(CException* e){
	AfxMessageBox("无法创建电梯按钮,程序不能正常运行。");
	return FALSE;//退出!
	}

	GetDlgItem(IDC_BTN_OPEN_DOOR)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_CLOSE_DOOR)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_PSG_ENTER)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_PSG_LEAVE)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_CORRECT_FAULT)->EnableWindow(FALSE);


	//===========创建楼层号
	iPanelLeft = 160;
	int iLabelWidth = 20;
	int iLabelHeight = ELEVATOR_HEIGHT;
	iPanelTop = 36;

	try{

	for(i=FLOOR_NUM;i>0;i--)
	{
		TempString.Format("%d",i);
		m_pLabel[i-1] = new CStatic;
		m_pLabel[i-1]->Create(TempString+"#",WS_CHILD|WS_VISIBLE|SS_CENTER,\
			CRect(\
			iPanelLeft,\
			iPanelTop+(10-i)*iLabelHeight,\
			iPanelLeft+iLabelWidth,\
			iPanelTop+(11-i)*iLabelHeight),\
			this,IDC_MYLABELS+i-1);
		
		
	}//for

	}catch(CException* e){
	AfxMessageBox("无法创建楼层标号,程序不能正常运行。");
	return FALSE;//退出!
	}

	CFont cFont;
	cFont.CreateFont(12, 10, 0, 0, FW_BOLD, FALSE, TRUE,0,0,0,0,0,0, "宋体");
	//m_hbrush=CreateSolidBrush(RGB(0,0,255));
	
	TempString.Format("%c",str_arrElevatorName[m_iElevatorNo-1]);
	((CButton* )GetDlgItem(IDC_STATIC_ELEVATOR))->SetFont(&cFont);	
	((CButton* )GetDlgItem(IDC_STATIC_ELEVATOR))->SetWindowText("电梯"+TempString);
	cFont.DeleteObject();

	m_ElevatorBox.SetWindowText("     ");
	m_ElevatorBox.SetBkColor(RGB(0,0,255));

	//===========电梯箱体
	m_ElevatorBox.MoveWindow(iPanelLeft-70,iPanelTop+(10-m_iCurFloor)*iLabelHeight,\
		70,20);	


	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CElevatorDlg::OnSysCommand(UINT nID, LPARAM lParam)
{

		CDialog::OnSysCommand(nID, lParam);

}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CElevatorDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CElevatorDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



/*####################################################################
// name:		OnTimer

// input1:		UINT nIDEvent
				
// ouput:		void
				
// 功能描述:	基于定时器的电梯时序控制函数
// 开发作者:	漫步阳光
// 开发时间:	@2005.11
// 版本说明:	v1.0
####################################################################*/
void CElevatorDlg::OnTimer(UINT nIDEvent) 
{
	//static int CurFloor=1;
	// TODO: Add your message handler code here and/or call default

	/*
	if(m_iNextDestFloor<0) //如果下一个目的楼层无效,则停止
	{
	KillTimer(1);
	return;
	}
	*/

	switch(nIDEvent)
	{
	case 1://电梯运行控制时钟
		if(m_eCurState==UP_RUN)
		{
			if(m_iCurFloor < m_iNextDestFloor)
			{
				m_iCurFloor++;
				m_ElevatorBox.MoveWindow(90,36+(10-m_iCurFloor)*ELEVATOR_HEIGHT,\
		70,20);						
			}
			else //到达目的楼层
			{
				KillTimer(1);
				buzz();
				openDoor();

			}
		}else if(m_eCurState==DOWN_RUN)
		{
			if(m_iCurFloor > m_iNextDestFloor)
			{
				m_iCurFloor--;
				m_ElevatorBox.MoveWindow(90,36+(10-m_iCurFloor)*ELEVATOR_HEIGHT,\
		70,20);						
			}
			else //到达目的楼层
			{
				KillTimer(1);
				buzz();
				openDoor();
			}
		}
		break;

	case 2://电梯开门超时控制时钟
		KillTimer(2);
		closeDoor();
		break;

	}

	CDialog::OnTimer(nIDEvent);
}



/*####################################################################
// name:		openDoor

// input1:		
				
// ouput:		void
				
// 功能描述:	电梯到达目的楼层后开门
// 开发作者:	漫步阳光
// 开发时间:	@2005.11
// 版本说明:	v1.0
####################################################################*/
void CElevatorDlg::openDoor() {

	//如果电梯之前接受的是逆向请求,现在到达了目的楼层,则转向并解禁
	if(m_eBackupDirection!=BAD_DIRE)
	{
		if(m_eBackupDirection==UP)
		{
			m_eCurState = UP_PAUSE;
			m_cDownWaitQueue->removeElement(m_iCurFloor);
		}else{
			m_eCurState = DOWN_PAUSE;
			m_cUpWaitQueue->removeElement(m_iCurFloor);
		}
		
		m_bCanSchedule = TRUE;
		m_eBackupDirection = BAD_DIRE;

	}else{

	//从等待队列中移除当前楼层请求
	if(m_eCurState == UP_RUN){
		m_cUpWaitQueue->removeElement(m_iCurFloor);
		m_eCurState = UP_PAUSE;
	}
	else if(m_eCurState == DOWN_RUN){
		m_cDownWaitQueue->removeElement(m_iCurFloor);
		m_eCurState = DOWN_PAUSE;
	}
	}
	

	CString tempStr,tempStr1;
	tempStr.Format("%d",m_iCurFloor);
	tempStr = "到达"+tempStr+"楼";
	m_conState.SetWindowText(tempStr);
	tempStr1.Format("%d",m_iCurPassengerNum);
	Write2Log(tempStr+"("+tempStr1+"人)");//记录日志
	tempStr="";

	//m_conState.SetWindowText("门开");	
	
	GetDlgItem(IDC_BTN_OPEN_DOOR)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_CLOSE_DOOR)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_PSG_ENTER)->EnableWindow(TRUE);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -