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

📄 elevatordlg.cpp

📁 常考设计题
💻 CPP
字号:
// 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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// 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);
}

void CElevatorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CElevatorDlg)
	DDX_Control(pDX, IDC_UP_9, m_up_9);
	DDX_Control(pDX, IDC_UP_8, m_up_8);
	DDX_Control(pDX, IDC_UP_7, m_up_7);
	DDX_Control(pDX, IDC_UP_6, m_up_6);
	DDX_Control(pDX, IDC_UP_5, m_up_5);
	DDX_Control(pDX, IDC_UP_4, m_up_4);
	DDX_Control(pDX, IDC_UP_3, m_up_3);
	DDX_Control(pDX, IDC_UP_2, m_up_2);
	DDX_Control(pDX, IDC_UP_1, m_up_1);
	DDX_Control(pDX, IDC_DOWN_9, m_down_9);
	DDX_Control(pDX, IDC_DOWN_8, m_down_8);
	DDX_Control(pDX, IDC_DOWN_7, m_down_7);
	DDX_Control(pDX, IDC_DOWN_6, m_down_6);
	DDX_Control(pDX, IDC_DOWN_5, m_down_5);
	DDX_Control(pDX, IDC_DOWN_4, m_down_4);
	DDX_Control(pDX, IDC_DOWN_3, m_down_3);
	DDX_Control(pDX, IDC_DOWN_2, m_down_2);
	DDX_Control(pDX, IDC_GO, m_go);
	DDX_Control(pDX, IDC_DEST_9, m_dest_9);
	DDX_Control(pDX, IDC_DEST_8, m_dest_8);
	DDX_Control(pDX, IDC_DEST_7, m_dest_7);
	DDX_Control(pDX, IDC_DEST_6, m_dest_6);
	DDX_Control(pDX, IDC_DEST_5, m_dest_5);
	DDX_Control(pDX, IDC_DEST_4, m_dest_4);
	DDX_Control(pDX, IDC_DEST_3, m_dest_3);
	DDX_Control(pDX, IDC_DEST_2, m_dest_2);
	DDX_Control(pDX, IDC_DEST_1, m_dest_1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CElevatorDlg, CDialog)
	//{{AFX_MSG_MAP(CElevatorDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_UP_1, OnPressUp_1)
	ON_BN_CLICKED(IDC_UP_2, OnPressUp_2)
	ON_BN_CLICKED(IDC_UP_3, OnPressUp_3)
	ON_BN_CLICKED(IDC_UP_4, OnPressUp_4)
	ON_BN_CLICKED(IDC_UP_5, OnPressUp_5)
	ON_BN_CLICKED(IDC_UP_6, OnPressUp_6)
	ON_BN_CLICKED(IDC_UP_7, OnPressUp_7)
	ON_BN_CLICKED(IDC_UP_8, OnPressUp_8)
	ON_BN_CLICKED(IDC_DEST_1, OnDest_1)
	ON_BN_CLICKED(IDC_DEST_2, OnDest_2)
	ON_BN_CLICKED(IDC_DEST_3, OnDest_3)
	ON_BN_CLICKED(IDC_DEST_4, OnDest_4)
	ON_BN_CLICKED(IDC_DEST_5, OnDest_5)
	ON_BN_CLICKED(IDC_DEST_6, OnDest_6)
	ON_BN_CLICKED(IDC_DEST_7, OnDest_7)
	ON_BN_CLICKED(IDC_DEST_8, OnDest_8)
	ON_BN_CLICKED(IDC_DEST_9, OnDest_9)
	ON_BN_CLICKED(IDC_DOWN_2, OnPressDown_2)
	ON_BN_CLICKED(IDC_DOWN_3, OnPressDown_3)
	ON_BN_CLICKED(IDC_DOWN_4, OnPressDown_4)
	ON_BN_CLICKED(IDC_DOWN_5, OnPressDown_5)
	ON_BN_CLICKED(IDC_DOWN_6, OnPressDown_6)
	ON_BN_CLICKED(IDC_DOWN_7, OnPressDown_7)
	ON_BN_CLICKED(IDC_DOWN_8, OnPressDown_8)
	ON_BN_CLICKED(IDC_DOWN_9, OnPressDown_9)
	ON_BN_CLICKED(IDC_GO, OnGo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

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

	//初始化为停靠在第一层
	for (int i=0;i<9;i++)
	{
		m_bFloor[i]=false;
	}

	m_bFloor[0]=true;
	CStatic* pStatic=(CStatic*)this->GetDlgItem (IDC_FLOOR_1);
	pStatic->ModifyStyleEx (105,WS_EX_STATICEDGE,0);

	//将第一层向下的按钮和第九层向上的按钮屏蔽掉
	CButton* pButton1=(CButton*)this->GetDlgItem (IDC_DOWN_1);
	pButton1->EnableWindow (false);
	CButton* pButton2=(CButton*)this->GetDlgItem (IDC_UP_9);
	pButton2->EnableWindow (false);

	//初始时电梯没有运动
	m_up=false;
	m_down=false;

	//设置电梯的定时器,启动电梯控制逻辑
	m_elevator.SetMyTimer (50);

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

void CElevatorDlg::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 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;
}

