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

📄 mythreadview.cpp

📁 通过菜单的start 来开启一个线程程
💻 CPP
字号:
// MyThreadView.cpp : implementation of the CMyThreadView class
//

#include "stdafx.h"
#include "MyThread.h"
#include "math.h"
#include "MyThreadDoc.h"
#include "MyThreadView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
volatile int threadCount=0;
volatile int ThreadFlag;
/////////////////////////////////////////////////////////////////////////////
// CMyThreadView

IMPLEMENT_DYNCREATE(CMyThreadView, CView)

BEGIN_MESSAGE_MAP(CMyThreadView, CView)
	//{{AFX_MSG_MAP(CMyThreadView)
	ON_COMMAND(ID_startThread, OnstartThread)
	ON_COMMAND(ID_StopThread, OnStopThread)
    ON_MESSAGE(WM_THREADENDED,OnThreadEnded)
	//}}AFX_MSG_MAP
	/////////消息函数,用来发送消息窗口
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyThreadView construction/destruction

CMyThreadView::CMyThreadView()
{
	// TODO: add construction code here

}

CMyThreadView::~CMyThreadView()
{
}

BOOL CMyThreadView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyThreadView drawing

void CMyThreadView::OnDraw(CDC* pDC)
{
	CMyThreadDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMyThreadView diagnostics

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

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

CMyThreadDoc* CMyThreadView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyThreadDoc)));
	return (CMyThreadDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyThreadView message handlers



UINT ThreadPro(LPVOID lpParam)
{    
     int i=0;
	double r=35.0;
	double alpha=3.14/30;
	int cx,cy;
	
	if(threadCount>=8)
	{
		MessageBeep((WORD)-1);
		return 0;
	}
	cx=50+(threadCount%4)*120;
	 if(threadCount<4)
		 cy=60;
	 else cy=200;
	threadCount++;
	 CClientDC dc(((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->GetActiveView());//用来获得当前视图窗口
	 dc.SetROP2(R2_NOT);
	 dc.MoveTo(cx,cy);

   dc.LineTo(int(cx+r*sin(i*alpha)),int(cy+r*cos(i*alpha)));
	for (; ;)
	{  		if(ThreadFlag==0) break;
		
		dc.LineTo(cx,cy);
		 
        dc.LineTo(int(cx+r*cos(i*alpha)),int(cy+r*sin(i*alpha)));
		 i++;
		
      if(i==60)i=0;
		 ::Sleep(500);
	}
	  ::PostMessage((HWND)lpParam, WM_THREADENDED, 0, 0 );//////用来发送消息
	 return 0;

} 


void CMyThreadView::OnstartThread() 
{
	// TODO: Add your command handler code here
		HWND hWnd = GetSafeHwnd();
		ThreadFlag=1;

	AfxBeginThread( ThreadPro, hWnd, THREAD_PRIORITY_NORMAL );
}

void CMyThreadView::OnStopThread() 
{
	// TODO: Add your command handler code here
	ThreadFlag=0;
}

LONG CMyThreadView::OnThreadEnded( WPARAM wParam, LPARAM lParam )////消息函数
{
	CString str;
	str.Format("%s %d %s","第",threadCount--,"个线程终止");
		MessageBox(str);
	   return 0;
}

⌨️ 快捷键说明

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