📄 eventsdlg.cpp
字号:
// EventsDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Events.h"
#include "EventsDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEventsDlg dialog
void Randomize ();
int Random (int num);
static int g_nWaitTime = 1500;
CEventsDlg::CEventsDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEventsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEventsDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEventsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEventsDlg)
//}}AFX_DATA_MAP
DDX_Control(pDX, IDC_MESSAGE_PROCESSOR, m_strProcessor);
DDX_Control(pDX, IDC_MESSAGE_RECEIVER, m_strReceiver);
}
BEGIN_MESSAGE_MAP(CEventsDlg, CDialog)
//{{AFX_MSG_MAP(CEventsDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEventsDlg message handlers
BOOL CEventsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
Randomize ();
m_bmpKey.LoadBitmap (IDB_MUTEXKEY);
m_Images.Create (IDB_MUTEXKEY, 32, 2, ILC_MASK);
m_strReceiver.GetWindowRect (m_rcReceiver);
m_strProcessor.GetWindowRect (m_rcProcessor);
ScreenToClient (m_rcReceiver);
ScreenToClient (m_rcProcessor);
m_rcReceiver.right = m_rcReceiver.left - 10;
m_rcReceiver.left = m_rcReceiver.right - 32;
m_rcProcessor.left = m_rcReceiver.left;
m_rcProcessor.right = m_rcReceiver.right;
//
// For a dialog box, ScreenToClient includes the title
// bar, so adjust for it.
//
m_rcProcessor.top += 16;
m_rcProcessor.bottom += 16;
m_rcReceiver.top += 16;
m_rcReceiver.bottom += 16;
m_nQueueCount = 0;
::SetWindowText (GetDlgItem(IDC_STATIC_QUEUECOUNT)->m_hWnd, "0");
AfxBeginThread (StartReceiver, this);
AfxBeginThread (StartProcessor, this);
return TRUE;
}
// 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 CEventsDlg::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 CEventsDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEventsDlg::AddItemToQueue(QUEUEITEM *pItem)
{
m_strReceiver.SetWindowText ("Waiting for queue");
CSingleLock QueueLock(&m_cs);
QueueLock.Lock ();
DrawKey (m_rcReceiver, true);
m_strReceiver.SetWindowText ("Queue is locked");
Sleep (Random(g_nWaitTime));
DrawKey (m_rcReceiver, false);
m_strReceiver.SetWindowText ("Queue is unlocked");
++m_nQueueCount;
CString strText;
strText.Format ("%d", m_nQueueCount);
::SetWindowText (GetDlgItem(IDC_STATIC_QUEUECOUNT)->m_hWnd, (LPCSTR) strText);
// m_cs.Unlock ();
}
int CEventsDlg::DeleteItemFromQueue(QUEUEITEM *pItem)
{
if (!m_nQueueCount)
return (0);
m_strProcessor.SetWindowText ("Waiting for queue");
CSingleLock QueueLock(&m_cs);
QueueLock.Lock ();
DrawKey (m_rcProcessor, true);
m_strProcessor.SetWindowText ("Queue is locked");
Sleep (Random(g_nWaitTime));
DrawKey (m_rcProcessor, false);
m_strProcessor.SetWindowText ("Queue is unlocked");
--m_nQueueCount;
CString strText;
strText.Format ("%d", m_nQueueCount);
::SetWindowText (GetDlgItem(IDC_STATIC_QUEUECOUNT)->m_hWnd, (LPCSTR) strText);
int nCount = m_nQueueCount;
// m_cs.Unlock ();
return (nCount);
}
UINT CEventsDlg::StartReceiver(void *pParam)
{
CEventsDlg *pOwner = (CEventsDlg *) pParam;
QUEUEITEM qi;
while (1)
{
if (g_nWaitTime > 100)
g_nWaitTime -= 100;
pOwner->m_strReceiver.SetWindowText ("Receiving item");
int nWaitTime, nCount;
nWaitTime = Random (7000)+ 3000;
;
Sleep (nWaitTime);
nCount = Random (7) + 1;
while (nCount--)
{
pOwner->AddItemToQueue (&qi);
pOwner->m_CheckQueue.SetEvent ();
}
}
return (0);
}
UINT CEventsDlg::StartProcessor(void *pParam)
{
CEventsDlg *pOwner = (CEventsDlg *) pParam;
QUEUEITEM qi;
while (1)
{
pOwner->m_strProcessor.SetWindowText ("Waiting");
DWORD dwEvent = WaitForSingleObject (pOwner->m_CheckQueue, INFINITE);
switch (dwEvent)
{
case WAIT_OBJECT_0:
pOwner->m_strProcessor.SetWindowText ("Processing item");
while (pOwner->DeleteItemFromQueue (&qi))
{
pOwner->m_strProcessor.SetWindowText ("Processing");
Sleep (1000);
}
continue;
case WAIT_TIMEOUT:
continue;
default:
return (-1);
}
}
return (0);
}
void CEventsDlg::DrawKey(CRect &rc, bool bDraw)
{
CWindowDC dc(this);
if (bDraw == false)
m_Images.Draw (&dc, 1, CPoint (rc.left, rc.top), ILC_MASK);
else
m_Images.Draw (&dc, 0, CPoint (rc.left, rc.top), ILC_MASK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -