更新1:这是简化版本:
所以我有一个特殊的固定大小的子窗口,我想制作它留在右侧可调整大小的主窗口。当用户通过拖动主窗口的左/右边缘来调整主窗口的大小时,会发送 WM_WINDOWPOSCHANGED ,子窗口将在此消息处理程序中移动,使其“粘”到右侧,并且发生这种情况时不会闪烁。
但是,当我尝试通过 SetWindowPos 以编程方式调整主窗口大小时,出现明显的闪烁。似乎操作系统将旧内容复制到新客户区,甚至在我有机会处理 WM_WINDOWPOSCHANGED 中的子窗口重新定位之前。以下是 SetWindowPos 和 WM_SIZE 之间调度的消息:
WndProc: 0x00000046 WM_WINDOWPOSCHANGING
WndProc: 0x00000024 WM_GETMINMAXINFO
WndProc: 0x00000083 WM_NCCALCSIZE
WndProc: 0x00000093 WM_UAHINITMENU
===Flickering happens between these two messages!===
WndProc: 0x00000085 WM_NCPAINT
WndProc: 0x00000093 WM_UAHINITMENU
WndProc: 0x00000093 WM_UAHINITMENU
WndProc: 0x00000091 WM_UAHDRAWMENU
WndProc: 0x00000092 WM_UAHDRAWMENUITEM
WndProc: 0x00000092 WM_UAHDRAWMENUITEM
WndProc: 0x00000092 WM_UAHDRAWMENUITEM
WndProc: 0x00000014 WM_ERASEBKGND
WndProc: 0x00000047 WM_WINDOWPOSCHANGED
WndProc: 0x00000003 WM_MOVE
WndProc: 0x00000005 WM_SIZE
这是可重现的伪代码。要测试它,您可以通过 Visual Studio 的新建项目向导创建一个 Windows 桌面应用程序项目,然后将这些代码复制到适当的位置。发生闪烁是因为操作系统“BitBlt”将旧内容(由于左侧没有其他子窗口,因此为白色背景)转移到新客户区域。如果您在主窗口的左侧创建另一个子窗口,闪烁将会更加明显。
HWND g_hWndList = NULL;
#define LIST_WIDTH 500
#define LIST_HEIGHT 400
void GetListRect(HWND hWnd, RECT& rectList)
{
GetClientRect(hWnd, &rectList);
InflateRect(&rectList, -10, -10);
rectList.left = rectList.right - LIST_WIDTH;
rectList.bottom = rectList.top + LIST_HEIGHT;
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, ...);
RECT rectList;
GetListRect(hWnd, rectList);
g_hWndList = CreateWindow(WC_LISTVIEW, TEXT("listR"), WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP | LVS_REPORT,
rectList.left, rectList.top, rectList.right - rectList.left, rectList.bottom - rectList.top, hWnd, nullptr, hInstance, nullptr);
...
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
switch (wmId)
{
case IDM_ABOUT:
// Resize the window instead of showing "About" dialog
//DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
{
RECT rect;
GetWindowRect(hWnd, &rect);
rect.left += 100; // make it smaller
SetWindowPos(hWnd, nullptr, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOACTIVATE | SWP_NOZORDER);
}
break;
}
}
break;
case WM_WINDOWPOSCHANGED:
{
RECT rectList;
GetListRect(hWnd, rectList);
SetWindowPos(g_hWndList, nullptr, rectList.left, rectList.top, 0, 0, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE);
}
break;
}
}
注意:当您仅使用 SetWindowPos 更改主窗口的右边缘时,不会发生闪烁。
原文内容:
假设我有一个带有两个列表控件的对话框,我希望左侧的列表控件随对话框一起调整大小,但右侧的列表控件保持相同的大小。手动调整大小时不闪烁 https://i.stack.imgur.com/PQm7Z.gif
当用户拖动对话框的左(或右)边缘来调整其大小时,不会出现闪烁。但是,当我通过调用 SetWindowPos 以编程方式执行此操作时,将会出现明显的闪烁。看来 Windows 在发送 WM_SIZE 之前就将保存的内容复制到窗口中。
SetWindowPos产生闪烁 https://i.stack.imgur.com/vVuO0.gif
我知道这个问题之前已经被提出过,有些人 https://stackoverflow.com/questions/2165759/how-do-i-force-windows-not-to-redraw-anything-in-my-dialog-when-the-user-is-resiz建议WM_NCCALCSIZE https://msdn.microsoft.com/en-us/library/windows/desktop/ms632634(v=vs.85).aspx可以帮助。尽管它的文档似乎确实是可行的方法,但我仍然无法解决闪烁问题。
代码基本上如下所示。我还放了一个演示项目 https://github.com/wingkinl/WindowResizingFlickeringDemo在 github 上。
我在这里做错了什么?
BOOL g_bExpandingShrinking = FALSE;
void OnCommandExpandShrinkWindow(HWND hWnd, BOOL bExpand)
{
RECT rect;
GetWindowRect(hWnd, &rect);
rect.left += bExpand ? -100 : 100;
UINT nFlags = SWP_NOZORDER | SWP_NOACTIVATE;
g_bExpandingShrinking = TRUE;
SetWindowPos(hWnd, nullptr, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, nFlags);
g_bExpandingShrinking = FALSE;
}
LRESULT OnNcCalcSize(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
NCCALCSIZE_PARAMS* lpncsp = reinterpret_cast<NCCALCSIZE_PARAMS*>(lParam);
LRESULT res;
if (wParam && g_bExpandingShrinking)
{
// let DefWindowProc calculate the new client rectangle
res = DefWindowProc(hWnd, WM_NCCALCSIZE, wParam, lParam);
// copy the content of the right list control
GetWindowRect(g_hwndListRight, lpncsp->rgrc + 2);
lpncsp->rgrc[1] = lpncsp->rgrc[2];
res = WVR_VALIDRECTS;
}
else
{
res = DefWindowProc(hWnd, WM_NCCALCSIZE, wParam, lParam);
}
return res;
}
窗口向左无闪烁扩展(调整大小) https://stackoverflow.com/questions/48825519/flicker-free-expansion-resize-of-a-window-to-the-left
从左侧调整大小时窗口闪烁 https://stackoverflow.com/questions/20958574/flickering-on-window-when-resizing-from-left-side