gpt4 book ai didi

c++ - PeekMessage 没有收到消息?

转载 作者:太空狗 更新时间:2023-10-29 21:50:22 25 4
gpt4 key购买 nike

我创建了一个自定义消息类型,用于调整我的 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(..) 方法接收(这对我没有真正帮助)。如果你们能提供任何帮助,我将不胜感激。

谢谢

贾勒特

最佳答案

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

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

    <

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

关于c++ - PeekMessage 没有收到消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6222635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com