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

📄 uithreaddlg.cpp

📁 从学案例入手
💻 CPP
字号:
// UIThreadDlg.cpp : implementation file
//

#include "stdafx.h"
#include "UIThread.h"
#include "UIThreadDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CUIThreadDlg dialog

#include "CounterThread.h" //
#include "DisplayThread.h" //

CCounterThread* m_pCounterThread;//指向计数线程的指针
CDisplayThread* m_pDisplayThread;//指向显示线程的指针


CUIThreadDlg::CUIThreadDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUIThreadDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUIThreadDlg)
		// 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 CUIThreadDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUIThreadDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUIThreadDlg, CDialog)
	//{{AFX_MSG_MAP(CUIThreadDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELCHANGE(IDC_PRIORITYCLASS, OnSelChangePriorityClass)
	ON_CBN_SELCHANGE(IDC_DSPYTHRDPRIORITY, OnSelChangeDspyThrdPriority)
	ON_CBN_SELCHANGE(IDC_CNTRTHRDPRIORITY, OnSelChangeCntrThrdPriority)
	ON_BN_CLICKED(IDC_PAUSE, OnPause)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUIThreadDlg message handlers

BOOL CUIThreadDlg::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
	CComboBox* pBox;
	pBox=(CComboBox*) GetDlgItem(IDC_PRIORITYCLASS);
	ASSERT(pBox != NULL);
	if (pBox != NULL){
		pBox->AddString(_T("Idle"));
		pBox->AddString(_T("Normal"));
		pBox->AddString(_T("High"));
		pBox->AddString(_T("Realtime"));
		pBox->SetCurSel(1); 
	}

	//初始化显示线程优先级组合框并置缺省为 NORMAL
	pBox=(CComboBox*) GetDlgItem(IDC_DSPYTHRDPRIORITY);
	ASSERT(pBox != NULL);
	if (pBox != NULL){
		pBox->AddString(_T("Idle"));
		pBox->AddString(_T("Lowest"));
		pBox->AddString(_T("Below normal"));
		pBox->AddString(_T("Normal"));
		pBox->AddString(_T("Above normal"));
		pBox->AddString(_T("Highest"));
		pBox->AddString(_T("Timecritical"));
		pBox->SetCurSel(3); 
	}

	//初始化计数线程优先级组合框并置缺省为 NORMAL
	pBox=(CComboBox*) GetDlgItem(IDC_CNTRTHRDPRIORITY);
	ASSERT(pBox != NULL);
	if (pBox != NULL){
		pBox->AddString(_T("Idle"));
		pBox->AddString(_T("Lowest"));
		pBox->AddString(_T("Below normal"));
		pBox->AddString(_T("Normal"));
		pBox->AddString(_T("Above normal"));
		pBox->AddString(_T("Highest"));
		pBox->AddString(_T("Timecritical"));
		pBox->SetCurSel(3); 
	}

	//初始化线程挂起复选框为挂起状态
	CButton* pCheck=(CButton*) GetDlgItem(IDC_PAUSE);
	pCheck->SetCheck(1);

	//初始化线程
	m_pDisplayThread =(CDisplayThread*)AfxBeginThread(
		RUNTIME_CLASS(CDisplayThread), 
		THREAD_PRIORITY_NORMAL,
		0, 
		CREATE_SUSPENDED
	);
	m_pDisplayThread->SetOwner(this);

	m_pCounterThread=(CCounterThread*)AfxBeginThread(
		RUNTIME_CLASS(CCounterThread), 
		THREAD_PRIORITY_NORMAL,
		0, 
		CREATE_SUSPENDED
	);
	m_pCounterThread->SetOwner(this);


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

void CUIThreadDlg::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 CUIThreadDlg::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 CUIThreadDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CUIThreadDlg::AddToListBox(LPCTSTR szBuffer)
{
	CListBox* pBox=(CListBox*) GetDlgItem(IDC_DATABOX);
	ASSERT(pBox != NULL);
	if (pBox!=NULL){
		int x=pBox->AddString(szBuffer);
		pBox->SetCurSel(x);
		if(pBox->GetCount()>100)
			pBox->DeleteString(0);
	}
}

