如何强制SetWindowPos生成WM_SIZE消息
How to force SetWindowPos to generate WM_SIZE message
我发现如果新的大小与以前相同,SetWindowPos(...hwnd...)
函数将不会生成WM_SIZE消息。但有时我需要SetWindowPos来生成WM_SIZE消息,即使大小没有改变。虽然我可以在SetWindowPos
之后使用SendMessage(hwnd, WM_SIZE...)
,但这是一个糟糕的解决方案,因为它可能会生成两次WM_SIZE。
Is any flag for SetWindowPos or any similar functions so a WM_SIZE message is always generated?
我有一个主窗口hwndMain,一个子窗口hwndChild和hwndChild的子窗口hwndChild2。当hwndMain被调整大小时,它接收WM_SIZE,并且有一个MainOnSize(…)函数来调整hwndChild的大小。类似地,如果hwndChild被调整大小,它接收WM_SIZE,并且有一个childsize(…)函数来调整hwndChild2的大小。
但它来的情况:hwndMain和hwndChild被创建后,但不hwndChild2, hwndMain收到WM_SIZE,所以hwndMain和hwndChild被调整大小。现在hwndChild2创建后,hwndMain仍然收到一个WM_SIZE(见备注),所以它调用MainOnSize来调整hwndChild的大小。但是hwndChild的大小没有改变,所以它不会生成WM_SIZE,也不会调用ChildOnsize(),因此hwndChild2不会调整大小。
备注:
hwndMain第一次接收WM_SIZE是在创建WM_SIZE的时候,第二次是在使用ShowWindow(…)显示WM_SIZE的时候。
您可以使用EqualRect
API来确定SetWindowPos
函数是否会生成WM_SIZE
。
bool Window::resize(int x, int y, int width, int height, bool repaint)
{
UINT flags = SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOACTIVATE;
if (!repaint)
flags |= SWP_NOREDRAW;
RECT requested {
.left = x,
.top = y,
.right = width,
.bottom = height
};
RECT actual;
GetWindowRect(hWnd, &actual);
const bool IsSame = EqualRect(&requested, &actual) != FALSE;
const bool status = SetWindowPos(mhWnd, HWND_TOP, x, y, width, height, flags) != FALSE;
if (IsSame)
SendMessageW(hWnd, WM_SIZE, ...)
return status;
}
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 在线编译器中的分段C++没有打印消息
- C++错误消息*成员参考.**初学者*
- 大于65535的C++数组[size]引发不一致的溢出
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 为什么(-1)%vector::size()总是返回0
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何通过参数抛出错误消息
- 从服务器传输到客户端的消息不会出现
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- ROS2 动态消息模板
- C++秘密消息学校作业
- glad 导致 glfwSwapBuffers 返回错误消息
- C++入门 5 版:类消息和文件夹
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 如何处理从一个对象传递到另一个在C++中具有公共抽象类的对象的消息
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 为什么这个 std::queue/指向结构的指针列表直到 List.Size() == 0 才释放内存?