How to adjust the width of CListCtrl columns to fit the longest string in each column?

StackOverflow https://stackoverflow.com/questions/16691286

  •  30-05-2022
  •  | 
  •  

문제

I am trying:

void MyListCtrl::UpdateWidthOfColumns( void )
{

  int NofColumns = GetItemCount();
  for (int i = 0; i < NofColumns - 1 ; ++i)
  {
      SetColumnWidth( i, LVSCW_AUTOSIZE_USEHEADER );
  }
}

result: width of each column is adjusted to it`s header string size, not to the largest string.
What I am doing wrong?

도움이 되었습니까?

해결책

You are doing this:

SetColumnWidth( i, LVSCW_AUTOSIZE_USEHEADER );

According to MSDN

LVSCW_AUTOSIZE_USEHEADER: Automatically sizes the column to fit the header text.

You need to go through each column, find the longest string:

CSize   sz;

for (/*for each column, go through each row*/)
{
   sz = pDC->GetTextExtent(str); // get string size for each row
   if (sz.cx > dx)
      dx = sz.cx;
}

Then

// Set the column width to the largest string.

SetColumnWidth(dx);

다른 팁

It is also possible with Windows hot key Ctrl Shift Plus

m_listLicences.SendMessage(WM_KEYDOWN, VK_CONTROL, 1);
m_listLicences.SendMessage(WM_KEYDOWN, VK_SHIFT, 1);
m_listLicences.SendMessage(WM_KEYDOWN, VK_ADD, 1);
m_listLicences.SendMessage(WM_KEYUP, VK_ADD, 1);
m_listLicences.SendMessage(WM_KEYUP, VK_SHIFT, 1);
m_listLicences.SendMessage(WM_KEYUP, VK_CONTROL, 1);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top