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

📄 threadsdlg.cpp

📁 关于使用VC编程的代码
💻 CPP
字号:
// ThreadsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Threads.h"
#include "ThreadsDlg.h"
#include "afxmt.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CEvent gPixelEventKill, gTimeEventKill;
CCriticalSection gCS;
int gCount;

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CThreadsDlg dialog

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

BEGIN_MESSAGE_MAP(CThreadsDlg, CDialog)
	//{{AFX_MSG_MAP(CThreadsDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BEGIN_BUTTON, OnBeginButton)
	ON_BN_CLICKED(IDC_END_BUTTON, OnEndButton)
	ON_BN_CLICKED(IDC_PRIORITY_BEGIN_BUTTON, OnPriorityBeginButton)
	ON_BN_CLICKED(IDC_PRIORITY_END_BUTTON, OnPriorityEndButton)
	ON_WM_CHAR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CThreadsDlg message handlers

BOOL CThreadsDlg::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

	m_nProgressMin = 0;
	m_nProgressMax = 100;

	CProgressCtrl* pCtrl;

	pCtrl = (CProgressCtrl*)GetDlgItem(IDC_HIGHEST_PROGRESS);
	pCtrl->SetRange(m_nProgressMin, m_nProgressMax);
	pCtrl->SetPos(m_nProgressMin);
	pCtrl = (CProgressCtrl*)GetDlgItem(IDC_ABOVE_NORMAL_PROGRESS);
	pCtrl->SetRange(m_nProgressMin, m_nProgressMax);
	pCtrl->SetPos(m_nProgressMin);
	pCtrl = (CProgressCtrl*)GetDlgItem(IDC_NORMAL_PROGRESS);
	pCtrl->SetRange(m_nProgressMin, m_nProgressMax);
	pCtrl->SetPos(m_nProgressMin);
	pCtrl = (CProgressCtrl*)GetDlgItem(IDC_BELOW_NORMAL_PROGRESS);
	pCtrl->SetRange(m_nProgressMin, m_nProgressMax);
	pCtrl->SetPos(m_nProgressMin);
	pCtrl = (CProgressCtrl*)GetDlgItem(IDC_LOWEST_PROGRESS);
	pCtrl->SetRange(m_nProgressMin, m_nProgressMax);
	pCtrl->SetPos(m_nProgressMin);

	CWnd* pWnd;

	pWnd = GetDlgItem(IDC_HIGHEST_STATIC);
	pWnd->SetWindowText("0");
	pWnd = GetDlgItem(IDC_ABOVE_NORMAL_STATIC);
	pWnd->SetWindowText("0");
	pWnd = GetDlgItem(IDC_NORMAL_STATIC);
	pWnd->SetWindowText("0");
	pWnd = GetDlgItem(IDC_BELOW_NORMAL_STATIC);
	pWnd->SetWindowText("0");
	pWnd = GetDlgItem(IDC_LOWEST_STATIC);
	pWnd->SetWindowText("0");

	CString strTime;
	CTime t;

	t = CTime::GetCurrentTime();
	strTime.Format("%d:%d:%d", t.GetHour(), t.GetMinute(), t.GetSecond());
	GetDlgItem(IDC_TIME_STATIC)->SetWindowText(strTime);

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

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

UINT ShowPixel(LPVOID pParam)
{
	CThreadsDlg* pDlg;
    CWnd* pWnd;
    CDC* pControlDC;
 	int nX,nY,nRed,nGreen,nBlue;
	CRect ClientRect;

	pDlg = (CThreadsDlg*)pParam;
	pWnd = pDlg->GetDlgItem(IDC_PIXEL_STATIC); 
	pWnd->GetClientRect(&ClientRect);
	pControlDC = pWnd->GetDC();
	while(TRUE)
	{
		nX = rand() % ClientRect.right;
		nY = rand() % ClientRect.bottom;
		nRed = rand() % 256;
		nGreen = rand() % 256;
		nBlue = rand() % 256;
		pControlDC->SetPixel(nX,nY,RGB(nRed,nGreen,nBlue));
		if( ::WaitForSingleObject(gPixelEventKill, 0) == WAIT_OBJECT_0 )
		{
			break;
		}
	}
	pWnd->ReleaseDC(pControlDC);
	return 0;
}

