PeekMessage not getting the message?

不问归期 提交于 2019-12-23 21:08:20

问题


I've created a custom message type for use in resizing my Window, called WM_NEED_RESIZE. I've defined it in my .h file, and initialized in my .cpp file. I have also registered my WindowProc function to accept messages. Here is the code for these items:

const uint32 WindowsGLWindow::WM_NEED_RESIZE = WM_USER + 100;
LONG WINAPI WindowsGLWindow::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static PAINTSTRUCT ps;// do I need this?
    static sint32 newWidth = 0;
    static sint32 newHeight = 0;
    bool res = false;

    switch (uMsg) {
        case WM_PAINT:
            //display();
            BeginPaint(hWnd, &ps);
            EndPaint(hWnd, &ps);
            return 0;

        case WM_SIZE:
            //glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
            res = PostMessage(hWnd, WindowsGLWindow::WM_NEED_RESIZE, wParam,     lParam);
            std::cout << "WM_SIZE: " << res << std::endl;
            return 0;

        case WindowsGLWindow::WM_NEED_RESIZE:
            std::cout << "WindowsGLWindow::WM_NEED_RESIZE" << std::endl;
            break;

        case WM_CHAR:
            switch (wParam) {
                case 27: /* ESC key */
                    PostQuitMessage(0);
                    break;
            }
            return 0;

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

    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

In another function I am running through PeekMessage(..) to collect all messages. Here is the snippet of the message pump:

    MSG msg;
    while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE) == TRUE) // maybe use GetInputState(?)     as well?
    {
        if (msg.message == WM_QUIT)
            retVal = -1;

        if (msg.message == WindowsGLWindow::WM_NEED_RESIZE) {
            uint32 newWidth = LOWORD(msg.lParam);
            uint32 newHeight = HIWORD(msg.lParam);

            std::cout << "PeekMessage: WindowsGLWindow::WM_NEED_RESIZE" <<         std::endl;

            // call resize only if our window-size changed
            if ((newWidth != width_) || (newHeight != height_)) {
                resize(newWidth, newHeight);
            }

            PostMessage(msg.hwnd, WM_PAINT, 0, 0);
        }

        switch (msg.message) {
            case WM_MOUSEMOVE:
                // Retrieve mouse screen position
                //int x = (short) LOWORD(lParam);
                //int y = (short) HIWORD(lParam);

                // Check to see if the left button is held down:
                //bool leftButtonDown = wParam & MK_LBUTTON;

                // Check if right button down:
                //bool rightButtonDown = wParam & MK_RBUTTON;
                break;
            case WM_LBUTTONDOWN:
            case WM_RBUTTONDOWN:
            case WM_LBUTTONUP:
            case WM_RBUTTONUP:
            case WM_KEYUP:
            case WM_KEYDOWN:
                /*
                switch (msg.wParam) {
                    case 'W':
                        // w key pressed
                        break;
                    case VK_RIGHT:
                        // Right arrow pressed
                        break;
                    default:
                        break;
                }
                */
                break;
        }

        TranslateMessage(&msg);
        DispatchMessage(&msg);

    }

My problem is that the WM_NEED_RESIZE message is only found once in the message queue when the window first opens, after which it is never found in the message queue by my PeekMessage(..). I'm really not sure why this is happening. It is, however, being received by the WindowProc(..) method (which doesn't really help me). I would appreciate any help you guys could provide.

Thanks

Jarrett


回答1:


  1. Dont use std::cout expecting to see that output in your debugger, insted use OutputDebugString(); .

  2. You need to pass your class pointer to the last parameter of your call to CreateWindowEx, then retrieve that pointer from the LPCREATESTRUCT passed to you in the LPARAM of WM_CREATE, your class pointer will be in the lpCreateParmas feild of the struct. Set your clas pointer to the GWLP_USERDATA of your window, and on any other message calls , call GetWindowsLong , retrieve your class pointer, then pass the message, wparam, and lparam all off to your internal class message handler.

http://msdn.microsoft.com/en-us/library/ff381400%28v=VS.85%29.aspx




回答2:


The message pump loop that you are showing will exit as soon as the queue is empty. I can't tell from what you've posted if it ever gets entered again.

If this is your main message pump, you should use GetMessage() instead, as it will wait until something is available before returning. Take a look at this MSDN article for more info.



来源:https://stackoverflow.com/questions/6222635/peekmessage-not-getting-the-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!