Click to See Complete Forum and Search --> : About writing a window application manually...


errand
June 8th, 2005, 10:30 AM
err.. hi everyone, i'm a newbie in Visual C++. I have this tutorial from the internet about writing a window application using API and handling their messages through stuff like WndProc. I follow every single instruction on the tutorial from Initializing window class, registering the class, create the window, until handling messages. After i compiled and executed the program, when i closed the window, the process won't quit and still remains in memory, so i had to open Task Manager to stop it. I know, it seems that i forgot to call PostQuitMessage(0) in the WM_DESTROY message, but, i didn't. I do exactly what the tutorial says. The funny thing is, when i copy the code from the tutorial to my IDE and then compile and execute it, everything works just fine. Can anyone give me suggestions to my problem ?

golanshahar
June 8th, 2005, 11:06 AM
first why you are not using MFC to create your app ( just for curiosity )?
secondly can you send your project ? so i will take a look into it?

Cheers

errand
June 9th, 2005, 06:07 AM
I do use MFC, but since i've been working with API's in Visual Basic, i kinda interested in writing my own program fully with API's in C++, just to improve my skills...hehe. Besides that, i also want to learn the 'behind the scenes' of what process is actually happened in a Windows program and how do we maintained it. I think, it's a good start for me...

About the project, which one would you like to see ? The one that i wrote or the one that i copied and pasted. Actually this two project is as same as the other. Coz, when the problem araised, i just moved every code from the Web Browser to the same Project and move the older codes.

And, FYI, this program is a simple program that shows how to make a window manually, and process only left and right mouse messages.

Thanks

SuperKoko
June 9th, 2005, 06:41 AM
You can attach the program you wrote, so we can see where is the problem.

Are you sure to treat the WM_SYSCOMMAND message when wParam=SC_CLOSE?
Are you sure to treat the WM_CLOSE message, by calling DestroyWindow.

I you used a switch statement, be sure that every case is correctly exited with a break statement or a return statement.
It is really a big source of bugs!

In your message loop, be sure to stop the loop and exit the process or return from WinMain, when the WM_QUIT message is found in the message queue.
In that case the GetMessage function returns 0.

errand
June 10th, 2005, 08:16 AM
Here's the code from the tutorial, this is the exact code that i used with my VC++. I think my program doesn't process any messages that you mentioned golanshahar, instead it sends any other messages to the Default message handler. Thanks...

#include <windows.h>

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof (wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_WINLOGO);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "Window Class 1";
wndclass.hIconSm = LoadIcon (NULL, IDI_WINLOGO);

RegisterClassEx (&wndclass);
hwnd = CreateWindow ("Window Class 1",
"My First Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow (hwnd, iCmdShow);
UpdateWindow (hwnd);
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}

UnregisterClass("Window Class 1",hInstance);

return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch (iMsg) {
case WM_LBUTTONDOWN:
MessageBox(hwnd,"You clicked the left mouse button!","First Window",MB_OK);
break;

case WM_RBUTTONDOWN:
MessageBox(hwnd,"You clicked the right mouse button!", "First Window",MB_OK);
break;

case WM_KEYDOWN:
MessageBox(hwnd,"You pressed a key!","First Window",MB_OK);
break;

case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam);

}

NoHero
June 10th, 2005, 11:24 AM
The closing of your program slightly depends on your main window loop. If you specify a valid window handle to GetMessage() the function will abort when the given handle - which must belong to the calling thread - gets closed. If you pass NULL to GetMessage() the only way to abort the main message loop is PostQuitMessage().

This is the way it supposed to be.

/And please use CodeTags when posting code. See my signature. :wave:

+J_o_S_H
June 10th, 2005, 12:21 PM
try adding this code right before case WM_DESTROY:


case WM_CLOSE:
DestroyWindow(hwnd);
break;

SuperKoko
June 11th, 2005, 04:56 AM
Your code works with my computer.
I suggest that you use a debugger, and put a breakpoint on the PostQuitMessage(0); line in the WM_DESTROY message processing, and another breakpoint on the UnregisterClass("Window Class 1",hInstance);.
With, that you can see where there is a problem.

Note that you are not supposed to call DefWindowProc for messages you process.
Instead, you must return zero for the messages you process.
So, replace the break; statements by return 0; statements.

to +J_o_S_H:
I initially thought, like you that the DefWindowProc don't treat the WM_CLOSE message, but it does process this message!
The confusion comes from the DefDlgProc, that don't treat these message!

errand
June 11th, 2005, 11:38 AM
Actually, when i copy the code and pasted it in the editor, it also works. The problems araise only if i write it in the editor by myself.

Well anyway, thanks for all the helps guys, i very appreciate it, from now on i will make use my debugger and be more thorough... hehe...

And please accept my apologize for not being neat with the code that i posted..

-Thanks-

NoHero
June 11th, 2005, 11:42 AM
Just post your Code with using Code Tags and then we will take a look at it ;)