void CUIThreadDlg::OnSelChangePriorityClass() 
{
	// TODO: Add your control notification handler code here
	DWORD dw;
	//取焦点选项 
	CComboBox* pBox=(CComboBox*)GetDlgItem(IDC_PRIORITYCLASS);
	int nCurSel=pBox->GetCurSel();
	switch (nCurSel){
	case 0:
		dw=IDLE_PRIORITY_CLASS;
		break;
	case 1:
	default:
		dw=NORMAL_PRIORITY_CLASS;
		break;
	case 2:
		dw=HIGH_PRIORITY_CLASS;
		break;
	case 3:
		dw=REALTIME_PRIORITY_CLASS;
		break;
	}

	SetPriorityClass(GetCurrentProcess(), dw);//调整优先级	
}

void CUIThreadDlg::OnPriorityChange(UINT nID)
{
	ASSERT(nID==IDC_CNTRTHRDPRIORITY||nID == IDC_DSPYTHRDPRIORITY);
	DWORD dw;
	//取对应该ID的焦点选项
	CComboBox* pBox=(CComboBox*) GetDlgItem(nID);
	int nCurSel=pBox->GetCurSel();
	switch (nCurSel){
	case 0:
		dw=(DWORD)THREAD_PRIORITY_IDLE;
		break;
	case 1:
		dw=(DWORD)THREAD_PRIORITY_LOWEST;
		break;
	case 2:
		dw=(DWORD)THREAD_PRIORITY_BELOW_NORMAL;
		break;
	case 3:
	default:
		dw=(DWORD)THREAD_PRIORITY_NORMAL;
		break;
	case 4:
		dw=(DWORD)THREAD_PRIORITY_ABOVE_NORMAL;
		break;
	case 5:
		dw=(DWORD)THREAD_PRIORITY_HIGHEST;
		break;
	case 6:
		dw=(DWORD)THREAD_PRIORITY_TIME_CRITICAL;
		break;
	}
	if(nID==IDC_CNTRTHRDPRIORITY)
		m_pCounterThread->SetThreadPriority(dw);
	//调整计数线程优先级
	else
		m_pDisplayThread->SetThreadPriority(dw);
	//调整显示线程优先级
}

void CUIThreadDlg::OnSelChangeDspyThrdPriority() 
{
	// TODO: Add your control notification handler code here
	OnPriorityChange(IDC_DSPYTHRDPRIORITY);
}

void CUIThreadDlg::OnSelChangeCntrThrdPriority() 
{
	// TODO: Add your control notification handler code here
	OnPriorityChange(IDC_CNTRTHRDPRIORITY);	
}

void CUIThreadDlg::OnPause() 
{
	// TODO: Add your control notification handler code here
	//取挂起复选框状态
	CButton* pCheck=(CButton*)GetDlgItem(IDC_PAUSE);
	BOOL bPaused=((pCheck->GetState() & 0x003) != 0);
	if (bPaused) {
		m_pCounterThread->SuspendThread();
		m_pDisplayThread->SuspendThread();
	}//挂起线程
	else {
		m_pCounterThread->ResumeThread();
		m_pDisplayThread->ResumeThread();
	}//恢复线程运行
}

void CUIThreadDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	int nCount=0;
	DWORD dwStatus;
	//取挂起复选框状态
	CButton* pCheck=(CButton*) GetDlgItem(IDC_PAUSE);
	BOOL bPaused=((pCheck->GetState() & 0x003) != 0);
	
	if (bPaused == TRUE){
		pCheck->SetCheck(0);//复选取消
		m_pCounterThread->ResumeThread();
		//恢复线程运行
		m_pDisplayThread->ResumeThread();
	}

	if (m_pCounterThread != NULL){
		VERIFY(::GetExitCodeThread(m_pCounterThread->m_hThread, &dwStatus));//取计数线程结束码
		if (dwStatus == STILL_ACTIVE){
			nCount++;
			m_pCounterThread->m_bDone=TRUE;
		}//如果仍为运行状态,则终止
		else{
			delete m_pCounterThread;
			m_pCounterThread=NULL;
		}//如果已经终止,则删除该线程对象
	}

	if (m_pDisplayThread != NULL){
		VERIFY(::GetExitCodeThread(m_pDisplayThread->m_hThread, &dwStatus));//取显示线程结束码
		if (dwStatus == STILL_ACTIVE){
			nCount++;
			m_pDisplayThread->m_bDone=TRUE;
		}//如果仍为运行状态,则终止
		else{
			delete m_pDisplayThread;
			m_pDisplayThread=NULL;
		}//如果已经终止,则删除该线程对象
	}

	if (nCount == 0)//两个线程均终止,则关闭程序
		CDialog::OnClose();
	else //否则发送WM_CLOSE消息
	PostMessage(WM_CLOSE, 0, 0);
}

⌨️ 快捷键说明

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