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
354 lines
10 KiB
C++
354 lines
10 KiB
C++
#include "stdafx.h"
|
||
#include "CListCtrlEx.h"
|
||
#include "2015Remote.h"
|
||
|
||
// CHeaderCtrlEx 实现
|
||
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);
|
||
int nCol = pNMHeader->iItem;
|
||
|
||
if (m_pListCtrl && nCol >= 0 && nCol < (int)m_pListCtrl->m_Columns.size()) {
|
||
if (!m_pListCtrl->m_Columns[nCol].Visible) {
|
||
*pResult = TRUE; // 阻止拖拽
|
||
return;
|
||
}
|
||
}
|
||
*pResult = FALSE;
|
||
}
|
||
|
||
// CListCtrlEx 实现
|
||
IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)
|
||
|
||
CListCtrlEx::CListCtrlEx()
|
||
{
|
||
}
|
||
|
||
CListCtrlEx::~CListCtrlEx()
|
||
{
|
||
}
|
||
|
||
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
|
||
ON_WM_CONTEXTMENU()
|
||
ON_WM_ERASEBKGND()
|
||
END_MESSAGE_MAP()
|
||
|
||
void CListCtrlEx::SetConfigKey(const CString& strKey)
|
||
{
|
||
m_strConfigKey = strKey;
|
||
}
|
||
|
||
void CListCtrlEx::PreSubclassWindow()
|
||
{
|
||
CListCtrl::PreSubclassWindow();
|
||
|
||
// 虚拟列表模式必须在窗口创建后立即设置
|
||
if (m_bVirtualMode) {
|
||
// 使用 SetWindowLong 直接添加样式(ModifyStyle 对某些样式不起作用)
|
||
LONG_PTR style = ::GetWindowLongPtr(m_hWnd, GWL_STYLE);
|
||
style |= LVS_OWNERDATA;
|
||
::SetWindowLongPtr(m_hWnd, GWL_STYLE, style);
|
||
}
|
||
}
|
||
|
||
int CListCtrlEx::AddColumn(int nCol, LPCTSTR lpszColumnHeading, int nWidth, int nFormat, BOOL bCanHide)
|
||
{
|
||
// 添加到列表控件
|
||
int nResult = InsertColumnL(nCol, lpszColumnHeading, nFormat, nWidth);
|
||
|
||
if (nResult != -1) {
|
||
// 保存列信息
|
||
ColumnInfoEx info;
|
||
info.Name = lpszColumnHeading;
|
||
info.Width = nWidth;
|
||
info.Percent = 0.0f; // 稍后在 InitColumns 中计算
|
||
info.Visible = TRUE;
|
||
info.CanHide = bCanHide;
|
||
|
||
// 确保 vector 大小足够
|
||
if (nCol >= (int)m_Columns.size()) {
|
||
m_Columns.resize(nCol + 1);
|
||
}
|
||
m_Columns[nCol] = info;
|
||
}
|
||
|
||
return nResult;
|
||
}
|
||
|
||
void CListCtrlEx::InitColumns()
|
||
{
|
||
if (m_Columns.empty()) {
|
||
return;
|
||
}
|
||
|
||
// 子类化 Header 控件
|
||
SubclassHeader();
|
||
|
||
// 计算总宽度和百分比
|
||
int totalWidth = 0;
|
||
for (const auto& col : m_Columns) {
|
||
totalWidth += col.Width;
|
||
}
|
||
|
||
if (totalWidth > 0) {
|
||
for (auto& col : m_Columns) {
|
||
col.Percent = (float)col.Width / totalWidth;
|
||
}
|
||
}
|
||
|
||
// 从配置加载列可见性
|
||
LoadColumnVisibility();
|
||
|
||
// 应用列宽
|
||
AdjustColumnWidths();
|
||
}
|
||
|
||
void CListCtrlEx::SubclassHeader()
|
||
{
|
||
CHeaderCtrl* pHeader = GetHeaderCtrl();
|
||
if (pHeader && !m_HeaderCtrl.GetSafeHwnd()) {
|
||
m_HeaderCtrl.SubclassWindow(pHeader->GetSafeHwnd());
|
||
m_HeaderCtrl.m_pListCtrl = this;
|
||
}
|
||
}
|
||
|
||
void CListCtrlEx::AdjustColumnWidths()
|
||
{
|
||
if (m_Columns.empty() || GetSafeHwnd() == NULL) {
|
||
return;
|
||
}
|
||
|
||
CRect rect;
|
||
GetClientRect(&rect);
|
||
int totalWidth = rect.Width() - 20; // 减去滚动条宽度
|
||
|
||
// 计算可见列的总百分比
|
||
float visiblePercent = 0.0f;
|
||
for (const auto& col : m_Columns) {
|
||
if (col.Visible) {
|
||
visiblePercent += col.Percent;
|
||
}
|
||
}
|
||
|
||
// 按比例分配宽度给可见列
|
||
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 会让表头重画一次,OnSize 每次都跑这里,
|
||
// 逐列无条件设置就会在拖窗口时反复重画表头。
|
||
if (GetColumnWidth(i) != width) {
|
||
SetColumnWidth(i, width);
|
||
}
|
||
} else if (GetColumnWidth(i) != 0) {
|
||
SetColumnWidth(i, 0); // 隐藏列
|
||
}
|
||
}
|
||
}
|
||
|
||
BOOL CListCtrlEx::IsColumnVisible(int nCol) const
|
||
{
|
||
if (nCol >= 0 && nCol < (int)m_Columns.size()) {
|
||
return m_Columns[nCol].Visible;
|
||
}
|
||
return TRUE;
|
||
}
|
||
|
||
void CListCtrlEx::SetColumnVisible(int nCol, BOOL bVisible)
|
||
{
|
||
if (nCol >= 0 && nCol < (int)m_Columns.size()) {
|
||
if (m_Columns[nCol].CanHide || bVisible) { // 不允许隐藏的列只能设为可见
|
||
m_Columns[nCol].Visible = bVisible;
|
||
AdjustColumnWidths();
|
||
// 列数变化后完整重绘(含背景擦除),否则最后一列右侧会残留旧列残影。
|
||
// 不带 RDW_ALLCHILDREN:表头是列表的子窗口,列宽一变它自己就会重画,
|
||
// 强制跟着擦一帧只会在表头那行闪一下。
|
||
ScopedEraseBkgnd scope(*this);
|
||
RedrawWindow(NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW);
|
||
}
|
||
}
|
||
}
|
||
|
||
void CListCtrlEx::OnContextMenu(CWnd* pWnd, CPoint pt)
|
||
{
|
||
// 检查是否点击在表头区域
|
||
CHeaderCtrl* pHeader = GetHeaderCtrl();
|
||
if (pHeader) {
|
||
CRect headerRect;
|
||
pHeader->GetWindowRect(&headerRect);
|
||
if (headerRect.PtInRect(pt)) {
|
||
ShowColumnContextMenu(pt);
|
||
return;
|
||
}
|
||
}
|
||
|
||
// 非表头区域,调用父类处理
|
||
CListCtrl::OnContextMenu(pWnd, pt);
|
||
}
|
||
|
||
void CListCtrlEx::ShowColumnContextMenu(CPoint pt)
|
||
{
|
||
CMenu menu;
|
||
menu.CreatePopupMenu();
|
||
|
||
// 添加所有列到菜单(跳过空标题的列)
|
||
for (int i = 0; i < (int)m_Columns.size(); i++) {
|
||
// 跳过空标题的列
|
||
if (m_Columns[i].Name.IsEmpty()) {
|
||
continue;
|
||
}
|
||
|
||
UINT flags = MF_STRING;
|
||
if (m_Columns[i].Visible) {
|
||
flags |= MF_CHECKED;
|
||
}
|
||
// 不允许隐藏的列显示为灰色
|
||
if (!m_Columns[i].CanHide) {
|
||
flags |= MF_GRAYED;
|
||
}
|
||
menu.AppendMenuL(flags, 1000 + i, m_Columns[i].Name);
|
||
}
|
||
|
||
// 显示菜单并获取选择
|
||
int nCmd = menu.TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, this);
|
||
if (nCmd >= 1000 && nCmd < 1000 + (int)m_Columns.size()) {
|
||
ToggleColumnVisibility(nCmd - 1000);
|
||
}
|
||
}
|
||
|
||
void CListCtrlEx::ToggleColumnVisibility(int nColumn)
|
||
{
|
||
if (nColumn < 0 || nColumn >= (int)m_Columns.size()) {
|
||
return;
|
||
}
|
||
|
||
// 不允许隐藏的列不处理
|
||
if (!m_Columns[nColumn].CanHide) {
|
||
return;
|
||
}
|
||
|
||
// 切换可见性
|
||
m_Columns[nColumn].Visible = !m_Columns[nColumn].Visible;
|
||
|
||
// 保存到配置
|
||
SaveColumnVisibility();
|
||
|
||
// 重新调整列宽
|
||
AdjustColumnWidths();
|
||
|
||
// 列数变化后完整重绘(含背景擦除),否则最后一列(TLS 等)右侧会残留旧列残影。
|
||
// 与 OnSize 中 needRefresh 分支相同的处理:临时放行背景擦除 + 全量重绘。
|
||
// 表头不在其中:列宽变化会触发它自己重画,跟着擦一帧只会闪一下。
|
||
ScopedEraseBkgnd scope(*this);
|
||
RedrawWindow(NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW);
|
||
}
|
||
|
||
void CListCtrlEx::LoadColumnVisibility()
|
||
{
|
||
if (m_strConfigKey.IsEmpty()) {
|
||
return; // 没有设置配置键,不加载
|
||
}
|
||
|
||
// 配置结构:list\{ConfigKey},如 list\ClientList
|
||
// 格式:逗号分隔的隐藏列名,如 "备注,程序路径"
|
||
CT2A configKeyA(m_strConfigKey);
|
||
std::string strHidden = THIS_CFG.GetStr("list", std::string(configKeyA), "");
|
||
if (strHidden.empty()) {
|
||
return; // 使用默认值(全部显示)
|
||
}
|
||
|
||
// 解析隐藏的列名
|
||
std::vector<std::string> hiddenNames = StringToVector(strHidden, ',');
|
||
for (const auto& name : hiddenNames) {
|
||
// 查找列名对应的索引
|
||
for (int i = 0; i < (int)m_Columns.size(); i++) {
|
||
CT2A colNameA(m_Columns[i].Name);
|
||
if (name == std::string(colNameA)) {
|
||
if (m_Columns[i].CanHide) {
|
||
m_Columns[i].Visible = FALSE;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void CListCtrlEx::SaveColumnVisibility()
|
||
{
|
||
if (m_strConfigKey.IsEmpty()) {
|
||
return; // 没有设置配置键,不保存
|
||
}
|
||
|
||
// 只保存隐藏的列名
|
||
std::string strHidden;
|
||
for (int i = 0; i < (int)m_Columns.size(); i++) {
|
||
if (!m_Columns[i].Visible) {
|
||
if (!strHidden.empty()) strHidden += ",";
|
||
CT2A colNameA(m_Columns[i].Name);
|
||
strHidden += std::string(colNameA);
|
||
}
|
||
}
|
||
|
||
// 配置结构:list\{ConfigKey},如 list\ClientList
|
||
CT2A configKeyA(m_strConfigKey);
|
||
THIS_CFG.SetStr("list", std::string(configKeyA), strHidden);
|
||
}
|
||
|
||
BOOL CListCtrlEx::OnEraseBkgnd(CDC* pDC)
|
||
{
|
||
if (m_bSkipEraseBkgnd) {
|
||
// 只擦除列表底部空白区域,避免残影
|
||
int nItemCount = GetItemCount();
|
||
if (nItemCount > 0) {
|
||
CRect rcItem;
|
||
// 获取最后一项的矩形
|
||
if (GetItemRect(nItemCount - 1, &rcItem, LVIR_BOUNDS)) {
|
||
CRect rcClient;
|
||
GetClientRect(&rcClient);
|
||
// 如果最后一项下方有空白区域,擦除它
|
||
if (rcItem.bottom < rcClient.bottom) {
|
||
CRect rcEmpty(rcClient.left, rcItem.bottom, rcClient.right, rcClient.bottom);
|
||
pDC->FillSolidRect(&rcEmpty, GetBkColor());
|
||
}
|
||
}
|
||
} else {
|
||
// 没有项目时,擦除整个背景
|
||
return CListCtrl::OnEraseBkgnd(pDC);
|
||
}
|
||
return TRUE;
|
||
}
|
||
return CListCtrl::OnEraseBkgnd(pDC);
|
||
}
|
||
|