3

我创建了一个自定义消息类型,用于调整我的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消息仅在窗口首次打开时在消息队列中找到一次PeekMessage(..),之后我的. 我真的不确定为什么会这样。但是,它WindowProc(..)方法接收(这对我没有帮助)。我将不胜感激你们可以提供的任何帮助。

谢谢

贾勒特

4

2 回答 2

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

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

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

于 2011-06-03T04:29:36.540 回答
0

您正在显示的消息泵循环将在队列为空后立即退出。如果再次输入,我无法从您发布的内容中判断。

如果这是你的主要消息泵,你应该使用GetMessage()代替,因为它会等到有东西可用后再返回。查看此 MSDN 文章以获取更多信息。

于 2011-06-03T04:28:54.970 回答