主题:为什么程序无法终止?
这个程序出现了2个问题:
1.程序运行后关闭,虽然窗口没有了,但是程序实际上却没有终止,任务管理器上可以看到。
2.若在回调函数的WM_PAINT消息处 加上 MessageBox()函数,就会在运行时出现类似死循环的情况,程序无法退出。
3.随便问一下,不知道为什么VC设置不能保存了。设置以后只要一关闭当前工作区再次打开就要重新设置。我用的是VC6.0.。
上述问题如有知道的请给予笿复,谢谢!
以下是程序的完整代码:
#include "windows.h"
LRESULT CALLBACK WinProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
MSG msg;
WNDCLASS wndclass;
wndclass.style =CS_VREDRAW | CS_HREDRAW;
wndclass.lpfnWndProc = WinProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon = LoadCursor(NULL,0);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszClassName = "Let";
wndclass.lpszMenuName = NULL;
RegisterClass(&wndclass);
HWND hwnd;
hwnd = CreateWindow("Let",
"This is test!",
WS_OVERLAPPEDWINDOW,
0,0,
600,400,
NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWDEFAULT);
UpdateWindow(hwnd);
while(GetMessage(&msg,hwnd,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam ;
}
//回调函数
LRESULT CALLBACK WinProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
HDC hdc;
PAINTSTRUCT ps;
switch(uMsg)
{
case WM_PAINT:
//MessageBox(hwnd,"","",MB_OK);
hdc = BeginPaint(hwnd,&ps);
TextOut(hdc,5,50,"Hello,Welcome to windows!",strlen("Hello,welcome to windows!"));
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc (hwnd,uMsg,wParam,lParam);
}
return 0;
}
1.程序运行后关闭,虽然窗口没有了,但是程序实际上却没有终止,任务管理器上可以看到。
2.若在回调函数的WM_PAINT消息处 加上 MessageBox()函数,就会在运行时出现类似死循环的情况,程序无法退出。
3.随便问一下,不知道为什么VC设置不能保存了。设置以后只要一关闭当前工作区再次打开就要重新设置。我用的是VC6.0.。
上述问题如有知道的请给予笿复,谢谢!
以下是程序的完整代码:
#include "windows.h"
LRESULT CALLBACK WinProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
MSG msg;
WNDCLASS wndclass;
wndclass.style =CS_VREDRAW | CS_HREDRAW;
wndclass.lpfnWndProc = WinProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon = LoadCursor(NULL,0);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszClassName = "Let";
wndclass.lpszMenuName = NULL;
RegisterClass(&wndclass);
HWND hwnd;
hwnd = CreateWindow("Let",
"This is test!",
WS_OVERLAPPEDWINDOW,
0,0,
600,400,
NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWDEFAULT);
UpdateWindow(hwnd);
while(GetMessage(&msg,hwnd,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam ;
}
//回调函数
LRESULT CALLBACK WinProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
HDC hdc;
PAINTSTRUCT ps;
switch(uMsg)
{
case WM_PAINT:
//MessageBox(hwnd,"","",MB_OK);
hdc = BeginPaint(hwnd,&ps);
TextOut(hdc,5,50,"Hello,Welcome to windows!",strlen("Hello,welcome to windows!"));
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc (hwnd,uMsg,wParam,lParam);
}
return 0;
}