`

[置顶] 多任务和多线程(2)

阅读更多

创建一个有四个窗口的多线程程序,第一个窗口显示一个递增的数列,第二个窗口显示一个递增的质数数列,第三个窗口显示一个递增的斐波那契数列,第四个窗口显示一些随机产生大小不一的圆

一个可能的方案是使用WM_TIMER消息中处理多个更新,但是没有人圆心根据计算机速度来写程序;

看看多线程的解决方法:

程序有点长,思路却很清晰

#include<windows.h>
#include<math.h>
#include<process.h>

typedef struct  
{
	HWND hwnd;
	int cxClient;
	int cyClient;
	int cyChar;
	BOOL bKill;

}
PARAMS,*PPARAMS;


LRESULT CALLBACK WindowProc(
			HWND hwnd,      // handle to window
			UINT uMsg,      // message identifier
			WPARAM wParam,  // first message parameter
			LPARAM lParam   // second message parameter
			);

int WINAPI WinMain(
		HINSTANCE hInstance,      // handle to current instance
		HINSTANCE hPrevInstance,  // handle to previous instance
		LPSTR lpCmdLine,          // command line
		int nCmdShow              // show state
		)
{
	static TCHAR szAppName[]=TEXT("leidemingzi");
	HWND hwnd;
	MSG msg;
	WNDCLASS wndclass;

	wndclass.cbClsExtra=0;
	wndclass.cbWndExtra=0;
	wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
	wndclass.hInstance=hInstance;
	wndclass.lpfnWndProc=WindowProc;
	wndclass.lpszClassName=szAppName;
	wndclass.lpszMenuName=NULL;
	wndclass.style=CS_HREDRAW|CS_VREDRAW;

	if(!RegisterClass(&wndclass))
	{
		MessageBox(NULL,TEXT("the program require the window nt"),TEXT("tips"),MB_ICONERROR);
		return 0;
	}

	hwnd=CreateWindow(
		szAppName,  // registered class name
		TEXT("this is title"), // window name
		WS_OVERLAPPEDWINDOW,        // window style
		CW_USEDEFAULT,                // horizontal position of window
		CW_USEDEFAULT,                // vertical position of window
		CW_USEDEFAULT,           // window width
		CW_USEDEFAULT,          // window height
		NULL,      // handle to parent or owner window
		NULL,          // menu handle or child identifier
		hInstance,  // handle to application instance
		NULL       // window-creation data
		);

	ShowWindow(hwnd,nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

int CheckBottom(HWND hwnd,int cyClient,int cyChar,int iLine)
{
	if(iLine*cyChar+cyChar>cyClient)
	{
		InvalidateRect(hwnd,NULL,TRUE);
		UpdateWindow(hwnd);
		iLine=0;
	}
	return iLine;
}

//the window1
VOID Thread1(PVOID pvoid)
{
	HDC hdc;
	int iNum=0,iLine=0;
	PPARAMS pparams;
	TCHAR szBuffer[20];

	pparams=(PPARAMS)pvoid;

	while(!pparams->bKill)
	{
		if(iNum<0)
			iNum=0;
		iLine=CheckBottom(pparams->hwnd,pparams->cyClient,pparams->cyChar,iLine);

		hdc=GetDC(pparams->hwnd);
		TextOut(hdc,0,iLine*pparams->cyChar,szBuffer,wsprintf(szBuffer,TEXT("%d"),iNum++));
		ReleaseDC(pparams->hwnd,hdc);
		iLine++;
	}
	_endthread();
}


LRESULT CALLBACK WindowProc1(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	static PARAMS params;
	switch(uMsg)
	{
	case WM_CREATE:
		params.hwnd=hwnd;
		params.cyChar=HIWORD(GetDialogBaseUnits());
		_beginthread(Thread1,0,¶ms);
		return 0;

	case WM_SIZE:
		params.cyClient=HIWORD(lParam);
		return 0;

	case WM_DESTROY:
		params.bKill=TRUE;
		return 0;
	}

	return DefWindowProc(hwnd,uMsg,wParam,lParam);
}

//the second window
VOID Thread2(PVOID pvoid)
{
	HDC hdc;
	int iNum=1,iLine=0,i,iSqrt;
	PPARAMS pparams;
	TCHAR szBuffer[20];

	pparams=(PPARAMS)pvoid;

	while(!pparams->bKill)
	{
		do 
		{
			if(++iNum<0)
			{
				iNum=0;
			}
			iSqrt=(int)sqrt((double)iNum);

			for(i=2;i<=iSqrt;++i)
			{
				if(iNum%i==0)
					break;
			}
		} while (i<=iSqrt);
		iLine=CheckBottom(pparams->hwnd,pparams->cyClient,pparams->cyChar,iLine);
		hdc=GetDC(pparams->hwnd);

		TextOut(hdc,0,iLine*pparams->cyChar,szBuffer,wsprintf(szBuffer,TEXT("%d"),iNum));

		ReleaseDC(pparams->hwnd,hdc);
		iLine++;
	}
	_endthread();
}

LRESULT CALLBACK WindowProc2(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	static PARAMS params;

	switch(uMsg)
	{
	case WM_CREATE:
		params.hwnd=hwnd;
		params.cyChar=HIWORD(GetDialogBaseUnits());
		_beginthread(Thread2,0,¶ms);
		return 0;

	case WM_SIZE:
		params.cyClient=HIWORD(lParam);
		return 0;

	case WM_DESTROY:
		params.bKill=TRUE;
		return 0;
	}
	return DefWindowProc(hwnd,uMsg,wParam,lParam);
}

//the third window
VOID Thread3(PVOID pvoid)
{
	HDC hdc;
	int iNum=0,iNext=1,iLine=0,iTemp;
	PPARAMS pparams;
	TCHAR szBuffer[20];

	pparams=(PPARAMS)pvoid;

	while(!pparams->bKill)
	{
		if(iNum<0)
		{
			iNum=0;
			iNext=1;
		}
		iLine=CheckBottom(pparams->hwnd,pparams->cyClient,pparams->cyChar,iLine);

		hdc=GetDC(pparams->hwnd);

		TextOut(hdc,0,iLine*pparams->cyChar,szBuffer,wsprintf(szBuffer,TEXT("%d"),iNum));
		ReleaseDC(pparams->hwnd,hdc);
		iTemp=iNum;
		iNum=iNext;
		iNext+=iTemp;
		iLine++;
	}
	_endthread();
}

LRESULT CALLBACK WindowProc3(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	static PARAMS params;

	switch(uMsg)
	{
	case WM_CREATE:
		params.hwnd=hwnd;
		params.cyChar=HIWORD(GetDialogBaseUnits());
		_beginthread(Thread3,0,¶ms);
		return 0;

	case WM_SIZE:
		params.cyClient=HIWORD(lParam);
		return 0;

	case WM_DESTROY:
		params.bKill=TRUE;
		return 0;
	}
	return DefWindowProc(hwnd,uMsg,wParam,lParam);
}

//the forth window
VOID Thread4(PVOID pvoid)
{
	HDC hdc;
	int iDiameter;
	PPARAMS pparams;
	pparams=(PPARAMS)pvoid;

	while(!pparams->bKill)
	{
		InvalidateRect(pparams->hwnd,NULL,TRUE);
		UpdateWindow(pparams->hwnd);

		iDiameter=rand()%(max(1,min(pparams->cxClient,pparams->cyClient)));

		hdc=GetDC(pparams->hwnd);
		Ellipse(hdc,(pparams->cxClient-iDiameter)/2,(pparams->cyClient-iDiameter)/2,
			(pparams->cxClient+iDiameter)/2,(pparams->cyClient+iDiameter)/2);
		
		ReleaseDC(pparams->hwnd,hdc);



	}
	_endthread();
}

LRESULT CALLBACK WindowProc4(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	static PARAMS params;
	switch(uMsg)
	{
	case WM_CREATE:
		params.hwnd=hwnd;
		params.cyChar=HIWORD(GetDialogBaseUnits());
		_beginthread(Thread4,0,¶ms);
		return 0;

	case WM_SIZE:
		params.cxClient=LOWORD(lParam);
		params.cyClient=HIWORD(lParam);
		return 0;

	case WM_DESTROY:
		params.bKill=TRUE;
		return 0;
	}

	return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
LRESULT CALLBACK WindowProc(
							HWND hwnd,      // handle to window
							UINT uMsg,      // message identifier
							WPARAM wParam,  // first message parameter
							LPARAM lParam   // second message parameter
							)
{
	static HWND hwndChild[4];
	static TCHAR *szChildClass[]={TEXT("Child1"),TEXT("Child2"),TEXT("Child3"),TEXT("Child4")};
	static WNDPROC ChildProc[]={WindowProc1,WindowProc2,WindowProc3,WindowProc4};
	HINSTANCE hInstance;
	int i,cxClient,cyClient;
	WNDCLASS wndclass;

	switch(uMsg)
	{
	case WM_CREATE:
		hInstance=(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE);
		wndclass.cbClsExtra=0;
		wndclass.cbWndExtra=0;
		wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
		wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
		wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
		wndclass.hInstance=hInstance;
		wndclass.lpszMenuName=NULL;
		wndclass.style=CS_HREDRAW|CS_VREDRAW;

		for(i=0;i<4;++i)
		{
			wndclass.lpfnWndProc=ChildProc[i];
			wndclass.lpszClassName=szChildClass[i];
			
			RegisterClass(&wndclass);

			hwndChild[i]=CreateWindow(szChildClass[i],NULL,WS_CHILDWINDOW|WS_BORDER|WS_VISIBLE,0,0,0,0,hwnd,(HMENU)i,hInstance,NULL);

		}
		return 0;

	case WM_SIZE:
		cxClient=LOWORD(lParam);
		cyClient=HIWORD(lParam);

		for(i=0;i<4;++i)
		{
			MoveWindow(hwndChild[i],(i%2)*cxClient/2,(i>1)*cyClient/2,cxClient/2,cyClient/2,TRUE);

			
		}
		return 0;

	case WM_CHAR:
		if(wParam=='x1B')
			DestroyWindow(hwnd);
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}

	return DefWindowProc(hwnd,uMsg,wParam,lParam);
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics