虽然点不多,只给一点表示谢意.但问题对我来说很难!研究了一天也没有结果,看下面的正题吧
//先给出异常的地方 这里会弹出一个错误 :UnHandled exception avocess..0xccccc 之类的窗口
try
{
hwnd=CreateWindow(szAppName,"MultiPleUIThread",WS_OVERLAPPEDWINDOW,0,0,400,600,NULL,NULL,hInstance,NULL);
set_terminate(mytest);
throw"##################!";
}
catch(...)
{
cout<<"error! here....."<<endl;
}
////////////////////////////////////////////////////////////////
//下面是所有源代码: 并附有详细注释!
程序主要做的是:
在主窗体中创建4个相同的子窗体,在创建每个子窗体时启动每个字窗体对应的线程,其窗口一样,线呈函数里的代码也一样
是纯Win32Application程序!
请先不要判定是创建窗体错误,那里单步调试的时候总会执行catch语句的...........
///////////////////////////////////////////////////////////////////
// UIMutiThread.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
LRESULT APIENTRY WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam);//窗口消息函数声明
///////////////////////////////////////////////////////////////////////////
HANDLE hThread[4]; //全局线程句柄数组
///////////////////////////////////////////////////////////////////////////
void mytest()
{}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
static char szAppName[]="MultiThread";
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
WNDPROC WndProc;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
int a=RegisterClass(&wndclass);
if(!a)
cout<<"error here!\n"<<GetLastError()<<endl;
try
{
hwnd=CreateWindow(szAppName,"MultiPle UI Thread",WS_OVERLAPPEDWINDOW,0,0,400,600,NULL,NULL,hInstance,NULL);
set_terminate(mytest);
throw"##################!";
}
catch(...)
{
cout<<"error! here....."<<endl;
}
if(hwnd==NULL)
cout<<"error here!\n"<<GetLastError()<<endl;
ShowWindow(hwnd,nCmdShow);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT APIENTRY WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)//主线程窗口消息处理函数
{
typedef LRESULT CALLBACK MyWndProc(HWND,UINT,WPARAM,LPARAM);//函数原形声明
MyWndProc *WndProc1 = NULL,*WndProc2 = NULL,*WndProc3=NULL,*WndProc4=NULL;//函数指针
static char *szChildClass[]={"Child1","Child2","Child3","Child4"};//子窗口类名数组
static HWND hwndChild[4];//字窗口句柄数组
HINSTANCE hInstance;
WNDPROC ChildProc[]={WndProc1,WndProc2,WndProc3,WndProc4};//函数指针数组
int i,cxClient,cyClient;//存储窗口尺寸变量
WNDCLASSEX wndclass;
switch(iMsg)
{
case WM_CREATE :
hInstance=(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE);
wndclass.cbSize=sizeof(wndclass);
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.cbClsExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=NULL;
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hIconSm=NULL;
wndclass.cbWndExtra=0;
wndclass.lpszMenuName=NULL;
for(i=0;i<4;i++)
{
wndclass.lpfnWndProc=ChildProc[i];
wndclass.lpszClassName=szChildClass[i];
RegisterClassEx(&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(wParam);
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=='q')
DestroyWindow(hwnd); //输入'q'时退出应用程序
return 0;
case WM_DESTROY:
PostQuitMessage(0);
for(int i=0;i<4;i++)
CloseHandle(hThread[i]);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
//自定义结构体传递参数
typedef struct
{
HWND hwnd;
int cxClient;
int cyClient;
int cyChar;
BOOL bKill;
}PARAMS,*PPARAMS;
//全局线程函数的声明
unsigned long __stdcall Thread1(PVOID pvoid);//子窗口一的线程函数,产生素数并输出
unsigned long __stdcall Thread2(PVOID pvoid);//其余和子窗口一相同
unsigned long __stdcall Thread3(PVOID pvoid);
unsigned long __stdcall Thread4(PVOID pvoid);
LRESULT APIENTRY WndProc1(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)//子窗口1消息处理函数
{
static PARAMS params;
switch(iMsg)
{
case WM_CREATE:
params.hwnd=hwnd;
params.cyChar=HIWORD(GetDialogBaseUnits());
hThread[0]=CreateThread(NULL,0,Thread1,¶ms,0,NULL);//启动线程1
SetThreadPriority(hThread[0],THREAD_PRIORITY_BELOW_NORMAL);
return 0;
case WM_SIZE:
params.cyClient=HIWORD(lParam);
return 0;
case WM_DESTROY:
params.bKill=true;
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
//自定义小函数检测字符有没有超过窗口高度
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;
}
unsigned long __stdcall Thread1(PVOID pvoid)//子窗口一的线程函数,产生素数并输出
{
char szBuffer[16];
int iNum=1,iLine=0,i,iSqrt;
PPARAMS pparams;
pparams=PPARAMS(pvoid);
while(!pparams->bKill)
{
do{
if(++iNum<0)
iNum=0;
iSqrt=(int)sqrt(iNum);
for(i=2;i<=iSqrt;i++)
if(iNum%i==0)
break;
}while(i<=iSqrt);
iLine=CheckBottom(pparams->hwnd,pparams->cyChar,pparams->cyClient,iLine);
wsprintf(szBuffer,"%d",iNum);
HDC hdc=GetDC(pparams->hwnd);
TextOut(hdc,0,iLine*pparams->cyChar,szBuffer,strlen(szBuffer));
ReleaseDC(pparams->hwnd,hdc);
iLine++;
}
return 0;
}