CListCtrl shows ellipsis despite plenty of space (only Win2008 and Win7)

1.6k Views Asked by At

I'm using a CListCtrl to display some items with icons in ListView Mode. Most of the time there is only one item in the list with plenty of space to the right, but on my Win2008 system (or Win7) it truncates the text using ellipsis (e.g. "Tank" is truncated to "Ta..."). This does not happen with all data (even some longer strings work), but repeatedly with the "Tank" example. Also on a WinXP system it works fine - always.

The list view is created via a rc file with

CONTROL  "List2",IDC_LIST,"SysListView32",LVS_LIST | WS_BORDER | WS_TABSTOP,320,27,195,38

then it is instantiated

myListCtrl.SubclassDlgItem( IDC_LIST, this );
myListCtrl.ModifyStyle(LVS_OWNERDRAWFIXED, LVS_SHAREIMAGELISTS | LVS_SINGLESEL | LVS_SHOWSELALWAYS);

ListView_SetBkColor(myListCtrl.m_hWnd,PMAINFRM->GetColor(IDCOLOR_LI_BKG));
ListView_SetTextBkColor(myListCtrl.m_hWnd,PMAINFRM->GetColor(IDCOLOR_LI_BKG));

myListCtrl.SetImageList(PMAINFRM->GetImageList(IDICO_16),LVSIL_NORMAL);
myListCtrl.SetImageList(PMAINFRM->GetImageList(IDICO_16),LVSIL_SMALL);

I'm inserting only 1 column with the following format:

LV_COLUMN lvc;
lvc.mask = LVCF_FMT | LVCF_SUBITEM;
lvc.fmt = LVCFMT_LEFT;
lvc.iSubItem = 0;
myListCtrl.InsertColumn(0,&lvc);

And the data is inserted

int index = 0;
int nItem = m_lstClass.InsertItem(index,(LPTSTR) strLabel, iIconID));
myListCtrl.SetItemData( nItem, (DWORD)index);
myListCtrl.SetItemState( nItem, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED);

I've tried

myListCtrl.SetColumnWidth(column, LVSCW_AUTOSIZE_USEHEADER); 

as well as

myListCtrl.SetColumnWidth(column, LVSCW_AUTOSIZE); 

And a

myListCtrl.SetExtendedStyle(LVS_EX_AUTOSIZECOLUMNS);

didn't do the trick either.

Any ideas?

Micha

5

There are 5 best solutions below

0
On

I was pulling my hair out on this one as I had 2 identical CListCtrl's on the same dialog and one was showing the ellipsis and the other was not.

The issue in my case turned out to be that the items I was inserting had an appended newline character (10) as a result of reading the items from file with _fgetts(). Carriage return displayed the same issue.

The length of the item didn't matter, if it had a newline character then the last 2 characters would be stripped and replaced with ellipsis (although at least 1 character would be shown). The issue only manifested on Vista / Windows 7 and the server equivalents 2008, 2008 R2. I imagine Windows 8 and Server 2012 also show it but haven't yet tested that.

Stripping the newline character displayed the item in its full glory!

0
On

This might help you. Call this function after you have inserted all of your columns and rows.

void SizeAllColumns(CListCtrl& list)
{
  CHeaderCtrl* pHeader = list.GetHeaderCtrl();
  ASSERT(pHeader);
  if (pHeader)
  {
    // Turn off redraw until the columns have all been resized
    list.SetRedraw(FALSE);

    for (int iCurrCol = 0;
        iCurrCol < pHeader->GetItemCount();
        iCurrCol++)
    {
      list.SetColumnWidth(iCurrCol, LVSCW_AUTOSIZE);

      int nCurrWidth = list.GetColumnWidth(iCurrCol);

      list.SetColumnWidth(iCurrCol, LVSCW_AUTOSIZE_USEHEADER);

      int nColHdrWidth = list.GetColumnWidth(iCurrCol);

      list.SetColumnWidth(iCurrCol, max(nCurrWidth, nColHdrWidth));
    }

    // Now that sizing is finished, turn redraw back on and
    // invalidate so that the control is repainted
    list.SetRedraw(TRUE);
    list.Invalidate();
  }
}
0
On

I had this problem, and I think I have finally found the answer. The problem in my case was that the dialog had a font specified in this style:

IDD_DIALOG_TurnOnOffRecords DIALOG 0, 0, 376, 263
STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Turn on off records"
FONT 8, "@Arial Unicode MS"
.
.
.

If I removed the FONT line and the DS_SETFONT flag, CListCtrl started showing the text without truncation again.

My assumption is that it is using different fonts to measure the width of the text and actually do the drawing, this is causing the truncation.

0
On

This comment is a bit late, but I too had the exact same issue as stated (where the word Tank always becomes Ta...). After reviewing the other suggestions it turned out that it was because no font was configured for the control. The ListCtrl needs the font to measure the supplied text to determine if the text fits in the area. If the font is not set words like (Tank,BLT,Vegetables,...) will all end up with ellipsis. To fix this you should add code to set the font to the parent windows font like this:

CFont *pfont=this->GetFont();
myListCtrl->SetFont(pfont);
0
On

In my case appeared ellipsis at the end of each row after resizing the list. The call to "SetRedraw (false)" before filling the list and "SetRedraw (true)" to finalize the completion of checklist (the solution of Tom Archer) was the solution.