|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
win32 window process keeps running
When I close it, the window closes, but the process keeps running and I have to end task it:
Code:
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
//#include <gl/gl.h>
//#include <gl/glu.h>
//#include "MyGL.h"
char szApp[] = "First OpenGL ****";
char szClassname[] = "FirstOpenGL";
char szFilename[MAX_PATH];
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if(uMsg==WM_DESTROY)
{
PostQuitMessage(0);
return 0;
}
else if(uMsg==WM_CREATE)
{
//
}
else if(uMsg==WM_SIZE)
{ /*
int height, width;
height=HIWORD(lParam);
width = LOWORD(lParam);
if(height==0)
{
height=1;
} */
/*
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
*/
}
else
return DefWindowProc(hwnd,uMsg,wParam,lParam);
return 0;
}
WPARAM WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR CmdLine, int CmdShow)
{
WNDCLASSEX wc;
MSG msg;
HWND hwnd;
/*
OpenGL specific
*/
HDC hDc=NULL;
HGLRC hRc=NULL;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance = hInst;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = szClassname;
wc.lpszMenuName = NULL;
wc.style = CS_OWNDC;
if(!RegisterClassEx(&wc))
{
MessageBox(NULL,"Son... an error occured at RegisterClassEx()",szApp,MB_OK);
return 0;
}
hwnd = CreateWindowEx(
0,
szClassname,
szApp,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
500,
NULL,
NULL,
hInst,
NULL);
if(hwnd==NULL)
{
MessageBox(NULL,"Son... an error occured at CreateWindowEx()",szApp,MB_OK);
return 0;
}
ShowWindow(hwnd,CmdShow);
UpdateWindow(hwnd);
while(1)
{
if(PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE))
if(msg.message == WM_QUIT)
break;
else
{
//RenderScene();
//SwapBuffers();
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
What's wrong with this? It worked until I changed GetMessage to PeakMessage |
|
#2
|
||||
|
||||
|
Re: win32 window process keeps running
I guess I have no answer to your question, except some notes on your program:
I'd like to know where you got the code for your message loop from: Code:
while(1)
{
if(PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE))
if(msg.message == WM_QUIT)
break;
else
{
//RenderScene();
//SwapBuffers();
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
because that is not normal. This is what the standard message loop looks like: Code:
BOOL bRet;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
even though most people do NOT check for the possibility of an error from GetMessage, so they incorrectly use this: Code:
while( (GetMessage( &msg, NULL, 0, 0 )) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
which is ok, if you want a version of your program that doesn't bother checking for errors to make things simplier to understand. Notice how the look ends without a call to break; This is because GetMessage() returns 0 (false) when it retrieves the WM_QUIT message. Although... PeekMessage() always retrieves WM_QUIT messages, so there seems like there is nothing wrong with your code... Another note: You could use a switch statement in the WndProc() function, instead of all those if ... else if ... else if.
__________________
Jason Doucette / Xona.com™ - Programming Windows Errata Addendum "Discussion is an exchange of knowledge; argument is an exchange of ignorance." Last edited by Jason Doucette : July 16th, 2003 at 08:28 PM. |
|
#3
|
||||
|
||||
|
I got it from a directX book, getMessge() is too slow for games, so it uses PeekMessage().
I commented out the code below and changed it to a standard messagebox, and it doesn't hang on exit.. So the problem is with the msgloop, what is wrong with it though? In my WndProc, I only handle WM_DESTROY as PostQuitMessage(0). Code:
/*
while(1)
{
if(PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE))
if(msg.message == WM_QUIT)
break;
else
{
//RenderScene();
//SwapBuffers();
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
*/
while( (GetMessage( &msg, NULL, 0, 0 ))>0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Last edited by movEAX_444 : July 16th, 2003 at 08:32 PM. |
|
#4
|
||||
|
||||
|
The only difference I can see between the two is that PeekMessage is documented as follows:
"During this call, the system delivers pending messages that were sent to windows owned by the calling thread using the SendMessage, SendMessageCallback, SendMessageTimeout, or SendNotifyMessage function. The system may also process internal events. Messages are processed in the following order: Sent messages Posted messages Input (hardware) messages and system internal events Sent messages (again) WM_PAINT messages WM_TIMER messages" I cannot test the code at the moment, so I am sorry that I cannot help you much. Perhaps the above has something to do with your problem. |
|
#5
|
||||
|
||||
|
I just fixed it, here is how it looks now:
Code:
while(1)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
//RenderScene();
//SwapBuffers();
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
|
|
#6
|
||||
|
||||
|
Aha. Good job! I did not notice that you were calling it with the window handle before. I didn't think to compare the arguments with arguments in GetMessage() to see what the differences / similarities are.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > win32 window process keeps running |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|