Fix: Stop the online-host list header from flickering

The list header is a child window of the list view, and
LVS_EX_DOUBLEBUFFER only covers the item area. The list view was created
without WS_CLIPCHILDREN, so every whole-list Invalidate or RedrawWindow
also erased the header with the class background brush before repainting
it. On top of that, AdjustColumnWidths ran on every WM_SIZE and issued an
unconditional SetColumnWidth per column, and each call repaints the
header. The result was an occasional one-frame blink of the header row:
irregular, low frequency, and confined to the header because the item
area is double-buffered.

Both lists now carry WS_CLIPCHILDREN, RDW_ALLCHILDREN was dropped from the
redraw calls that only need the list body (that flag forces child
repaints even when they are clipped), and column widths are only set when
they actually change.

The header still needs its background erase, but only for the strip to
the right of the last column: AdjustColumnWidths reserves 20px there and
the header's own paint stops at the last column, so that strip is colored
by WM_ERASEBKGND alone. Skipping the erase entirely left it black after a
minimize/restore cycle, when the window contents are discarded. The
header subclass now fills just that strip with COLOR_3DFACE and leaves the
column area untouched, which keeps the flicker fix, and the needRefresh
branch of OnSize forces an erase-redraw of the header so the strip is
repainted on minimize/restore and maximize/normalize.

Verified with a standalone probe that mirrors the list setup for the
header erase counts, and by rebuilding the app and checking the
minimize/restore case reported against the first version of this change.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-24 15:45:49 +02:00
1 parent 434812f87d
commit 7160ea8f42
3 files changed
+62 -12

No files matched your search

+39 -5
View File
@@ -6,8 +6,35 @@
BEGIN_MESSAGE_MAP(CHeaderCtrlEx, CHeaderCtrl)
ON_NOTIFY_REFLECT(HDN_BEGINTRACKA, &CHeaderCtrlEx::OnBeginTrack)
ON_NOTIFY_REFLECT(HDN_BEGINTRACKW, &CHeaderCtrlEx::OnBeginTrack)
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
BOOL CHeaderCtrlEx::OnEraseBkgnd(CDC* pDC)
{
// 表头的 WM_PAINT 只画到最后一列,最后一列(如 TLS)右边那条空带
// (CListCtrlEx 算列宽时预留的 20px)要靠背景擦除上色。
// 之前整个跳过擦除,平时看着没事;最小化再还原时窗口内容被丢弃,
// 这条空带就再没人画,成了黑色且不会刷新(表头那行右端一条黑块)。
// 这里只补空带、不擦整行:整行擦除会在每次表头重画时先闪一帧底色。
CRect rcClient;
GetClientRect(&rcClient);
int used = 0;
const int nCols = GetItemCount();
if (nCols > 0) {
RECT rcLast = { 0 };
if (GetItemRect(nCols - 1, &rcLast)) {
used = rcLast.right; // 最后一列的右边界 = 表头已画到的位置
}
}
if (used < rcClient.right) {
CRect rcGap(used, 0, rcClient.right, rcClient.bottom);
pDC->FillSolidRect(&rcGap, ::GetSysColor(COLOR_3DFACE));
}
return TRUE;
}
void CHeaderCtrlEx::OnBeginTrack(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMHEADER pNMHeader = reinterpret_cast<LPNMHEADER>(pNMHDR);
@@ -139,8 +166,12 @@ void CListCtrlEx::AdjustColumnWidths()
for (int i = 0; i < (int)m_Columns.size(); i++) {
if (m_Columns[i].Visible) {
int width = (visiblePercent > 0) ? (int)(totalWidth * m_Columns[i].Percent / visiblePercent) : 0;
SetColumnWidth(i, width);
} else {
// 只改真正变化的列:SetColumnWidth 会让表头重画一次,OnSize 每次都跑这里,
// 逐列无条件设置就会在拖窗口时反复重画表头。
if (GetColumnWidth(i) != width) {
SetColumnWidth(i, width);
}
} else if (GetColumnWidth(i) != 0) {
SetColumnWidth(i, 0); // 隐藏列
}
}
@@ -161,8 +192,10 @@ void CListCtrlEx::SetColumnVisible(int nCol, BOOL bVisible)
m_Columns[nCol].Visible = bVisible;
AdjustColumnWidths();
// 列数变化后完整重绘(含背景擦除),否则最后一列右侧会残留旧列残影。
// 不带 RDW_ALLCHILDREN:表头是列表的子窗口,列宽一变它自己就会重画,
// 强制跟着擦一帧只会在表头那行闪一下。
ScopedEraseBkgnd scope(*this);
RedrawWindow(NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_UPDATENOW);
RedrawWindow(NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW);
}
}
}
@@ -235,9 +268,10 @@ void CListCtrlEx::ToggleColumnVisibility(int nColumn)
AdjustColumnWidths();
// 列数变化后完整重绘(含背景擦除),否则最后一列(TLS 等)右侧会残留旧列残影。
// 与 OnSize 中 needRefresh 分支相同的处理:临时放行背景擦除 + 全量 RedrawWindow。
// 与 OnSize 中 needRefresh 分支相同的处理:临时放行背景擦除 + 全量重绘。
// 表头不在其中:列宽变化会触发它自己重画,跟着擦一帧只会闪一下。
ScopedEraseBkgnd scope(*this);
RedrawWindow(NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_UPDATENOW);
RedrawWindow(NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW);
}
void CListCtrlEx::LoadColumnVisibility()