www.pudn.com > EventExplorer.rar > EventExplorerDlg.cpp


// EventExplorerDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "EventExplorer.h" 
#include "EventExplorerDlg.h" 
#include "about.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
#define UWM_CLOSEHANDLE (WM_USER+1000) 
#define UWM_STEPIT      (WM_USER+1001) 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CEventExplorerDlg dialog 
 
CEventExplorerDlg::CEventExplorerDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CEventExplorerDlg::IDD, pParent) 
{ 
 	running = FALSE; 
	event = NULL; 
	manual = FALSE; 
	signalled = FALSE; 
 
	//{{AFX_DATA_INIT(CEventExplorerDlg) 
		// 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 CEventExplorerDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CEventExplorerDlg) 
	DDX_Control(pDX, IDC_ID2, c_ID2); 
	DDX_Control(pDX, IDC_ID1, c_ID1); 
	DDX_Control(pDX, IDC_HANDLE2, c_Handle2); 
	DDX_Control(pDX, IDC_HANDLE1, c_Handle1); 
	DDX_Control(pDX, IDC_CLEAR, c_Clear); 
	DDX_Control(pDX, IDC_CREATEEVENT, c_CreateEvent); 
	DDX_Control(pDX, IDC_SIGNALLED, c_Signalled); 
	DDX_Control(pDX, IDC_RUNNING2, c_Running2); 
	DDX_Control(pDX, IDC_RUNNING1, c_Running1); 
	DDX_Control(pDX, IDC_STOP, c_Stop); 
	DDX_Control(pDX, IDC_SETEVENT, c_SetEvent); 
	DDX_Control(pDX, IDC_RESETEVENT, c_ResetEvent); 
	DDX_Control(pDX, IDC_PULSEEVENT, c_PulseEvent); 
	DDX_Control(pDX, IDC_MANUAL_RESET, c_ManualReset); 
	DDX_Control(pDX, IDC_START, c_Start); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CEventExplorerDlg, CDialog) 
	//{{AFX_MSG_MAP(CEventExplorerDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_START, OnStart) 
	ON_BN_CLICKED(IDC_MANUAL_RESET, OnManualReset) 
	ON_BN_CLICKED(IDC_PULSEEVENT, OnPulseevent) 
	ON_BN_CLICKED(IDC_RESETEVENT, OnResetevent) 
	ON_BN_CLICKED(IDC_SETEVENT, OnSetevent) 
	ON_MESSAGE(UWM_CLOSEHANDLE, OnCloseHandle) 
	ON_BN_CLICKED(IDC_STOP, OnStop) 
	ON_MESSAGE(UWM_STEPIT, OnStepIt) 
	ON_BN_CLICKED(IDC_CLEAR, OnClear) 
	ON_BN_CLICKED(IDC_SIGNALLED, OnSignalled) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CEventExplorerDlg message handlers 
 
BOOL CEventExplorerDlg::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); 
	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 
	 
	thread1 = new RunningThread(&c_ID1, &c_Handle1, &c_Running1, this); 
	thread2 = new RunningThread(&c_ID2, &c_Handle2, &c_Running2, this); 
 
	enableControls(); 
	showCreateEvent(); 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
/**************************************************************************** 
*                          CEventExplorerDlg::showCreateEvent 
* Result: void 
*        
* Effect:  
*       Shows the CreateEvent call 
****************************************************************************/ 
 
void CEventExplorerDlg::showCreateEvent() 
    { 
     CString s; 
     s.Format(_T("CreateEvent(NULL, %s, %s, NULL)"), 
     			c_ManualReset.GetCheck() ? "TRUE" : "FALSE", 
			c_Signalled.GetCheck()? "TRUE" : "FALSE"); 
     c_CreateEvent.SetWindowText(s); 
    } 
 
void CEventExplorerDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAbout 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 CEventExplorerDlg::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 CEventExplorerDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void RunningThread::Create() 
    { 
     thread = AfxBeginThread(handler, this); 
 
     CString s; 
 
     s.Format(_T("%08x"), (DWORD)(thread->m_hThread)); 
     c_Handle->SetWindowText(s); 
     s.Format(_T("%08x"), (DWORD)(thread->m_nThreadID)); 
     c_ID->SetWindowText(s); 
    } 
 
