Sign in to follow this  

ummm help with quiting a pretty basic program in C++???

This topic is 4836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok my problem is that when i quit agree to quit this application it quits the window but the application is still running when i check task manager so its not actually quitting its just hiding the window basically.........my codes #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <windowsx.h> #include <stdio.h> #include <conio.h> #include <winuser.h> int focus; PAINTSTRUCT ps; HDC hdc; HWND MainWindow; LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { switch(msg) { case WM_CREATE: return(0); break; case WM_ACTIVATE: if (LOWORD(wparam) == WA_INACTIVE) focus = FALSE; else focus = TRUE; break; case WM_PAINT: hdc = BeginPaint(MainWindow, &ps); EndPaint(MainWindow, &ps); break; case WM_CLOSE: if (MessageBox(MainWindow, "Are you sure you want to quit?", "Abandoner", MB_YESNO) == IDYES) return(DefWindowProc(hwnd, msg, wparam, lparam)); else return(0); break; } return(DefWindowProc(hwnd, msg, wparam, lparam)); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { WNDCLASSEX AdamsClass; MSG msg; HWND hwnd; AdamsClass.cbSize = sizeof(WNDCLASSEX); AdamsClass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; AdamsClass.cbClsExtra = 0; AdamsClass.cbWndExtra = 0; AdamsClass.lpszMenuName = NULL; AdamsClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); AdamsClass.hIcon = LoadIcon(NULL, "IDI_ICON1"); AdamsClass.hIconSm = LoadIcon(NULL, "IDI_ICON1"); AdamsClass.hCursor = LoadCursor(NULL, IDC_ARROW); AdamsClass.hInstance = hInstance; AdamsClass.lpszClassName = "AdamsClass"; AdamsClass.lpfnWndProc = MsgHandler; RegisterClassEx(&AdamsClass); if (!(hwnd = CreateWindowEx(NULL, "AdamsClass", "Adams Game", WS_POPUP | WS_VISIBLE, 0, 0, 1000, 1000 , NULL, NULL, hInstance, NULL))) return(0); while (TRUE) if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } ReleaseDC(hwnd,hdc); return(msg.wParam); }

Share this post


Link to post
Share on other sites
In your WM_CLOSE handler, instead of calling DefWindowProc, call PostQuitMessage(0) and then return 0.

Also, you're doing a RelaseDC after your while loop. When the while loop exits, your hWnd is no longer valid, so the RelaseDC won't work as expected. In addition, the hDC you are releasing is only valid between the BeginPaint/EndPaint calls and is released via the EndPaint() call, so the ReleaseDC isn't necessary.

Share this post


Link to post
Share on other sites
Quote:


if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}



I can't see why do you use the line "if (msg.message == WM_QUIT) break". It just prevents your application from translating this message (which is quite important to close your app).

Other thing, you should maybe use this code for WM_CLOSE message


case WM_CLOSE:
{
PostQuitMessage(0);
return 0;
}


Hope it helps! :)

Share this post


Link to post
Share on other sites
Quote:
Original post by clapton

I can't see why do you use the line "if (msg.message == WM_QUIT) break". It just prevents your application from translating this message (which is quite important to close your app).

Other thing, you should maybe use this code for WM_CLOSE message


case WM_CLOSE:
{
PostQuitMessage(0);
return 0;
}


Hope it helps! :)


Actually, if I remember correctly, the WM_QUIT is never passed to the window procedure and, by the time PeekMessage/GetMessage sees it, all the necessary preparations for closing the app have been completed. In fact, GetMessage returns 0 when it sees a WM_QUIT, so "normal" windows message loops never translate/dispatch a WM_QUIT.

Share this post


Link to post
Share on other sites

This topic is 4836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this