ON_NOTIFY(HDN_DIVIDERDBLCLICK, 0, &CXXX::OnHdnDividerdblclickListData)
void CXXX::OnHdnDividerdblclickListData(NMHDR *pNMHDR, LRESULT *pResult) { LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR); // TODO: 在此添加控件通知处理程序代码 int col = phdr->iItem; CHeaderCtrl* pHeaderCtrl = m_wndList.GetHeaderCtrl(); int nColumnCount = pHeaderCtrl->GetItemCount(); if ((col < 0) || (nColumnCount <= 0) || (col >= nColumnCount)) { return; } int width = 0; //如果是最后一列 if (col == nColumnCount - 1) { CDC* pDC = this->GetDC(); LVCOLUMN lvc = { 0 }; lvc.mask = LVCF_WIDTH | LVCF_TEXT | LVCF_MINWIDTH | LVCF_DEFAULTWIDTH | LVCF_IDEALWIDTH; TCHAR sColText[256] = { 0 }; lvc.pszText = sColText; lvc.cchTextMax = sizeof(sColText) - 1; m_wndList.GetColumn(col, &lvc); if (lvc.pszText != nullptr) { width = pDC->GetTextExtent(lvc.pszText).cx; } for (int i = 0; i < m_wndList.GetItemCount(); ++i) { int w = pDC->GetTextExtent(m_wndList.GetItemText(i, col)).cx; if (w > width) { width = w; } } this->ReleaseDC(pDC); } else { m_wndList.SetColumnWidth(col, LVSCW_AUTOSIZE); int nColumnWidth = m_wndList.GetColumnWidth(col); m_wndList.SetColumnWidth(col, LVSCW_AUTOSIZE_USEHEADER); int nHeaderWidth = m_wndList.GetColumnWidth(col); width = max(nColumnWidth, nHeaderWidth); } width += 10; m_wndList.SetColumnWidth(col, width); *pResult = 0; }
当然,也可以实现自己的CListCtrl,此时需要将Message Map稍加改造,这点需要注意!
ON_NOTIFY(HDN_DIVIDERDBLCLICKA, 0, &CMyListCtrl::OnHdnDividerdblclick) ON_NOTIFY(HDN_DIVIDERDBLCLICKW, 0, &CMyListCtrl::OnHdnDividerdblclick)