Question

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?

Was it helpful?

Solution

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);

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top