www.pudn.com > 模拟LIFT.rar > liftDlg.cpp


// liftDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "lift.h" 
#include "liftDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CLiftDlg dialog 
 
CLiftDlg::CLiftDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CLiftDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CLiftDlg) 
	m_PresentFloor = 0; 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CLiftDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CLiftDlg) 
	DDX_Text(pDX, IDC_EDIT1, m_PresentFloor); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CLiftDlg, CDialog) 
	//{{AFX_MSG_MAP(CLiftDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1) 
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2) 
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3) 
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4) 
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5) 
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6) 
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7) 
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8) 
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9) 
	ON_BN_CLICKED(IDC_BUTTON10, OnButton10) 
	ON_BN_CLICKED(IDC_BUTTON11, OnButton11) 
	ON_BN_CLICKED(IDC_BUTTON12, OnButton12) 
	ON_BN_CLICKED(IDC_BUTTON13, OnButton13) 
	ON_BN_CLICKED(IDC_BUTTON14, OnButton14) 
	ON_BN_CLICKED(IDC_BUTTON15, OnButton15) 
	ON_BN_CLICKED(IDC_BUTTON16, OnButton16) 
	ON_BN_CLICKED(IDC_BUTTON17, OnButton17) 
	ON_BN_CLICKED(IDC_BUTTON18, OnButton18) 
	ON_BN_CLICKED(IDC_BUTTON19, OnButton19) 
	ON_BN_CLICKED(IDC_BUTTON20, OnButton20) 
	ON_BN_CLICKED(IDC_BUTTON21, OnButton21) 
	ON_BN_CLICKED(IDC_BUTTON22, OnButton22) 
	ON_BN_CLICKED(IDC_BUTTON23, OnButton23) 
	ON_BN_CLICKED(IDC_BUTTON24, OnButton24) 
	ON_WM_TIMER() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CLiftDlg message handlers 
 
BOOL CLiftDlg::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 
	SetTimer(10001,6000,NULL); 
	m_PresentFloor=1; 
	UpdateData(false); 
	iUpOrDownOrStop=0;//电梯初始为停的状态 
	SetDlgItemText(IDC_BUTTON25,"Stop");//设置面板上的状态按钮显示为停 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CLiftDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		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 CLiftDlg::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 CLiftDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
