1

While having finished making a wrapper for my windows class, I did a test with some text to make sure everything was working okay. However, no matter if I delete or comment out the text "This is a test!!!!", when running the program, it still remains there during the executable run.

LRESULT CALLBACK WinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg)
        {
        case WM_PAINT:
            PAINTSTRUCT ps;
            HDC hdc;

            //hdc = BeginPaint(hwnd, &ps); 

            //TextOut(hdc, 0, 0, L"This is a TEST!!!", 17);

            //EndPaint(hwnd, &ps);
            break;
        case WM_DESTROY:
            bWindowClosed = TRUE;
            break;
        case WM_CREATE:
            MessageBox(NULL, L"Create", L"test", MB_OK);
            break;
        default:
            return CBaseWindow::WinMsgHandler(hwnd, uMsg, wParam, lParam);
        }

        return 0;
    };

EDIT:

Here is the winmain source file. I have a feeling it has something to do with the way I bracketed everything. CDerivedWindow is a wrapper class for encapsulating most of the window initialization process.

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{

    CDerivedWindow mainWnd(hInstance);

    WNDCLASSEX wcx; 

    // Fill in the window class structure with default parameters 
    wcx.cbSize = sizeof(WNDCLASSEX);                            // size of structure 
    wcx.style = CS_HREDRAW | CS_VREDRAW;                        // redraw if size changes 
    wcx.lpfnWndProc = CBaseWindow::stWinMsgHandler;             // points to window procedure 
    wcx.cbClsExtra = 0;                                         // no extra class memory 
    wcx.cbWndExtra = 0;                                         // no extra window memory 
    wcx.hInstance = hInstance;                                  // handle to instance 
    wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);                // predefined app. icon 
    wcx.hCursor = LoadCursor(NULL, IDC_ARROW);                  // predefined arrow 
    wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);    // white background brush 
    wcx.lpszMenuName = NULL;                                    // name of menu resource 
    wcx.lpszClassName = L"True Wild";                           // name of window class 
    wcx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);              // small class icon 

    initSprites();

    // register the window
    if (mainWnd.RegisterWindow(&wcx))
    {
        DWORD dwError = 0;
        DWORD dwStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
        RECT rc;

        rc.top = 100;
        rc.left = 100;
        rc.right = SCREEN_WIDTH;
        rc.bottom = SCREEN_HEIGHT;

        // create the window and start the message loop
        // we will get kicked out of the message loop when the window closes
        if (mainWnd.Create(dwStyle, &rc))
        {
            // message loop
            MSG msg;


            //game Loop
            while (TRUE)
            {
                while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
                { 
                    // Translate the message and dispatch it to WindowProc()
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);

                    if (mainWnd.IsWindowClosed())
                        return 0;

                }


                //Run game code
                render();
            }
            return 0;
        }
        else
            return -1;
    }
    else
        return -2;

    return 0;
}

EDIT for Answer 1:

// the message handler for this window
LRESULT CALLBACK WinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        bWindowClosed = TRUE;
        break;
    default:
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

    return DefWindowProc(hwnd, uMsg, wParam, lParam);
};
Cdore
  • 107
  • 1
  • 11

1 Answers1

0

If you don't handle WM_PAINT, you should pass it to DefWindowProc() to validate the client area and repaint the window.

It looks like you are breaking out of the switch/case and returning 0. I would replace:

return 0;

with

return DefWindowProc(hwnd, uMsg, wParam, lParam);
user987280
  • 1,578
  • 1
  • 14
  • 24
  • That is already done through `default: return CBaseWindow::WinMsgHandler(hwnd, uMsg,wParam, lParam);`, which calls the base class's function of `DefWindowProc()`. Unless you are telling me to call it in the `PAINT` case, too? – Cdore Nov 24 '12 at 03:01
  • Ah, I cannot believe I didn't fix that. But alas, still the same issue. I might have to put making a wrapper class on hold so I can continue with my actual project. – Cdore Nov 24 '12 at 03:07
  • 1
    It looks like you are breaking out of the switch/case on a WM_PAINT message and the default case is never reached. Also, whenever you declare a variable within a case, you want to surround the entire case with curly brackets: http://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement – user987280 Nov 24 '12 at 03:07
  • Yeah, I fixed that up. I sometimes forget the rules of switch cases, so I should make it a habit to always throw those on there. It didn't fix it, however, even when arranging the order. It's just one of those issues that involves something being out of order. Windows are strict to deal with to begin with. – Cdore Nov 24 '12 at 03:12
  • One other odd thing that stands out is the way you handle WM_DESTROY. Usually it's handled by calling PostQuitMessage(0); so that a WM_QUIT message is inserted in your message queue. I don't know what's happening with `bWindowClosed` but you probably know that already. I was just thinking that maybe the window was never destroyed properly, because WM_DESTROY is sent after the window is removed from the screen but before it's destroyed. – user987280 Nov 24 '12 at 03:27
  • The way it works is that `bWindowClosed` is a variable in the base wrapper window class. The function is `BOOL IsWindowClosed() { return bWindowClosed`. As you see, in the message loop, `if (mainWnd.IsWindowClosed()) break;` is called to check if that variable was set. If so, it closes the program. As you expected, `PostQuitMessage` is usually the norm here. – Cdore Nov 24 '12 at 03:55
  • Post the edited code with the changes so we can have another look. I'm curious now :) – user987280 Nov 24 '12 at 03:59
  • I posted the edit to that switch statement in my post. My code is pretty messy at this point, so I'm likely going to start fresh some other time. – Cdore Nov 24 '12 at 04:06
  • Yeah, I don't know anything about `CBaseWindow` so I'm not sure about `wcx.lpfnWndProc = CBaseWindow::stWinMsgHandler;`. Usually I just define the WndProc as a static function in my App class. I would double check that your WndProc is even receiving messages. If so, I would have it return DefWindowProc() for everything and see if the issue is still there. At least you could then rule out that out as a possibility and look elsewhere. – user987280 Nov 24 '12 at 04:18