// CustomerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Customer.h"
#include "CustomerDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CCustomerDlg dialog
CCustomerDlg::CCustomerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCustomerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCustomerDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pHouse = NULL;
m_nProductors = 0;
m_nConsumers = 0;
InitializeCriticalSection(&m_csMessages_Lock);
}
CCustomerDlg::~CCustomerDlg()
{
DeleteCriticalSection(&m_csMessages_Lock);
}
void CCustomerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCustomerDlg)
DDX_Control(pDX, IDC_PROGRESS_STORE, m_StoreNums);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCustomerDlg, CDialog)
//{{AFX_MSG_MAP(CCustomerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
ON_BN_CLICKED(IDC_BUTTON_ADDP, OnButtonAddp)
ON_BN_CLICKED(IDC_BUTTON_ADDC, OnButtonAddc)
ON_WM_CLOSE()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCustomerDlg message handlers
BOOL CCustomerDlg::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);
}
}
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
GetDlgItem(IDC_BUTTON_START)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_ADDC)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_ADDP)->EnableWindow(FALSE);
SetTimer(MESSAGE_TIMER,TIMER_INVAL,NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CCustomerDlg::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 CCustomerDlg::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();
}
}
HCURSOR CCustomerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCustomerDlg::Refresh(void *pParam,StoreHouse *pHouse)
{
if (pParam == NULL
||pHouse == NULL)
{
return;
}
CCustomerDlg *pThis = (CCustomerDlg*)pParam;
char tempBuffer[BUFF_SIZE64];
_snprintf(tempBuffer,BUFF_SIZE64,"%d",pHouse->GetCurrentSize());
pThis->GetDlgItem(IDC_STATIC_CURRENT)->SetWindowText(tempBuffer);
pThis->m_StoreNums.SetPos(pHouse->GetCurrentSize());
}
void CCustomerDlg::ShowMessage(const char* pMessage)
{
CString szMessage;
szMessage.Format("%s%c%c",pMessage,13,10);
EnterCriticalSection(&m_csMessages_Lock);
m_Messages.Add(szMessage);
LeaveCriticalSection(&m_csMessages_Lock);
}
void CCustomerDlg::OnButtonStart()
{
// TODO: Add your control notification handler code here
if (m_pHouse != NULL)
{
return;
}
m_pHouse = new StoreHouse;
m_pHouse->SetRefresh(Refresh,this);
m_StoreNums.SetRange(0,m_pHouse->GetMaxSize());
char tempBuffer[BUFF_SIZE64];
_snprintf(tempBuffer,BUFF_SIZE64,"%d",m_pHouse->GetMaxSize());
GetDlgItem(IDC_STATIC_MAX)->SetWindowText(tempBuffer);
GetDlgItem(IDC_STATIC_CURRENT)->SetWindowText("0");
GetDlgItem(IDC_BUTTON_START)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON_ADDC)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON_ADDP)->EnableWindow(TRUE);
}
void CCustomerDlg::OnButtonStop()
{
// TODO: Add your control notification handler code here
if (m_pHouse == NULL)
{
return;
}
//停止仓库运作
if (m_pHouse != NULL)
{
m_pHouse->Stop();
}
//停止每个工作者
vector<Human*>::iterator itor;
Human* pTmpHuman = NULL;
for (itor = m_Workers.begin();itor != m_Workers.end();++itor)
{
pTmpHuman = (Human*)*itor;
pTmpHuman ->Stop();
delete pTmpHuman;
pTmpHuman = NULL;
}
m_Workers.clear();
m_nProductors = 0;
m_nConsumers = 0;
delete m_pHouse;
m_pHouse = NULL;
m_StoreNums.SetPos(0);
GetDlgItem(IDC_STATIC_PRODUCTOR)->SetWindowText("0");
GetDlgItem(IDC_STATIC_CONSUMER)->SetWindowText("0");
GetDlgItem(IDC_STATIC_CURRENT)->SetWindowText("0");
GetDlgItem(IDC_STATIC_MAX)->SetWindowText("0");
GetDlgItem(IDC_BUTTON_START)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_ADDC)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_ADDP)->EnableWindow(FALSE);
}
void CCustomerDlg::OnButtonAddp()
{
// TODO: Add your control notification handler code here
if (m_pHouse == NULL)
{
return;
}
Productor * pTmpProductor = new Productor();
if (pTmpProductor == NULL)
{
return;
}
pTmpProductor->SetHouse(m_pHouse);
pTmpProductor->SetDialog(this);
pTmpProductor->Start();
m_Workers.push_back((Human*)pTmpProductor);
m_nProductors++;
char tempBuffer[BUFF_SIZE64];
_snprintf(tempBuffer,BUFF_SIZE64,"%d",m_nProductors);
GetDlgItem(IDC_STATIC_PRODUCTOR)->SetWindowText(tempBuffer);
}
void CCustomerDlg::OnButtonAddc()
{
// TODO: Add your control notification handler code here
if (m_pHouse == NULL)
{
return;
}
Consumer * pTmpConsumer = new Consumer();
if (pTmpConsumer == NULL)
{
return;
}
pTmpConsumer->SetHouse(m_pHouse);
pTmpConsumer->SetDialog(this);
pTmpConsumer->Start();
m_Workers.push_back((Human*)pTmpConsumer);
m_nConsumers++;
char tempBuffer[BUFF_SIZE64];
_snprintf(tempBuffer,BUFF_SIZE64,"%d",m_nConsumers);
GetDlgItem(IDC_STATIC_CONSUMER)->SetWindowText(tempBuffer);
}
void CCustomerDlg::OnClose()
{
// TOD