源程序如下(虽然关闭了程序,但进程里仍然存在,且占用内存99%,哪位大虾帮找一下问题啊):
#include <windows.h>
HDC mdc;
HBITMAP hbm;
void MyPaint(HDC hdc)
{
BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);
}LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to windo 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
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor=LoadCursor(hInstance,IDC_APPSTARTING);
wndcls.hIcon=LoadIcon(hInstance,IDI_APPLICATION);
wndcls.lpfnWndProc=WindowProc;
wndcls.lpszClassName="dbnj";
wndcls.lpszMenuName=NULL;
wndcls.hInstance=hInstance;
wndcls.style=CS_HREDRAW|CS_VREDRAW; RegisterClass(&wndcls);
HWND hwnd;
hwnd=CreateWindow("dbnj","从文件调入位 图",WS_OVERLAPPEDWINDOW,100,100,640,480,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);
HDC hdc;
hdc=GetDC(hwnd);
mdc=CreateCompatibleDC(hdc);
hbm=(HBITMAP)LoadImage(NULL,"12.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
SelectObject(mdc,hbm); MyPaint(hdc);
ReleaseDC(hwnd,hdc);
MSG uMsg;
while (GetMessage(&uMsg,hwnd,0,0))
{
TranslateMessage(&uMsg);
DispatchMessage(&uMsg);
}
return 0;
}LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
PAINTSTRUCT ps;
HDC hdc;
switch (uMsg)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
MyPaint(hdc);
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
DeleteDC(mdc);
DeleteObject(hbm);
PostQuitMessage(0);
break;
case WM_CLOSE:
if (IDYES==MessageBox(hwnd,"确认关闭","关闭",MB_YESNO))
DestroyWindow(hwnd);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
#include <windows.h>
HDC mdc;
HBITMAP hbm;
void MyPaint(HDC hdc)
{
BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);
}LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to windo 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
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor=LoadCursor(hInstance,IDC_APPSTARTING);
wndcls.hIcon=LoadIcon(hInstance,IDI_APPLICATION);
wndcls.lpfnWndProc=WindowProc;
wndcls.lpszClassName="dbnj";
wndcls.lpszMenuName=NULL;
wndcls.hInstance=hInstance;
wndcls.style=CS_HREDRAW|CS_VREDRAW; RegisterClass(&wndcls);
HWND hwnd;
hwnd=CreateWindow("dbnj","从文件调入位 图",WS_OVERLAPPEDWINDOW,100,100,640,480,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);
HDC hdc;
hdc=GetDC(hwnd);
mdc=CreateCompatibleDC(hdc);
hbm=(HBITMAP)LoadImage(NULL,"12.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
SelectObject(mdc,hbm); MyPaint(hdc);
ReleaseDC(hwnd,hdc);
MSG uMsg;
while (GetMessage(&uMsg,hwnd,0,0))
{
TranslateMessage(&uMsg);
DispatchMessage(&uMsg);
}
return 0;
}LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
PAINTSTRUCT ps;
HDC hdc;
switch (uMsg)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
MyPaint(hdc);
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
DeleteDC(mdc);
DeleteObject(hbm);
PostQuitMessage(0);
break;
case WM_CLOSE:
if (IDYES==MessageBox(hwnd,"确认关闭","关闭",MB_YESNO))
DestroyWindow(hwnd);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
if (IDYES==MessageBox(hwnd,"确认关闭","关闭",MB_YESNO)) {
DestroyWindow(hwnd);
PostQuitMessage(0); // <<=====
}
break;
WM_CLOSE的时候还是会关闭窗口的,但是不会退出程序,因为没有调用PostQuitMessage,
你改成
case WM_CLOSE:
if (IDYES!=MessageBox(hwnd,"确认关闭","关闭",MB_YESNO))
{
return 0;
}
else
{
DestroyWindow(hwnd);
}
break;
试过了,不行啊。
回ralphvsclark(微笑)
程序运行后点击关闭时会调出确认关闭的对话框啊,说明IDYES分支是执行的。改后就不能关闭窗口了。请问:窗口关闭但程序没有退出的原因是不是调用了DestroyWindow(hwnd),而没有调用PostQuitMessage(0)?若是这样那就是PostQuitMessage(0)的问题了?
DeleteObject(hbm);把这两条删掉试一下,DestroyWindow本身应该已经做了吧
DeleteDC(mdc);
DeleteObject(hbm);
PostQuitMessage(0);
return 0;
最后一行,改成:return 0;
{
TranslateMessage(&uMsg);
DispatchMessage(&uMsg);
}
你的改法是正确的啊。
请问chehw(chehw):
我看了msdn上的解释,还不是太明白。你能解释一下吗,为什么应该用0或NULL,而不是hwnd。
PostQuitMessage发出的消息并不由框架窗口接收的,因此第二个参数需设为NULL