电梯窗体ElevatorDlgh头文件电梯窗体定义.docx
《电梯窗体ElevatorDlgh头文件电梯窗体定义.docx》由会员分享,可在线阅读,更多相关《电梯窗体ElevatorDlgh头文件电梯窗体定义.docx(30页珍藏版)》请在冰点文库上搜索。
电梯窗体ElevatorDlgh头文件电梯窗体定义
//电梯窗体ElevatorDlg.h头文件——————电梯窗体定义
//ElevatorDlg.h:
headerfile
#if!
defined(AFX_ELEVATORDLG_H__02CD205C_1566_449C_B58D_1BF712DC64AA__INCLUDED_)
#defineAFX_ELEVATORDLG_H__02CD205C_1566_449C_B58D_1BF712DC64AA__INCLUDED_
#if_MSC_VER>1000
#pragmaonce
#endif
#include"resource.h"//mainsymbols
#include"CommonInc.h"
#include"WaitQueue.h"
#include"ColorStaticST.h"
//发往父窗体的消息
#defineWM_ARRIVE_ONEFLOORWM_USER+0X80
//电梯窗体
classCElevatorDlg:
publicCDialog
{
//构造
public:
CElevatorDlg(CWnd*pParent=NULL);//standardconstructor
CElevatorDlg(intiElevatorNo,intiMaxPassengerNum=18,intiIniFloor=1,CWnd*pParent=NULL);
voidacceptReq(sOuterRequest&tmpOuterRequest);
boolisOverWeight(){returnm_iCurPassengerNum>m_iMaxPassengerNum;}
boolisMalFunction(){returnm_eCurState==MAL_FUNCTION;}
intgetElevatorNo(){returnm_iElevatorNo;}
intgetCurPassengerNum(){returnm_iCurPassengerNum;}
intgetCurFloor(){returnm_iCurFloor;}
intgetCurWeight(){returnm_iCurPassengerNum;}
enumstategetState(){returnm_eCurState;}
boolcanSchedule(){returnm_bCanSchedule;}
voidsetState(enumstateeCurState){m_eCurState=eCurState;}
//DialogData
enum{IDD=IDD_ELEVATOR_DIALOG};
CStaticm_conPsgNum;
CStaticm_conState;
CColorStaticSTm_ElevatorBox;
//ClassWizardgeneratedvirtualfunctionoverrides
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport
//Implementation
protected:
HICONm_hIcon;
CWnd*m_pParent;
CStdioFilem_cLogFile;//日志文件
CFileExceptionm_Except;
protected:
voidgotoNextDest();
//Generatedmessagemapfunctions
virtualBOOLOnInitDialog();
afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);
afx_msgvoidOnPaint();
afx_msgHCURSOROnQueryDragIcon();
afx_msgvoidOnTimer(UINTnIDEvent);
afx_msgvoidOnDestroy();
afx_msgvoidopenDoor();
afx_msgvoidcloseDoor();
afx_msgvoidOnBtnCreateFault();
afx_msgvoidOnBtnCorrectFault();
afx_msgvoidOnBtnPsgEnter();
afx_msgvoidOnBtnPsgLeave();
afx_msgHBRUSHOnCtlColor(CDC*pDC,CWnd*pWnd,UINTnCtlColor);
afx_msgvoidOnInnerBtnClk(UINTnID);
DECLARE_MESSAGE_MAP()
private:
intm_iElevatorNo;
intm_iMaxPassengerNum;
intm_iCurPassengerNum;
intm_iCurFloor;
intm_iNextDestFloor;
enumstatem_eCurState;
enumstatem_eOldState;
enumdirectionm_eBackupDirection;
boolm_bCanSchedule;
charstr_arrElevatorName[ELEVATOR_NUM];
//窗体成员变量
CButton*m_pButton[FLOOR_NUM];
CStatic*m_pLabel[FLOOR_NUM];
CStringstateDespArr[8];
//等待队列
CUpWaitQueue*m_cUpWaitQueue;
CDownWaitQueue*m_cDownWaitQueue;
private:
//voidgotoDestFloor(intiFloorNo);
voidenableRelateBtn(enumstateeCurState,intiFloorNo);
voidWrite2Log(CStringcontent);
voidbuzz();
};
#endif//
//电梯窗体ElevatorDlg.cpp资源文件——————电梯窗体实现
//ElevatorDlg.cpp:
implementationfile
#include"stdafx.h"
#include"Elevator.h"
#include"ElevatorDlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
//创建电梯窗体
//电梯窗体
CElevatorDlg:
:
CElevatorDlg(CWnd*pParent)
:
CDialog(CElevatorDlg:
:
IDD,pParent)
{
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_iMaxPassengerNum=17;
m_iCurPassengerNum=0;
m_iCurFloor=1;
m_iNextDestFloor=-1;
m_eCurState=IDLE;
m_cUpWaitQueue=newCUpWaitQueue();
m_cDownWaitQueue=newCDownWaitQueue();
}
CElevatorDlg:
:
CElevatorDlg(intiElevatorNo,intiMaxPassengerNum,intiIniFloor,CWnd*pParent)
{
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=newCUpWaitQueue(FLOOR_NUM);
m_cDownWaitQueue=newCDownWaitQueue(FLOOR_NUM);
stateDespArr[0]="电梯空闲,可以载人";
stateDespArr[1]="电梯下行运行中,请选择其他电梯或等待";
stateDespArr[2]="电梯上行运行中,请选择其他电梯或等待";
stateDespArr[3]="电梯下行暂停中";
stateDespArr[4]="电梯上行暂停中";
stateDespArr[5]="电梯开门,请乘客上下";
stateDespArr[6]="电梯故障!
请选择其他电梯";
stateDespArr[7]="";
str_arrElevatorName[0]='1';
str_arrElevatorName[1]='2';
str_arrElevatorName[2]='3';
str_arrElevatorName[3]='4';
CStringtempStr;
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){}
}
voidCElevatorDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
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);
}
BEGIN_MESSAGE_MAP(CElevatorDlg,CDialog)
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()
ON_COMMAND_RANGE(IDC_MYBUTTONS,IDC_MYBUTTONS+10,OnInnerBtnClk)
END_MESSAGE_MAP()
//电梯窗体消息响应
//电梯窗体初始化
BOOLCElevatorDlg:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();
//Add"About..."menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);//Setbigicon
SetIcon(m_hIcon,FALSE);//Setsmallicon
//TODO:
Addextrainitializationhere
CStringTempString;
//创建电梯窗体中的1-10楼层按钮参数
CStringstrCaption;
intiPanelLeft=10;
intiPanelTop=52;
intiRowHeight=75;
intiColWidth=35;
intiBtnWidth=30;
intiBtnHeight=45;
inti;
try{
for(i=FLOOR_NUM;i>0;i--)
{
strCaption.Format("%d",i);
m_pButton[i-1]=newCButton;
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("创建电梯按钮失败,程序将退出!
");
returnFALSE;
}
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);
//创建电梯窗体中1-10楼层编号
iPanelLeft=150;
intiLabelWidth=43;
intiLabelHeight=40;
iPanelTop=40;
try{
for(i=FLOOR_NUM;i>0;i--)
{
TempString.Format("%d",i);
m_pLabel[i-1]=newCStatic;
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);
}
}catch(CException*e){
AfxMessageBox("创建楼层编号失败,程序将退出!
");
returnFALSE;
}
CFontcFont;
cFont.CreateFont(12,10,0,0,FW_BOLD,FALSE,TRUE,0,0,0,0,0,0,"宋体");
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(225,0,0));
//指示条参数
m_ElevatorBox.MoveWindow(iPanelLeft-63,iPanelTop+(10-m_iCurFloor)*iLabelHeight,\
70,20);
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
}
voidCElevatorDlg:
:
OnSysCommand(UINTnID,LPARAMlParam)
{
CDialog:
:
OnSysCommand(nID,lParam);
}
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
voidCElevatorDlg:
:
OnPaint()
{
if(IsIconic())
{
CPaintDCdc(this);//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
//Centericoninclientrectangle
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//Drawtheicon
dc.DrawIcon(x,y,m_hIcon);
}
else
{
CDialog:
:
OnPaint();
}
}
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
HCURSORCElevatorDlg:
:
OnQueryDragIcon()
{
return(HCURSOR)m_hIcon;
}
//电梯时序控制函数(基于定时器)
voidCElevatorDlg:
:
OnTimer(UINTnIDEvent)
{
//staticintCurFloor=1;
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
switch(nIDEvent)
{
case1:
//电梯运行控制时钟
if(m_eCurState==UP_RUN)