PeekMessage没有收到消息?

时间:2011-06-03 03:19:49

标签: c++ window messages postmessage peekmessage

我创建了一个自定义消息类型,用于调整Window的大小,称为WM_NEED_RESIZE。我已在我的.h文件中定义它,并在我的.cpp文件中初始化。我还注册了WindowProc函数来接受消息。以下是这些项目的代码:

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);
}

在另一个函数中,我正在运行PeekMessage(..)来收集所有邮件。以下是消息泵的片段:

    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);

    }

我的问题是,当窗口首次打开时,WM_NEED_RESIZE消息只能在消息队列中找到一次,之后我的{{1}永远不会在消息队列中找到它}}。我真的不确定为什么会这样。然而,它是 ,被PeekMessage(..)方法接收(这对我没有帮助)。我很感激你们能提供的任何帮助。

由于

Jarrett的

2 个答案:

答案 0 :(得分:2)

  1. 不要使用std :: cout期望在调试器中看到输出,insted使用OutputDebugString(); 。

  2. 你需要将你的类指针传递给你调用CreateWindowEx的最后一个参数,然后从WM_CREATE的LPARAM中传递给你的LPCREATESTRUCT中检索那个指针,你的类指针将位于lpCreateParmas中。结构。将clas指针设置为窗口的GWLP_USERDATA,并在任何其他消息调用上调用GetWindowsLong,检索类指针,然后将消息,wparam和lparam全部传递给内部类消息处理程序。

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

答案 1 :(得分:0)

您显示的消息泵循环将在队列为空时立即退出。如果它再次进入,我无法判断你发布了什么。

如果这是您的主要消息泵,则应使用GetMessage()代替,因为它会等到返回之前有可用的内容。请查看this MSDN article以获取更多信息。