虽然点不多,只给一点表示谢意.但问题对我来说很难!研究了一天也没有结果,看下面的正题吧

jxw1987628 2007-10-09 10:30:18
//先给出异常的地方 这里会弹出一个错误 :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;
}
...全文
116 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jxw1987628 2007-10-11
  • 打赏
  • 举报
回复
楼上的 你好! 按你的我编译的时候去报如下错误的:

D:\Visual Studio 6.0\MSDev98\MyProjects\UIMutiThread\UIMutiThread.cpp(29) : error C2065: 'WndProc' : undeclared identifier
D:\Visual Studio 6.0\MSDev98\MyProjects\UIMutiThread\UIMutiThread.cpp(64) : error C2373: 'WndProc' : redefinition; different type modifiers
jxw1987628 2007-10-11
  • 打赏
  • 举报
回复
因篇幅有限 子窗口4对应的线程4与其他线程代码一样,所以没有帖,代码编译连接均正常,但有一个Warnong:WndProc not Initilize..............什么的.

还有就是如贴一所示的异常,只到今天还未找出错在哪里?谢谢楼上的帮我研究.因为我没有分了.....


真真喜欢研究程序的人,要不要分无所谓吧?至少楼上的做的很好!先说声谢谢了!===
jxw1987628 2007-10-11
  • 打赏
  • 举报
回复
// 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]; //全局线程句柄数组
///////////////////////////////////////////////////////////////////////////
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)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;


hwnd=CreateWindow(szAppName,"MultiPle UI Thread",WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);



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 APIENTRY WndProc(HWND,UINT,WPARAM,LPARAM);//函数原形声明

WndProc *WndProc1 = NULL,*WndProc2 = NULL,*WndProc3=NULL,*WndProc4=NULL;//函数指针
static char *szChildClass[]={"Child1","Child2","Child3","Child4"};//子窗口类名数组
static HWND hwndChild[4];//字窗口句柄数组
HINSTANCE hInstance;

static 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);
}
LRESULT APIENTRY WndProc2(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)//子窗口2消息处理函数
{
static PARAMS params;
switch(iMsg)
{
case WM_CREATE:
params.hwnd=hwnd;
params.cyChar=HIWORD(GetDialogBaseUnits());
hThread[1]=CreateThread(NULL,0,Thread2,¶ms,0,NULL);//启动线程2
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);
}

LRESULT APIENTRY WndProc3(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)//子窗口3消息处理函数
{
static PARAMS params;
switch(iMsg)
{
case WM_CREATE:
params.hwnd=hwnd;
params.cyChar=HIWORD(GetDialogBaseUnits());
hThread[2]=CreateThread(NULL,0,Thread3,¶ms,0,NULL);//启动线程3
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);
}
LRESULT APIENTRY WndProc4(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)//子窗口4消息处理函数
{
static PARAMS params;
switch(iMsg)
{
case WM_CREATE:
params.hwnd=hwnd;
params.cyChar=HIWORD(GetDialogBaseUnits());
hThread[3]=CreateThread(NULL,0,Thread4,¶ms,0,NULL);//启动线程4
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;
}
unsigned long __stdcall Thread2(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;
}

chehw 2007-10-11
  • 打赏
  • 举报
回复
重贴一下代码,是不是你改错地方了

chehw 2007-10-10
  • 打赏
  • 举报
回复
在WinMain中注释掉 WNDPROC WndProc; //多余的声明
jxw1987628 2007-10-10
  • 打赏
  • 举报
回复
为什么没有人回复啊,郁闷!

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