void CEventExplorerDlg::OnStart()  
{ 
 running = TRUE; 
 manual = c_ManualReset.GetCheck() ? TRUE : FALSE; 
 signalled = c_Signalled.GetCheck() ? TRUE : FALSE; 
 
 event = CreateEvent(NULL, manual, signalled, NULL); 
 
 
 thread1->Create(); 
 thread2->Create(); 
 
 enableControls(); 
 
 //s.Format(_T("%08x"), (DWORD)event); 
 //c_Event.SetWindowText(s); 
} 
 
/**************************************************************************** 
*                               enableControls 
* Result: void 
*        
* Effect:  
*       Enables the various controls 
****************************************************************************/ 
 
void CEventExplorerDlg::enableControls() 
    { 
     c_Start.EnableWindow(!running); 
     c_Stop.EnableWindow(running); 
     c_PulseEvent.EnableWindow(running); 
     c_ResetEvent.EnableWindow(running); 
     c_SetEvent.EnableWindow(running); 
     c_ManualReset.EnableWindow(!running); 
     c_Signalled.EnableWindow(!running); 
    } 
 
/**************************************************************************** 
*			    RunningThread::handler 
* Inputs: 
*       LPVOID me: 'this' pointer to CEventExplorerDlg object 
* Result: int 
*       0, always 
* Effect:  
*       Runs the thread 
****************************************************************************/ 
 
UINT RunningThread::handler(LPVOID me) 
    { 
     ((RunningThread *)me)->runMe(); 
     return 0; 
    } 
 
/**************************************************************************** 
*			     RunningThread::runMe 
* Result: void 
*        
* Effect:  
*       Loops and waits 
****************************************************************************/ 
 
void RunningThread::runMe() 
    { 
     while(dialog->running) 
        { /* execute */ 
	 WaitForSingleObject(dialog->event, INFINITE); 
	 if(!dialog->running) 
	    break; // stop button will signal object, but don't continue! 
 
	 dialog->PostMessage(UWM_STEPIT, 0, (LPARAM)this); 
	 Sleep(500); 
	} /* execute */ 
     dialog->PostMessage(UWM_CLOSEHANDLE); 
    } 
 
void CEventExplorerDlg::OnManualReset()  
{ 
 showCreateEvent(); 
} 
 
void CEventExplorerDlg::OnSignalled()  
{ 
 showCreateEvent();	 
} 
 
void CEventExplorerDlg::OnPulseevent()  
{ 
 ::PulseEvent(event);	 
} 
 
void CEventExplorerDlg::OnResetevent()  
{ 
 ::ResetEvent(event);	 
} 
 
void CEventExplorerDlg::OnSetevent()  
{ 
 ::SetEvent(event);	 
} 
 
LRESULT CEventExplorerDlg::OnCloseHandle(WPARAM, LPARAM) 
{ 
 ::CloseHandle(event); 
 event = NULL; 
 c_ID1.SetWindowText(_T("")); 
 c_ID2.SetWindowText(_T("")); 
 c_Handle1.SetWindowText(_T("")); 
 c_Handle2.SetWindowText(_T("")); 
 return 0; 
} 
 
void CEventExplorerDlg::OnStop()  
{ 
 running = FALSE; 
 SetEvent(event); 
 SetEvent(event); 
 
 HANDLE events[2] = {thread1->thread->m_hThread, thread2->thread->m_hThread }; 
 WaitForMultipleObjects(2, events, TRUE, INFINITE); 
 
 enableControls(); 
} 
 
void CEventExplorerDlg::OnClear()  
{ 
 c_Running1.SetPos(0); 
 c_Running2.SetPos(0); 
	 
} 
 
 
/**************************************************************************** 
*                             CEventExplorerDlg::OnStepIt 
* Inputs: 
*       ² 
* Result: ² 
*       ² 
* Effect:  
*       ² 
****************************************************************************/ 
 
LRESULT CEventExplorerDlg::OnStepIt(WPARAM, LPARAM lParam) 
    { 
     RunningThread * rt = (RunningThread *) lParam; 
     rt->c_Progress->StepIt(); 
     return 0; 
    }