Skip to main content
GameDev.net gamedev.net
🔒 Locked

Hmm what's wrong?!

Started by ElNoNombreHombre Jan 5, 2002 at 11:53 PM 2 replies 450+ views
Original Post
ElNoNombreHombre
ElNoNombreHombre
I''m trying to create a program that simpily creates a window with a black background. However when the porgram''s window closes the program doesn''t quit. And, yes, I do know that simpily closing the window does NOT end the program. Sorry to have to do such a massive code dump, but i really can''t understand what''s going on here. All i know is that the window closes and the WM_DESTROY message is sent to WndProc, however something seems to be screwy with the PostQuitMessage(). I''m dumping the entire code inncase something in there is screwing it up.
  
//---------------------------------------------------------------------------

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#pragma hdrstop
//---------------------------------------------------------------------------

////////// declarations




//---------------------------------------------------------------------------

LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
        // create variables, static so that it doesn''t have to keep getting created

        static PAINTSTRUCT ps;
        static HDC hDC;
        // find out what message is

        switch(Message)
        {
                case WM_CREATE: // window created

                {
                        // one time initalization

                        return 0;
                        break;
                }
                case WM_PAINT:  // window needs redrawn.

                {
                        hDC = BeginPaint( hWnd, &ps);
                        // do any drawing with gdi


                        EndPaint(hWnd, &ps);
                        return 0;
                        break;
                }
                case WM_DESTROY:// window closed

                {
                        // main quindow is closing,

                        // app needs to exit


                        // tell winodws to put a quit

                        // message in the message que


                        PostQuitMessage(0);
                        return 0;
                        break;
                }

        }
        // let window handle message

        return DefWindowProc(hWnd, Message, wParam, lParam);
}
//---------------------------------------------------------------------------


#pragma argsused
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
        HWND hWnd;      // window handle

        MSG msg;        // message

        WNDCLASSEX wc;  // holds the window properties


        // create a name for the window and window caption

        const char* AppName= "Temp Anchor";

        // fill in the window class structure


        // size of class in bytes

        wc.cbSize       = sizeof(wc);
        // style of window

        wc.style        = (CS_HREDRAW | CS_VREDRAW | CS_OWNDC);
        // stupid stuff we don''t need

        wc.cbClsExtra   = 0;
        wc.cbWndExtra   = 0;
        // name of our event handler

        wc.lpfnWndProc  = WndProc;
        // handle to app''s instance

        wc.hInstance    = hInstance;
        // handle to brush to use for the window background

        wc.hbrBackground= (HBRUSH)GetStockObject(BLACK_BRUSH);
        // a handle to the icon to use for the window

        wc.hIcon        = LoadIcon(NULL, IDI_APPLICATION);
        // a handle to a smaller version of the app''s icon

        wc.hIconSm      = LoadIcon(NULL, IDI_APPLICATION);
        // handle to the cursor to use for the mouse pointer

        wc.hCursor      = LoadIcon(NULL, IDC_CROSS);
        // menu resource

        wc.lpszMenuName = NULL;
        // last but not least the name of the class

        wc.lpszClassName= AppName;

        // register the window

        RegisterClassEx(&wc);

        // create window.

        hWnd = CreateWindowEx(  NULL,                   // Extended window style

                                AppName,                // class name

                                AppName,                // winodw name

                                WS_OVERLAPPEDWINDOW,    // window stlye

                                0, 0,                   // x,y

                                300, 300,               // width, height

                                NULL,                   // parent handle

                                NULL,                   // handle to child

                                hInstance,              // app instance

                                NULL);                  // window creation data

        // show for first time

        ShowWindow(hWnd, nCmdShow);
        // update it to make sure everything is in order.

        UpdateWindow(hWnd);

        while(1) // do forever

        {
                if( PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE) )  // get message

                {
                        if(msg.message == WM_QUIT)              //if it''s time to quit...

                                break;                          // quit


                        // In case of weird messages

                        TranslateMessage(&msg);
                        // send to WndProc

                        DispatchMessage(&msg);

                }
                else                                            // if no message, continue game

                {
                        // game code here.

                }

        }
        return msg.wParam;
}
//---------------------------------------------------------------------------


  
and by the way, yes I AM using Borland C++Builder 5, and NO I WILL NOT get MSVC++ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ To all of those who think I''m just being contradictory: No I''m not!
To all of those who think I'm just being contradictory:No I'm not!My First Game!!!"
LyLFox
LyLFox
I don''t see anything wrong with the code. I''ve even copy and paste it on my compiler and it works just fine. When you click the X on the window, the PostQuitMessage gets sent and then the program quits. I don''t know why yours isn''t.
EvilCrap
EvilCrap
wwhat sorta comp are u running on? i know that windows can act funny on my laptop. try running on a different comp.
ElNoNombreHombre
ElNoNombreHombre
Thank you for reasuring me that I''m NOT insane. I guess I''ll try to recompile it.

...
...

Nope.

...
...
...
...

Hmm... this is strange... it may be a borland quirk or something, but changing the WM_DESTROY: to WM_CLOSE: made it work properly. Although i KNOW that the WM_DESTROY part was called (for lack of a better phrase) when the x button was pressed. Any of you winodws gurus have any idea as to why this is so?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To all of those who think I''m just being contradictory:

No I''m not!
To all of those who think I'm just being contradictory:No I'm not!My First Game!!!"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.