The width of each cell in the ListCtrl control in MFC is adaptive

In the ListCtrl control, make the width of each cell in the table adaptive.

void CSecond::AdjustColumnWidth()
{
    
    
	SetRedraw(FALSE);
	//m_listCtrl为我自己定义的ListCtrl控件关联的变量名
	int nColumnCount = m_listCtrl.GetHeaderCtrl()->GetItemCount();//获取列数
	for (int i = 0; i < nColumnCount; i++)
	{
    
    
		m_listCtrl.SetColumnWidth(i, LVSCW_AUTOSIZE);
		int nColumnWidth = m_listCtrl.GetColumnWidth(i);
		m_listCtrl.SetColumnWidth(i, LVSCW_AUTOSIZE_USEHEADER);
		int nHeaderWidth = m_listCtrl.GetColumnWidth(i);

        //最后显示该列中宽度值最大的宽度
		m_listCtrl.SetColumnWidth(i, max(nColumnWidth, nHeaderWidth));
	}
	SetRedraw(TRUE);
}

Guess you like

Origin blog.csdn.net/weixin_38652989/article/details/108826585