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

📄 mythread.cpp

📁 MFC 函数实用手册 MFC 函数实用手册
💻 CPP
字号:
// MyThread.cpp : 实现文件
//

#include "stdafx.h"
#include "WinThreadExample.h"
#include "MyThread.h"


// CMyThread

//HANDLE CMyThread::m_hAnotherDead = CreateEvent(NULL, FALSE, FALSE, NULL);

IMPLEMENT_DYNAMIC(CMyThread, CWinThread)

// CMyThread 消息处理程序

BEGIN_MESSAGE_MAP(CMyThread, CWinThread)
END_MESSAGE_MAP()

CMyThread::CMyThread(CWnd* pWnd, HDC hDC,
				CPoint ptPos1, CPoint ptVel1, CPoint ptVel2, CSize Size, COLORREF color)
{
	m_pMainWnd = pWnd;
	m_pMainWnd->GetClientRect(&m_rectBorder);
	m_hDC = hDC;

	m_ptPosition1 = ptPos1;
	m_ptPosition2 = ptPos1 + Size;
	m_ptVelocity1 = ptVel1;
	m_ptVelocity2 = ptVel2;

	CPen pen(PS_SOLID, 1, color);
	m_hPen = (HPEN)pen.Detach();
}

CMyThread::~CMyThread()
{
}

BOOL CMyThread::InitInstance()
{
	// TODO: 在此执行任意逐线程初始化
	m_pen.Attach(m_hPen);

	m_dc.Attach(m_hDC);

	while (true)
	{
		DrawLine();
		Sleep(1);
	}

	m_dc.Detach();

	return FALSE;
}

void CMyThread::DrawLine()
{
	m_ptPosition1.Offset(m_ptVelocity1);
	m_ptPosition2.Offset(m_ptVelocity2);

	if (m_ptPosition1.y<m_rectBorder.top)
		m_ptVelocity1.y = (m_ptVelocity1.y>0) ? m_ptVelocity1.y : -m_ptVelocity1.y;
	else if (m_ptPosition1.y>m_rectBorder.bottom)
		m_ptVelocity1.y = (m_ptVelocity1.y>0) ? -m_ptVelocity1.y : m_ptVelocity1.y;

	if (m_ptPosition2.y<m_rectBorder.top)
		m_ptVelocity2.y = (m_ptVelocity2.y>0) ? m_ptVelocity2.y : -m_ptVelocity2.y;
	else if (m_ptPosition2.y>m_rectBorder.bottom)
		m_ptVelocity2.y = (m_ptVelocity2.y>0) ? -m_ptVelocity2.y : m_ptVelocity2.y;

	if (m_ptPosition1.x<m_rectBorder.left)
		m_ptVelocity1.x = (m_ptVelocity1.x>0) ? m_ptVelocity1.x : -m_ptVelocity1.x;
	else if (m_ptPosition1.x>m_rectBorder.right)
		m_ptVelocity1.x = (m_ptVelocity1.x>0) ? -m_ptVelocity1.x : m_ptVelocity1.x;

	if (m_ptPosition2.x<m_rectBorder.left)
		m_ptVelocity2.x = (m_ptVelocity2.x>0) ? m_ptVelocity2.x : -m_ptVelocity2.x;
	else if (m_ptPosition2.x>m_rectBorder.right)
		m_ptVelocity2.x = (m_ptVelocity2.x>0) ? -m_ptVelocity2.x : m_ptVelocity2.x;

	CPen* oldpen;

	oldpen = m_dc.SelectObject(&m_pen);
	m_dc.MoveTo(m_ptPosition1);
	m_dc.LineTo(m_ptPosition2);
	m_dc.SelectObject(oldpen);
}

void CMyThread::Kill()
{
	VERIFY(SetEvent(CreateEvent(NULL, TRUE, FALSE, NULL)));
	SetThreadPriority(THREAD_PRIORITY_ABOVE_NORMAL);
	WaitForSingleObject(CreateEvent(NULL, TRUE, FALSE, NULL), INFINITE);
	WaitForSingleObject(m_hThread, INFINITE);
	delete this;
}

⌨️ 快捷键说明

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