int CLiftDlg::CheckState() 
{ 
	for(int i=0;i<8;i++) 
	{ 
		if(lift.state[i].up==true||lift.state[i].down==true) 
		{ 
			return 1; 
		} 
		if(lift.stop[i]!=0) 
		{ 
			return 1; 
		} 
 	} 
	return 0; 
} 
 
 
void CLiftDlg::OnButton1()  
{ 
	lift.mkState(1,true); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton2()  
{ 
	lift.mkState(1,false); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton3()  
{ 
	lift.mkState(2,true); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton4()  
{ 
	lift.mkState(2,false); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton5()  
{ 
	lift.mkState(3,true); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton6()  
{ 
	lift.mkState(3,false); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton7()  
{ 
	lift.mkState(4,true); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton8()  
{ 
	lift.mkState(4,false); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton9()  
{ 
	lift.mkState(5,true); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton10()  
{ 
	lift.mkState(5,false); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton11()  
{ 
	lift.mkState(6,true); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton12() 
{ 
	lift.mkState(6,false); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton13()  
{ 
	lift.mkState(7,true); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton14()  
{ 
	lift.mkState(7,false); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton15()  
{ 
	lift.mkState(8,true); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton16()  
{ 
	lift.mkState(8,false); 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnButton17()  //want to the 1st floor 
{ 
	lift.stop[0]=2; 
	UpdateDlgView(); 
} 
void CLiftDlg::OnButton18()  
{ 
	if(lift.PresentFloor>2)   //want to 2nd when above it 
		lift.stop[1]=2; 
	else 
		lift.stop[1]=1; 
	UpdateDlgView(); 
		 
} 
 
void CLiftDlg::OnButton19()  
{ 
	if(lift.PresentFloor>3) 
		lift.stop[2]=2; 
	else 
		lift.stop[2]=1; 
	UpdateDlgView(); 
 
} 
 
void CLiftDlg::OnButton20()  
{ 
 
	if(lift.PresentFloor>4) 
		lift.stop[3]=2; 
	else 
		lift.stop[3]=1; 
	UpdateDlgView(); 
 
} 
 
void CLiftDlg::OnButton21()  
{ 
	if(lift.PresentFloor>5) 
		lift.stop[4]=2; 
	else 
		lift.stop[4]=1; 
	UpdateDlgView(); 
 
} 
 
void CLiftDlg::OnButton22()  
{ 
	if(lift.PresentFloor>6) 
		lift.stop[5]=2; 
	else 
		lift.stop[5]=1; 
	UpdateDlgView(); 
 
} 
 
void CLiftDlg::OnButton23()  
{ 
 
	if(lift.PresentFloor>7) 
		lift.stop[6]=2; 
	else 
		lift.stop[6]=1; 
	UpdateDlgView(); 
 
} 
 
 
void CLiftDlg::OnButton24()  
{ 
	lift.stop[7]=1;         //the highest floor should always be upwards 
	UpdateDlgView(); 
} 
 
void CLiftDlg::OnTimer(UINT nIDEvent)  
{ 
    UpDown();	 
	CDialog::OnTimer(nIDEvent); 
} 
 
 
/////////////////////////////////////////////////////////////////////////// 
//UpDown:the main arithmetic to judge up or down or stop 
 
 
void CLiftDlg::UpDown() 
 { 
	 if(iUpOrDownOrStop==1)  //when upwards 
	 { 
		for(int i=lift.PresentFloor-1;i<8;i++) 
		{ 
			if(lift.state[i].up==true) 
			{ 
				UpdateDlgView(); 
				To(i+1,true);//go upstairs 
				return;      //exit 
				 
			} 
			if(lift.stop[i]==1) 
			{ 
				UpdateDlgView(); 
				UpTo(i+1,2); 
				return; 
			} 
 		} 
 
        for(int m=7;m>=lift.PresentFloor;m--) 
		{ 
			if(lift.state[m].down==true) 
			{ 
				UpdateDlgView(); 
				To(m+1,false); 
				return; 
			} 
		} 
 
		for(int j=lift.PresentFloor-1;j>=0;j--) 
		{ 
			if(lift.state[j].down==true) 
			{ 
				UpdateDlgView(); 
				To(j+1,false); 
				return; 
			} 
			if(lift.stop[j]==2) 
			{ 
				UpdateDlgView(); 
				DownTo(j+1,2); 
				return; 
			} 
		} 
		 
		for(int n=0;n<=lift.PresentFloor;n++) 
		{ 
			if(lift.state[n].up==true) 
			{ 
				UpdateDlgView(); 
				To(n+1,true); 
				return; 
			} 
		} 
	 } 
	 else if(iUpOrDownOrStop==2)//when downwards 
	 { 
		for(int j=lift.PresentFloor-1;j>=0;j--) 
		{ 
			if(lift.state[j].down==true) 
			{ 
				UpdateDlgView(); 
				To(j+1,false); 
				return; 
			} 
			if(lift.stop[j]==2) 
			{ 
				UpdateDlgView(); 
				DownTo(j+1,2); 
				return; 
			} 
		} 
		for(int n=0;n<=lift.PresentFloor;n++) 
		{ 
			if(lift.state[n].up==true) 
			{ 
				UpdateDlgView(); 
				To(n+1,true); 
				return; 
			} 
		}  
		for(int i=lift.PresentFloor-1;i<8;i++) 
		{ 
			if(lift.state[i].up==true) 
			{ 
				UpdateDlgView(); 
				To(i+1,true); 
				return; 
			} 
			if(lift.stop[i]==1) 
			{ 
				UpdateDlgView(); 
				UpTo(i+1,2); 
				return; 
			} 
 		} 
		 
		for(int m=7;m>=lift.PresentFloor;m--) 
		{ 
			if(lift.state[m].down==true) 
			{ 
				UpdateDlgView(); 
				To(m+1,false); 
				return; 
			} 
		} 
	 } 
     else if(iUpOrDownOrStop==0)//when no action 
	 { 
		for(int i=lift.PresentFloor-1;i<8;i++) 
		{ 
			if(lift.state[i].up==true) 
			{ 
				UpdateDlgView(); 
				To(i+1,true); 
				return; 
			} 
			if(lift.stop[i]==1) 
			{ 
				UpdateDlgView(); 
				UpTo(i+1,2); 
					return; 
			} 
 		} 
		for(int j=lift.PresentFloor-1;j>=0;j--) 
		{ 
			if(lift.state[j].down==true) 
			{ 
				UpdateDlgView(); 
				To(j+1,false); 
				return; 
			} 
			if(lift.stop[j]==2) 
			{ 
				UpdateDlgView(); 
				DownTo(j+1,2); 
				return; 
			} 
		} 
		for(int m=7;m>=lift.PresentFloor;m--) 
		{ 
			if(lift.state[m].down==true) 
			{ 
				UpdateDlgView(); 
				To(m+1,false); 
				return; 
			} 
		} 
		for(int n=0;n<=lift.PresentFloor;n++) 
		{ 
			if(lift.state[n].up==true) 
			{ 
				UpdateDlgView(); 
				To(n+1,true); 
				return; 
			} 
		} 
	 } 
 
 } 
 
////////////////////////////////////////////////////////////////////////////////// 
//To: go to the floor   
 
 void CLiftDlg::To(int Floor,bool Up) 
{ 
	int i; 
	if(Up==false) 
		i=0; 
	else 
		i=1; 
	if(lift.PresentFloor<=Floor) 
		UpTo(Floor,i); 
	else 
		DownTo(Floor,i); 
} 
 
////////////////////////////////////////////////////////////////////////////////// 
// UpTo: upwards 
void CLiftDlg::UpTo(int Floor,int state) 
{ 
	if(lift.PresentFloorFloor) 
	{ 
		iUpOrDownOrStop=2; 
		lift.PresentFloor=lift.PresentFloor-1; 
 
	} 
	if(lift.PresentFloor==Floor) 
	{ 
		switch(state) 
		{ 
		case 0: 
			lift.state[Floor-1].down=false; 
			break; 
		case 1: 
			lift.state[Floor-1].up=false; 
			break; 
		case 2: 
			{			 
			lift.stop[Floor-1]=0; 
			if(CheckState()==0) 
				iUpOrDownOrStop=0; 
			break; 
			} 
		} 
	} 
	UpdateDlgView(); 
//	KillTimer(10001); 
//	SetTimer(10001,6000,NULL); 
} 
 
void CLiftDlg::UpdateDlgView() 
{ 
	for(int i=0;i<8;i++) 
	{ 
		if(lift.state[i].up==true) 
		{ 
			int ID=1000+2*i+1; 
			CButton* ptBtn=(CButton*)GetDlgItem(ID); 
			ptBtn->SetState(true); 
		} 
		else 
		{ 
			int ID=1000+2*i+1; 
			CButton* ptBtn=(CButton*)GetDlgItem(ID); 
			ptBtn->SetState(false); 
		} 
		if(lift.state[i].down==true) 
		{ 
			int ID=1000+2*(i+1); 
			CButton* ptBtn=(CButton*)GetDlgItem(ID); 
			ptBtn->SetState(true); 
		} 
		else 
		{ 
			int ID=1000+2*(i+1); 
			CButton* ptBtn=(CButton*)GetDlgItem(ID); 
			ptBtn->SetState(false); 
		} 
		if(lift.stop[i]!=0) 
		{ 
			int ID=1018+i; 
			CButton* ptBtn=(CButton*)GetDlgItem(ID); 
			ptBtn->SetState(true); 
		} 
		else 
		{ 
			int ID=1018+i; 
			CButton* ptBtn=(CButton*)GetDlgItem(ID); 
			ptBtn->SetState(false); 
		} 
 
	} 
	m_PresentFloor=lift.PresentFloor; 
	UpdateData(false); 
 
	switch(iUpOrDownOrStop)//show the state 
	{ 
	case 0: 
		SetDlgItemText(IDC_BUTTON25,"Stop"); 
		break; 
	case 1: 
		SetDlgItemText(IDC_BUTTON25,"Up"); 
		break; 
	case 2: 
		SetDlgItemText(IDC_BUTTON25,"Down"); 
		break; 
	} 
}