Files
SimpleRemoter/server/2015Remote/CListCtrlEx.h
T
yuanyuanxianganddeepseek-flash 7160ea8f42 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
2026-10-24 15:45:49 +02:00

101 lines
3.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#pragma once
#include <vector>
#include <string>
// 列信息结构
struct ColumnInfoEx {
CString Name; // 列名
int Width; // 初始宽度
float Percent; // 占比
BOOL Visible; // 是否可见
BOOL CanHide; // 是否允许隐藏(如序号列不允许)
};
class CListCtrlEx;
// 自定义 Header 控件,用于阻止隐藏列被拖拽
class CHeaderCtrlEx : public CHeaderCtrl
{
public:
CListCtrlEx* m_pListCtrl = nullptr;
protected:
DECLARE_MESSAGE_MAP()
afx_msg void OnBeginTrack(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
};
// CListCtrlEx - 支持列显示/隐藏功能的列表控件
class CListCtrlEx : public CListCtrl
{
DECLARE_DYNAMIC(CListCtrlEx)
friend class CHeaderCtrlEx; // 允许 CHeaderCtrlEx 访问 protected 成员
public:
CListCtrlEx();
virtual ~CListCtrlEx();
// 控制是否跳过背景擦除(减少闪烁)
void SetSkipEraseBkgnd(BOOL bSkip) { m_bSkipEraseBkgnd = bSkip; }
// RAII 辅助类:临时允许背景擦除,析构时自动恢复
class ScopedEraseBkgnd {
CListCtrlEx& m_list;
public:
ScopedEraseBkgnd(CListCtrlEx& list) : m_list(list) { m_list.SetSkipEraseBkgnd(FALSE); }
~ScopedEraseBkgnd() { m_list.SetSkipEraseBkgnd(TRUE); }
};
// 设置配置键名(用于区分不同列表的配置,如 "ClientList", "FileList")
void SetConfigKey(const CString& strKey);
// 添加列(替代 InsertColumnL)
// nCol: 列索引
// lpszColumnHeading: 列标题
// nFormat: 对齐方式,默认左对齐
// nWidth: 列宽
// bCanHide: 是否允许隐藏,默认允许
int AddColumn(int nCol, LPCTSTR lpszColumnHeading, int nWidth, int nFormat = LVCFMT_LEFT, BOOL bCanHide = TRUE);
// 初始化完成后调用,计算百分比并加载配置
void InitColumns();
// 调整列宽(窗口大小改变时调用)
void AdjustColumnWidths();
// 获取列是否可见
BOOL IsColumnVisible(int nCol) const;
// 设置列可见性
void SetColumnVisible(int nCol, BOOL bVisible);
// 设置虚拟列表模式(必须在窗口创建前调用,或在 DoDataExchange 中调用)
void SetVirtualMode(BOOL bVirtual = TRUE) { m_bVirtualMode = bVirtual; }
BOOL IsVirtualMode() const { return m_bVirtualMode; }
// 虚拟列表专用:设置项目数量(绕过 MFC 的 ASSERT)
BOOL SetItemCountExV(int iCount, DWORD dwFlags = LVSICF_NOINVALIDATEALL) {
ASSERT(m_bVirtualMode); // 仅虚拟列表模式可用
return (BOOL)::SendMessage(m_hWnd, LVM_SETITEMCOUNT, (WPARAM)iCount, (LPARAM)dwFlags);
}
protected:
virtual void PreSubclassWindow() override;
std::vector<ColumnInfoEx> m_Columns; // 列信息
CString m_strConfigKey; // 配置键名
CHeaderCtrlEx m_HeaderCtrl; // 子类化的 Header 控件
BOOL m_bSkipEraseBkgnd = TRUE; // 是否跳过背景擦除
BOOL m_bVirtualMode = FALSE; // 是否虚拟列表模式
void ShowColumnContextMenu(CPoint pt);
void ToggleColumnVisibility(int nColumn);
void LoadColumnVisibility();
void SaveColumnVisibility();
void SubclassHeader(); // 子类化 Header 控件
DECLARE_MESSAGE_MAP()
afx_msg void OnContextMenu(CWnd* pWnd, CPoint pt);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
};