当用户抓住一个可调整大小的窗口的一角,然后移动它时,窗口首先移动窗口的内容,然后向正在调整大小的窗口发出 WM_SIZE。
因此,在一个对话框中,我想控制各种子控件的移动,并且我想消除闪烁,用户首先会看到 windows 操作系统认为窗口会是什么样子(因为,AFAICT,操作系统使用 bitblt 方法来移动在发送 WM_SIZE 之前窗口内部的东西)——只有这样我的对话框才能处理移动它的子控件,或者调整它们的大小等,之后它必须强制重新绘制东西,这现在会导致闪烁(在非常至少)。
我的主要问题是:有没有办法强制 Windows 不做这个愚蠢的 bitblt 事情? 如果窗口的控件在调整窗口大小时移动,或者在调整其父级大小时调整自身大小,那么它肯定是错误的。无论哪种方式,让操作系统进行预涂漆只会让作品变得更糟。
我一度认为它可能与 CS_HREDRAW 和 CSVREDRAW 类标志有关。然而,现实情况是我不希望操作系统要求我擦除窗口 - 我只想在操作系统不先更改窗口内容的情况下自己重新绘制(即我希望显示是原来的样子在用户开始调整大小之前 - 没有来自操作系统的任何 bitblit'ing)。而且我不希望操作系统告诉每个控件它也需要重新绘制(除非它恰好是一个实际上被调整大小遮蔽或显示的控件。
我真正想要的:
- 在屏幕上更新任何内容之前移动和调整子控件的大小。
- 完全绘制所有移动或调整大小的子控件,以使它们在新的大小和位置上没有伪影。
- 在子控件之间绘制空格,而不影响子控件本身。
注意:步骤 2 和 3 可以颠倒。
当我将 DeferSetWindowPos() 与标记为 WS_CLIPCHILDREN 的对话框资源结合使用时,上述三件事似乎正确发生。
如果我可以对内存 DC 执行上述操作,然后只在 WM_SIZE 处理程序的末尾执行一个 bitblt,我将获得额外的小好处。
我已经玩了一段时间了,我无法逃避两件事:
我仍然无法阻止 Windows 执行“预测 bitblt”。 答:请参阅下面的解决方案,该解决方案覆盖 WM_NCCALCSIZE 以禁用此行为。
我看不出如何构建一个对话框,其中它的子控件绘制到双缓冲区。答案:请参阅下面约翰的答案(标记为答案),了解如何要求 Windows 操作系统双缓冲您的对话框(注意:根据文档,这不允许任何 GetDC() 中间的绘制操作)。
我的最终解决方案(感谢所有做出贡献的人,尤其是 John K.):
经过大量的汗水和泪水,我发现以下技术在 Aero 和 XP 中或在禁用 Aero 的情况下都能完美运行。不存在轻弹(1)。
- 挂钩对话过程。
- 覆盖 WM_NCCALCSIZE 以强制 Windows 验证整个客户区,而不是 bitblt 任何东西。
- 覆盖 WM_SIZE 以使用 BeginDeferWindowPos/DeferWindowPos/EndDeferWindowPos 对所有可见窗口执行所有移动和调整大小。
- 确保对话窗口具有 WS_CLIPCHILDREN 样式。
- 不要使用 CS_HREDRAW|CS_VREDRAW(对话框不使用,因此通常不是问题)。
布局代码由您决定 - 它很容易在布局管理器的 CodeGuru 或 CodeProject 上找到示例,或者自己滚动。
以下是一些代码摘录,可以帮助您了解大部分情况:
LRESULT ResizeManager::WinProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_ENTERSIZEMOVE:
m_bResizeOrMove = true;
break;
case WM_NCCALCSIZE:
// The WM_NCCALCSIZE idea was given to me by John Knoeller:
// see: http://stackoverflow.com/questions/2165759/how-do-i-force-windows-not-to-redraw-anything-in-my-dialog-when-the-user-is-resiz
//
// The default implementation is to simply return zero (0).
//
// The MSDN docs indicate that this causes Windows to automatically move all of the child controls to follow the client's origin
// and experience shows that it bitblts the window's contents before we get a WM_SIZE.
// Hence, our child controls have been moved, everything has been painted at its new position, then we get a WM_SIZE.
//
// Instead, we calculate the correct client rect for our new size or position, and simply tell windows to preserve this (don't repaint it)
// and then we execute a new layout of our child controls during the WM_SIZE handler, using DeferWindowPos to ensure that everything
// is moved, sized, and drawn in one go, minimizing any potential flicker (it has to be drawn once, over the top at its new layout, at a minimum).
//
// It is important to note that we must move all controls. We short-circuit the normal Windows logic that moves our child controls for us.
//
// Other notes:
// Simply zeroing out the source and destination client rectangles (rgrc[1] and rgrc[2]) simply causes Windows
// to invalidate the entire client area, exacerbating the flicker problem.
//
// If we return anything but zero (0), we absolutely must have set up rgrc[0] to be the correct client rect for the new size / location
// otherwise Windows sees our client rect as being equal to our proposed window rect, and from that point forward we're missing our non-client frame
// only override this if we're handling a resize or move (I am currently unaware of how to distinguish between them)
// though it may be adequate to test for wparam != 0, as we are
if (bool bCalcValidRects = wparam && m_bResizeOrMove)
{
NCCALCSIZE_PARAMS * nccs_params = (NCCALCSIZE_PARAMS *)lparam;
// ask the base implementation to compute the client coordinates from the window coordinates (destination rect)
m_ResizeHook.BaseProc(hwnd, msg, FALSE, (LPARAM)&nccs_params->rgrc[0]);
// make the source & target the same (don't bitblt anything)
// NOTE: we need the target to be the entire new client rectangle, because we want windows to perceive it as being valid (not in need of painting)
nccs_params->rgrc[1] = nccs_params->rgrc[2];
// we need to ensure that we tell windows to preserve the client area we specified
// if I read the docs correctly, then no bitblt should occur (at the very least, its a benign bitblt since it is from/to the same place)
return WVR_ALIGNLEFT|WVR_ALIGNTOP;
}
break;
case WM_SIZE:
ASSERT(m_bResizeOrMove);
Resize(hwnd, LOWORD(lparam), HIWORD(lparam));
break;
case WM_EXITSIZEMOVE:
m_bResizeOrMove = false;
break;
}
return m_ResizeHook.BaseProc(hwnd, msg, wparam, lparam);
}
调整大小实际上是由 Resize() 成员完成的,如下所示:
// execute the resizing of all controls
void ResizeManager::Resize(HWND hwnd, long cx, long cy)
{
// defer the moves & resizes for all visible controls
HDWP hdwp = BeginDeferWindowPos(m_resizables.size());
ASSERT(hdwp);
// reposition everything without doing any drawing!
for (ResizeAgentVector::const_iterator it = m_resizables.begin(), end = m_resizables.end(); it != end; ++it)
VERIFY(hdwp == it->Reposition(hdwp, cx, cy));
// now, do all of the moves & resizes at once
VERIFY(EndDeferWindowPos(hdwp));
}
也许最后一个棘手的部分可以在 ResizeAgent 的 Reposition() 处理程序中看到:
HDWP ResizeManager::ResizeAgent::Reposition(HDWP hdwp, long cx, long cy) const
{
// can't very well move things that no longer exist
if (!IsWindow(hwndControl))
return hdwp;
// calculate our new rect
const long left = IsFloatLeft() ? cx - offset.left : offset.left;
const long right = IsFloatRight() ? cx - offset.right : offset.right;
const long top = IsFloatTop() ? cy - offset.top : offset.top;
const long bottom = IsFloatBottom() ? cy - offset.bottom : offset.bottom;
// compute height & width
const long width = right - left;
const long height = bottom - top;
// we can defer it only if it is visible
if (IsWindowVisible(hwndControl))
return ::DeferWindowPos(hdwp, hwndControl, NULL, left, top, width, height, SWP_NOZORDER|SWP_NOACTIVATE);
// do it immediately for an invisible window
MoveWindow(hwndControl, left, top, width, height, FALSE);
// indicate that the defer operation should still be valid
return hdwp;
}
“棘手”是我们避免试图弄乱任何已被破坏的窗口,并且我们不会尝试将 SetWindowPos 推迟到不可见的窗口(因为这被记录为“将失败”。
我已经在一个隐藏了一些控件的真实项目中测试了上述内容,并使用了相当复杂的布局并取得了巨大的成功。即使没有 Aero,即使您使用对话框窗口的左上角调整大小(大多数可调整大小的窗口在您抓住该句柄 - IE、FireFox 等时会显示最多的闪烁和问题),也有零闪烁 (1)。
如果有足够的兴趣,我可能会被说服使用 CodeProject.com 或类似地方的真实示例实现来编辑我的发现。给我发短信。
(1) 请注意,不可能避免一次平局超过曾经存在的任何内容。对于对话框中没有改变的每一部分,用户什么都看不到(没有任何闪烁)。但是在事情发生变化的地方,用户可以看到变化——这是无法避免的,并且是 100% 的解决方案。