MFC自动调整CListCtrl控件列宽

2011-03-28 22:30:12|?次阅读|上传:wustguangh【已有?条评论】发表评论

关键词:C/C++, MFC, 界面设计|来源:唯设编程网

在使用CListCtrl列表控件的程序中,经常需要根据内容自动调整列宽,以下代码实现了该功能:

void CPrintsysDlg::AdjustColumnWidth()
{
    //SetRedraw(FALSE);
    int nColumnCount = GetColumnCount();

    for (int i = 0; i < nColumnCount; i++)
    {
        m_list.SetColumnWidth(i, LVSCW_AUTOSIZE);
        int nColumnWidth =m_list. GetColumnWidth(i);
        m_list.SetColumnWidth(i, LVSCW_AUTOSIZE_USEHEADER);
        int nHeaderWidth = m_list.GetColumnWidth(i);
        m_list.SetColumnWidth(i, max(nColumnWidth, nHeaderWidth));
    }
    // SetRedraw(TRUE);
}
int CPrintsysDlg::GetColumnCount()
{
    CHeaderCtrl* pHeaderCtrl =m_list. GetHeaderCtrl();
    return (pHeaderCtrl->GetItemCount());
}


As we can see, this function calculates the widths of the Column Headers as well as the Items and sets the width to the larger of the two, so that complete header is visible even if no item is inserted. Also note that the last column will take all the remaining space.

 

发表评论0条 】
网友评论(共?条评论)..
MFC自动调整CListCtrl控件列宽