UINT ShowCurrentTime(LPVOID pParam)
{
	CThreadsDlg* pDlg;
	CString strOld,strNew;
	CTime tOld,tNew;
	
	pDlg = (CThreadsDlg*)pParam;
	tOld = CTime::GetCurrentTime();
	while(TRUE)
	{
		tNew = CTime::GetCurrentTime();
		if( tOld != tNew )
		{
			tOld = tNew;
			strNew.Format("%d:%d:%d", tNew.GetHour(), tNew.GetMinute(), tNew.GetSecond());
			pDlg->GetDlgItem(IDC_TIME_STATIC)->SetWindowText(strNew);
			if( ::WaitForSingleObject(gTimeEventKill, 0) == WAIT_OBJECT_0 )
			{
				break;
			}
		}
	}
	return 0;
}

void CThreadsDlg::OnBeginButton() 
{
	AfxBeginThread(ShowCurrentTime, this , THREAD_PRIORITY_NORMAL);	
	AfxBeginThread(ShowPixel, this , THREAD_PRIORITY_NORMAL);	
}



void CThreadsDlg::OnEndButton() 
{
	gPixelEventKill.SetEvent();	
	gTimeEventKill.SetEvent();
}

UINT Priority_Sample(LPVOID pParam)
{
	int nIndex,i,j;
	int nProgress,nMin,nMax;
	BOOL bFinished;
	CThreadsDlg* pDlg;
	CProgressCtrl* pCtrl;
	CWnd* pWnd;
	CString strProgress;

	nIndex = *((int *)pParam);
	pDlg = (CThreadsDlg*)AfxGetApp()->m_pMainWnd;
	nMin = pDlg->m_nProgressMin;
	nMax = pDlg->m_nProgressMax;
	bFinished = FALSE;

	for(nProgress=nMin;nProgress<nMax;nProgress++)
	{
		switch (nIndex)
		{
		case 1:
			{
				pCtrl = (CProgressCtrl*)pDlg->GetDlgItem(IDC_HIGHEST_PROGRESS);
				pWnd = pDlg->GetDlgItem(IDC_HIGHEST_STATIC);
				break;
			}
		case 2:
			{
				pCtrl = (CProgressCtrl*)pDlg->GetDlgItem(IDC_ABOVE_NORMAL_PROGRESS);
				pWnd = pDlg->GetDlgItem(IDC_ABOVE_NORMAL_STATIC);
				break;
			}
		case 3:
			{
				pCtrl = (CProgressCtrl*)pDlg->GetDlgItem(IDC_NORMAL_PROGRESS);
				pWnd = pDlg->GetDlgItem(IDC_NORMAL_STATIC);
				break;
			}
		case 4:
			{
				pCtrl = (CProgressCtrl*)pDlg->GetDlgItem(IDC_BELOW_NORMAL_PROGRESS);
				pWnd = pDlg->GetDlgItem(IDC_BELOW_NORMAL_STATIC);
				break;
			}
		case 5:
			{
				pCtrl = (CProgressCtrl*)pDlg->GetDlgItem(IDC_LOWEST_PROGRESS);
				pWnd = pDlg->GetDlgItem(IDC_LOWEST_STATIC);
				break;
			}
		}
		strProgress.Format("%d",nProgress);
		pWnd->SetWindowText(strProgress);
		pCtrl->SetPos(nProgress);

		gCS.Lock();
		if(gCount == 1)
		{
			bFinished = TRUE;
		}
		gCS.Unlock();
		if(bFinished == TRUE)
		{
			break;
		}

		for(i=0;i<9999;i++)
			for(j=0;j<3000;j++)
			{}
		Sleep(100);
	}
	return 0;
}

void CThreadsDlg::OnPriorityBeginButton() 
{
	static DWORD nIndex[5] = {1,2,3,4,5};
	
	gCount = 0;
	AfxBeginThread(Priority_Sample, (LPVOID)&nIndex[0], THREAD_PRIORITY_HIGHEST);
	AfxBeginThread(Priority_Sample, (LPVOID)&nIndex[1], THREAD_PRIORITY_ABOVE_NORMAL);
	AfxBeginThread(Priority_Sample, (LPVOID)&nIndex[2], THREAD_PRIORITY_NORMAL);
	AfxBeginThread(Priority_Sample, (LPVOID)&nIndex[3], THREAD_PRIORITY_BELOW_NORMAL);
	AfxBeginThread(Priority_Sample, (LPVOID)&nIndex[4], THREAD_PRIORITY_LOWEST);	
	
}

void CThreadsDlg::OnPriorityEndButton() 
{
	gCS.Lock();
	gCount = 1;
	gCS.Unlock();
}

⌨️ 快捷键说明

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