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

📄 mythreadview.cpp

📁 经典C++书籍钱能C++书籍例子代码实例
💻 CPP
字号:
// MyThreadView.cpp : implementation of the CMyThreadView class
//

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

#include "MyThreadDoc.h"
#include "MyThreadView.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

volatile int ThreadCount=0;
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)
	//}}AFX_MSG_MAP
		ON_MESSAGE(WM_THREADENDED,OnThreadEnded)
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
// CThreadView message handlers
/*
UINT ThreadProc(LPVOID param)
{
	

	int i=0;
	double r=35.0;
	double alpha=2*3.14/60;
	int cx,cy;
	cx =50+(ThreadCount%4)*120;

	if(ThreadCount>= 8){MessageBeep((WORD)-1);return 0;}
	if(ThreadCount<4)cy=60;
	else             cy=200;
	
	ThreadCount++;
	
	CClientDC dc(((CFrameWnd *)(AfxGetApp()->m_pMainWnd))->GetActiveView());
   	
    dc.SetROP2(R2_NOT);
	//draw second time begins

    dc.MoveTo(cx,cy);

	dc.LineTo(int(cx+r),cy);
		     

	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(100);
	}

::PostMessage((HWND)param,WM_THREADENDED,0,0);
	return 0;
}*/

UINT ThreadProc(LPVOID param)
{
	char currenttime[30];
	struct tm *newtime;
	long ltime;

	time(&ltime);
	newtime=localtime(&ltime);
	
	strcpy(currenttime,asctime(newtime));
	currenttime[24]=' ';
	currenttime[25]=0;

	int i=0,k=-15+newtime->tm_min;
	double r=35.0,rk=28.0,rh=20.0,rp=30.0;
	double alpha=3.14/30.0;
	int cx,cy,r1=37;
	cx =50+(ThreadCount%4)*120;

	if(ThreadCount>= 8){MessageBeep((WORD)-1);return 0;}
	if(ThreadCount<4)cy=60;
	else             cy=200;
    
	
	CClientDC dc(((CFrameWnd *)(AfxGetApp()->m_pMainWnd))->GetActiveView());
   	
	//begin draw number

		dc.SetBkMode(TRANSPARENT);
		CFont MyFont;
		MyFont.CreateFont(20,0,0,0,10,false,false,
			0,ANSI_CHARSET,
			OUT_DEFAULT_PRECIS,
			CLIP_DEFAULT_PRECIS,
			DEFAULT_QUALITY,
			DEFAULT_PITCH|FF_SWISS,
			"Arial");
		CFont *pOldFont = dc.SelectObject(&MyFont);
		
		dc.SetTextColor(RGB(0,25,0));

		//draw  number 1----12
		char s[6];
		int number=1,j;
		float r2=45.0;
		for(j= -10;j<60-10;j+=5,number++)
		{
		dc.TextOut(int(cx-5+r2*cos(j*alpha)),
		      int(cy-10+r2*sin(j*alpha)),itoa(number,s,10));
		}
		// end  draw number 1----12
        switch(ThreadCount)
		{
		case 0:
			dc.TextOut(cx-35,cy+int(r2)+20,"泰国曼谷");
			break;
		case 1:
			dc.TextOut(cx-35,cy+int(r2)+20,"中国北京");
			break;
		case 2: 
			dc.TextOut(cx-35,cy+int(r2)+20,"日本东京");
			break;
		default:
			dc.TextOut(cx-35,cy+int(r2)+20,"悉    尼");
			break;
		}
		dc.SelectObject(pOldFont);
	    //end draw number

	//draw circle begins
	CPen mypen;
	mypen.CreatePen(PS_SOLID,1,RGB(20,20,200));
	CPen *po;
	po=dc.SelectObject(&mypen);
	CRect myrect(cx-r1,cy-r1,cx+r1,cy+r1);
	

	dc.Ellipse(&myrect);
	for(i=0;i<= 60;i+=5)
	{
	dc.MoveTo(int(cx+(rp-1)*cos(i*alpha)),
		      int(cy+(rp-1)*sin(i*alpha)));
	dc.LineTo(int(cx+(rp+1)*cos(i*alpha)),
		      int(cy+(rp+1)*sin(i*alpha)));
    }
	dc.SelectObject(po);
	//draw circle end

	//draw second time begins
	dc.SetROP2(R2_NOT);
	time(&ltime);
	newtime=localtime(&ltime);
	i= -15+newtime->tm_sec;
	k= -15+newtime->tm_min;
    
	switch(ThreadCount)
	{
	case 0:
		newtime->tm_hour= (newtime->tm_hour-1)%24;
		break;
	case 1:
		newtime->tm_hour= (newtime->tm_hour)%24;
		break;
	case 2:
		newtime->tm_hour= (newtime->tm_hour+1)%24;
		break;
	default:
		newtime->tm_hour= (newtime->tm_hour+2)%24;
	}
	ThreadCount++;
	int hour= -15+(newtime->tm_hour)*5+newtime->tm_min/12;
	dc.MoveTo(cx,cy);
	dc.LineTo(int(cx+rh*cos(hour*alpha)),
		      int(cy+rh*sin(hour*alpha)));//画一条直线hour
	
    dc.MoveTo(cx,cy);
	dc.LineTo(int(cx+rk*cos(k*alpha)),
		      int(cy+rk*sin(k*alpha)));//画一条直线minute
	//i= -15+newtime->tm_sec;	
    dc.SetTextColor(RGB(255,0,0));
	dc.MoveTo(cx,cy);
	dc.LineTo(int(cx+r*cos(i*alpha)),
		     int(cy+r*sin(i*alpha)));//画一条新直线second

	for(;;)
	{
	if(ThreadFlag==0)break;
	
	dc.MoveTo(cx,cy);
	dc.LineTo(int(cx+r*cos(i*alpha)),
		      int(cy+r*sin(i*alpha)));//删除上一条直线
	i++;
	if(i==60-15	)
	{
		i= -15;
		dc.SetTextColor(RGB(0,25,0));
		dc.MoveTo(cx,cy);
		dc.LineTo(int(cx+rk*cos(k*alpha)),
		      int(cy+rk*sin(k*alpha)));//删除上一条直线minute
		k++;
		dc.MoveTo(cx,cy);
		dc.LineTo(int(cx+rk*cos(k*alpha)),
		      int(cy+rk*sin(k*alpha)));//画一条新直线
		dc.SetTextColor(RGB(255,0,0));
	}

	dc.MoveTo(cx,cy);
	dc.LineTo(int(cx+r*cos(i*alpha)),
		      int(cy+r*sin(i*alpha)));//画一条新直线
    
	::Sleep(1000);
	}
	::PostMessage((HWND)param,WM_THREADENDED,0,0);
	return 0;
}

void CMyThreadView::OnStartThread() 
{
	ThreadFlag=1;	// TODO: Add your command handler code here
	HWND hWnd=GetSafeHwnd();
	AfxBeginThread(ThreadProc,hWnd);
}



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

LONG CMyThreadView::OnThreadEnded(WPARAM wParam,LPARAM lParam) 
{
	// TODO: Add your command handler code here
	char s[10];
	itoa(ThreadCount--,s,10);
	char str[30]="第 ";
	strcat(str,s);
	strcat(str," 个线程终止"); 
	MessageBox(str);
	return 0;
}

⌨️ 快捷键说明

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