//更改界面显示的消息映射函数
HBRUSH CElevatorDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	int i;
	for (i=0;i<9;i++)
	{
		if (m_bFloor[i])
			break;
	}

	if (pWnd->GetDlgCtrlID() == IDC_FLOOR_1+i) 
    { 
		// 如果电梯停靠在该层,则该层显示为红色;否则不予处理
        pDC->SetTextColor(RGB(0, 0, 255)); 
    } 

	if (m_up)
	{
		if (pWnd->GetDlgCtrlID ()== IDC_LIGHT_UP)
			pDC->SetTextColor (RGB(255,0,0));
	}

	if (m_down)
	{
		if (pWnd->GetDlgCtrlID ()== IDC_LIGHT_DOWN)
			pDC->SetTextColor (RGB(255,0,0));
	}

	// TODO: Return a different brush if the default is not desired
	return hbr;
}


void CElevatorDlg::OnPressUp_1() 
{
	// TODO: Add your control notification handler code here
	HighLight(UP_1);
	m_elevator.RequestUp(1);
}

void CElevatorDlg::OnPressUp_2() 
{
	// TODO: Add your control notification handler code here
	HighLight(UP_2);
	m_elevator.RequestUp(2);
}

void CElevatorDlg::OnPressUp_3() 
{
	// TODO: Add your control notification handler code here
	HighLight(UP_3);
	m_elevator.RequestUp(3);
}

void CElevatorDlg::OnPressUp_4() 
{
	// TODO: Add your control notification handler code here
	HighLight(UP_4);
	m_elevator.RequestUp(4);
}

void CElevatorDlg::OnPressUp_5() 
{
	// TODO: Add your control notification handler code here
	HighLight(UP_5);
	m_elevator.RequestUp(5);
}

void CElevatorDlg::OnPressUp_6() 
{
	// TODO: Add your control notification handler code here
	HighLight(UP_6);
	m_elevator.RequestUp(6);
}

void CElevatorDlg::OnPressUp_7() 
{
	// TODO: Add your control notification handler code here
	HighLight(UP_7);
	m_elevator.RequestUp(7);
}

void CElevatorDlg::OnPressUp_8() 
{
	// TODO: Add your control notification handler code here
	HighLight(UP_8);
	m_elevator.RequestUp(8);
}

void CElevatorDlg::OnDest_1() 
{
	// TODO: Add your control notification handler code here
	HighLight(DEST_1);
	m_elevator .RequestDest(1);
}

void CElevatorDlg::OnDest_2() 
{
	// TODO: Add your control notification handler code here
	HighLight(DEST_2);
	m_elevator .RequestDest(2);
}

void CElevatorDlg::OnDest_3() 
{
	// TODO: Add your control notification handler code here
	HighLight(DEST_3);
	m_elevator .RequestDest(3);
}

void CElevatorDlg::OnDest_4() 
{
	// TODO: Add your control notification handler code here
	HighLight(DEST_4);
	m_elevator .RequestDest(4);
}

void CElevatorDlg::OnDest_5() 
{
	// TODO: Add your control notification handler code here
	HighLight(DEST_5);
	m_elevator .RequestDest(5);
}

void CElevatorDlg::OnDest_6() 
{
	// TODO: Add your control notification handler code here
	HighLight(DEST_6);
	m_elevator .RequestDest(6);
}

void CElevatorDlg::OnDest_7() 
{
	// TODO: Add your control notification handler code here
	HighLight(DEST_7);
	m_elevator .RequestDest(7);
}

void CElevatorDlg::OnDest_8() 
{
	// TODO: Add your control notification handler code here
	HighLight(DEST_8);
	m_elevator .RequestDest(8);
}

void CElevatorDlg::OnDest_9() 
{
	// TODO: Add your control notification handler code here
	HighLight(DEST_9);
	m_elevator .RequestDest(9);
}

void CElevatorDlg::OnPressDown_2() 
{
	// TODO: Add your control notification handler code here
	HighLight(DOWN_2);
	m_elevator.RequestDown(2);
}

void CElevatorDlg::OnPressDown_3() 
{
	// TODO: Add your control notification handler code here
	HighLight(DOWN_3);
	m_elevator.RequestDown(3);
}

void CElevatorDlg::OnPressDown_4() 
{
	// TODO: Add your control notification handler code here
	HighLight(DOWN_4);
	m_elevator.RequestDown(4);
}

void CElevatorDlg::OnPressDown_5() 
{
	// TODO: Add your control notification handler code here
	HighLight(DOWN_5);
	m_elevator.RequestDown(5);
}

