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

📄 threadview.cpp

📁 这是一个多线程的代码
💻 CPP
字号:
// ThreadView.cpp : CThreadView 类的实现
//

#include "stdafx.h"
#include "Thread.h"

#include "ThreadDoc.h"
#include "ThreadView.h"
#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CThreadView

IMPLEMENT_DYNCREATE(CThreadView, CView)

BEGIN_MESSAGE_MAP(CThreadView, CView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
UINT ThreadProc( LPVOID lpParam )
{
	THREAD_INFO *lpThreadInfo =(THREAD_INFO *) lpParam;

	unsigned char Red, Green, Blue;
	int nFixedRadius = 80;
	int nMovingRadius = 10;
	int nMovingOffset = 70;

	Red = (unsigned char)
		( GetTickCount() & 0x000000ff );
	Green = (unsigned char)
		( ( GetTickCount() & 0x00000ff0 ) >> 4 );
	Blue = (unsigned char)
		( ( GetTickCount() & 0x0000ff00 ) >> 8 );

	while( *lpThreadInfo->lpKillThread == FALSE )
	{
		HDC hdc = ::GetDC( lpThreadInfo->hWnd );

		RECT Rect;
		::GetClientRect( lpThreadInfo->hWnd, &Rect );
		int nMidx = Rect.right / 2;
		int nMidy = Rect.bottom / 2;

		::InvalidateRect( lpThreadInfo->hWnd, NULL, TRUE );
		::UpdateWindow( lpThreadInfo->hWnd );

		HPEN hPen, hOldPen;
		hPen =
			::CreatePen( PS_SOLID, 1, RGB( Red, Green, Blue ) );
		hOldPen = (HPEN) ::SelectObject( hdc, hPen );

		int prevx, prevy, x = 0, y = 0;
		for( int i=0; i<=500; i++ )
		{
			prevx = x;
			prevy = y;

			x = (int) ( ( nFixedRadius + nMovingRadius ) *
				cos( (double) i ) -
				( nMovingRadius + nMovingOffset ) *
				cos((double)(( ( nFixedRadius + nMovingRadius ) /
				nMovingRadius ) * i ) ) );
			y = (int) ( ( nFixedRadius + nMovingRadius ) *
				sin( (double) i ) -
				( nMovingRadius + nMovingOffset ) *
				sin((double)(( ( nFixedRadius + nMovingRadius ) /
				nMovingRadius ) * i ) ) );

			if( i > 0 )
				::LineTo( hdc, x + nMidx, y + nMidy );
			else
				::MoveToEx( hdc, x + nMidx, y + nMidy, NULL );

		}

		Red += 6;
		Green += 5;
		Blue += 4;

		nFixedRadius++;
		if( nFixedRadius > 170 )
			nFixedRadius = 90;
	
		nMovingRadius++;
		if( nMovingRadius > 40 )
			nMovingRadius = 10;

		nMovingOffset++;
		if( nMovingOffset > 100 )
			nMovingOffset = 70;

		::SelectObject( hdc, hOldPen );
		::DeleteObject( hPen );
		::ReleaseDC( lpThreadInfo->hWnd, hdc );

		Sleep( 200 );
		}

	return( 0 );

}
// CThreadView 构造/销毁

CThreadView::CThreadView()
{
	// TODO: 在此处添加构造代码
	m_bKillThread = FALSE;
	m_pThread = NULL;

}

CThreadView::~CThreadView()
{
	HANDLE hThread = m_pThread->m_hThread;
	m_bKillThread = TRUE;
	::WaitForSingleObject( hThread, 5000 );
}

BOOL CThreadView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
	// 样式

	return CView::PreCreateWindow(cs);
}

// CThreadView 绘制

void CThreadView::OnDraw(CDC* /*pDC*/)
{
	CThreadDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: 在此处为本机数据添加绘制代码
		if( m_pThread == NULL )
		{
			m_ThreadInfo.hWnd = m_hWnd;
			m_ThreadInfo.lpKillThread = &m_bKillThread;
			m_pThread =
				AfxBeginThread(ThreadProc, (LPVOID) &m_ThreadInfo);
		}
}


// CThreadView 打印

BOOL CThreadView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CThreadView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 打印前添加额外的初始化
}

void CThreadView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 打印后添加清除过程
}


// CThreadView 诊断

#ifdef _DEBUG
void CThreadView::AssertValid() const
{
	CView::AssertValid();
}

void CThreadView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CThreadDoc* CThreadView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CThreadDoc)));
	return (CThreadDoc*)m_pDocument;
}
#endif //_DEBUG


// CThreadView 消息处理程序

⌨️ 快捷键说明

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