void CElevatorDlg::OnPressDown_6() 
{
	// TODO: Add your control notification handler code here
	HighLight(DOWN_6);
	m_elevator.RequestDown(6);
}

void CElevatorDlg::OnPressDown_7() 
{
	// TODO: Add your control notification handler code here
	HighLight(DOWN_7);
	m_elevator.RequestDown(7);
}

void CElevatorDlg::OnPressDown_8() 
{
	// TODO: Add your control notification handler code here
	HighLight(DOWN_8);
	m_elevator.RequestDown(8);
}

void CElevatorDlg::OnPressDown_9() 
{
	// TODO: Add your control notification handler code here
	HighLight(DOWN_9);
	m_elevator.RequestDown(9);
}

//用户按下“Go”按钮
void CElevatorDlg::OnGo() 
{
	// TODO: Add your control notification handler code here
	HighLight(GO);
	m_elevator.CloseDoor();
}

//用户对界面的操作通过改变界面元素的状态来实现
//这里的界面元素指的是位于系统主界面之上的所有静态文本、按钮等控件
//界面元素的状态有两种:正常显示和高亮显示
//HighLight()将界面元素由正常显示转为高亮显示
//StopHighLight()将界面元素由高亮显示恢复为正常显示
//参数i表示需要改变状态的界面元素的代码
//如果需要了解每个界面元素的详细代码,请查阅常量定义(const.h)
void CElevatorDlg::HighLight(int i)
{
	//楼层指示按钮
	if ((FLOOR_1-1<i)&&(i<FLOOR_9+1))
	{
		CStatic* pStatic=(CStatic*)this->GetDlgItem (i);
		ASSERT(pStatic);
		pStatic->ModifyStyleEx(105,WS_EX_STATICEDGE,0);
		m_bFloor[i-FLOOR_1]=true;

		this->Invalidate (false);
		return ;
	}

	//各个楼层的向上按钮
	if ((UP_1-1<i)&&(i<UP_8+1))
	{
		CSXButton* pButton=(CSXButton*)this->GetDlgItem (i);
		pButton->ChangeColor (RGB(255,0,0));
		return ;
	}

	//各个楼层的向下按钮
	if ((DOWN_2-1<i)&&(i<DOWN_9+1))
	{
		CSXButton* pButton=(CSXButton*)this->GetDlgItem (i);
		pButton->ChangeColor (RGB(255,0,0));
		return ;
	}

	//电梯内部的目标楼层按钮
	if ((DEST_1-1<i)&&(i<DEST_9+1))
	{
		CSXButton* pButton=(CSXButton*)this->GetDlgItem (i);
		pButton->ChangeColor (RGB(255,0,0));
		return ;
	}

	//Go按钮
	if (i==GO)
	{
		m_go.ChangeColor (RGB(255,0,0));
		return ;
	}

	//上下指示灯
	if (i==LIGHT_UP)
	{
		this->m_up =true;
		this->Invalidate (false);
		return ;
	}

	if (i==LIGHT_DOWN)
	{
		this->m_down =true;
		this->Invalidate (false);
		return ;
	}
}

void CElevatorDlg::StopHighLight(int i)
{
	//楼层指示按钮
	if ((FLOOR_1-1<i)&&(i<FLOOR_9+1))
	{
		CStatic* pStatic=(CStatic*)this->GetDlgItem (i);
		ASSERT(pStatic);
		pStatic->ModifyStyleEx(WS_EX_STATICEDGE,105,0);
		m_bFloor[i-FLOOR_1]=false;

		this->Invalidate (false);		
		return ;
	}

	//各个楼层的向上按钮
	if ((UP_1-1<i)&&(i<UP_8+1))
	{
		CSXButton* pButton=(CSXButton*)this->GetDlgItem (i);
		pButton->ChangeColor (RGB(0,0,0));
		return ;
	}

	//各个楼层的向下按钮
	if ((DOWN_2-1<i)&&(i<DOWN_9+1))
	{
		CSXButton* pButton=(CSXButton*)this->GetDlgItem (i);
		pButton->ChangeColor (RGB(0,0,0));
		return ;
	}

	//电梯内部的目标楼层按钮
	if ((DEST_1-1<i)&&(i<DEST_9+1))
	{
		CSXButton* pButton=(CSXButton*)this->GetDlgItem (i);
		pButton->ChangeColor (RGB(0,0,0));
		return ;
	}

	//Go按钮
	if (i==GO)
	{
		m_go.ChangeColor (RGB(0,0,0));
		return ;
	}

	//上下指示灯
	if (i==LIGHT_UP)
	{
		this->m_up =false;
		this->Invalidate (false);
		return ;
	}

	if (i==LIGHT_DOWN)
	{
		this->m_down =false;
		this->Invalidate (false);
		return ;
	}

}


void CElevatorDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

⌨️ 快捷键说明

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