尽管空间充足,CListCtrl仍显示省略号(仅限Win2008和Win7)

发布于 2024-12-31 23:53:37 字数 1612 浏览 7 评论 0原文

我正在使用 CListCtrl 在列表视图模式下显示一些带有图标的项目。大多数时候,列表中只有一项,右侧有足够的空间,但在我的 Win2008 系统(或 Win7)上,它使用省略号截断文本(例如“Tank”被截断为“Ta...”) 。并非所有数据都会发生这种情况(甚至一些较长的字符串也可以),但在“Tank”示例中会重复出现这种情况。在 WinXP 系统上它也可以正常工作 - 始终如此。

列表视图是通过 rc 文件创建的

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

,然后实例化

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

我只插入具有以下格式的 1 列:

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

并且插入了数据

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

我也尝试过

myListCtrl.SetColumnWidth(column, LVSCW_AUTOSIZE_USEHEADER); 

并且

myListCtrl.SetColumnWidth(column, LVSCW_AUTOSIZE); 

myListCtrl.SetExtendedStyle(LVS_EX_AUTOSIZECOLUMNS);

没有做到这一点。

有什么想法吗?

米夏

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

秋风の叶未落 2025-01-07 23:53:38

我遇到了这个问题,我想我终于找到了答案。我的情况的问题是对话框有一个以此样式指定的字体:

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"
.
.
.

如果我删除了 FONT 行和 DS_SETFONT 标志,CListCtrl再次开始显示没有截断的文本。

我的假设是它使用不同的字体来测量文本的宽度并实际进行绘图,这导致了截断。

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.

清眉祭 2025-01-07 23:53:38

我在这个问题上感到很烦恼,因为我在同一个对话框上有 2 个相同的 CListCtrl,其中一个显示省略号,另一个则不显示。

我的情况的问题是,由于使用 _fgetts() 从文件中读取项目,我插入的项目附加了换行符 (10)。回车显示同样的问题。

项目的长度并不重要,如果它有换行符,那么最后 2 个字符将被删除并替换为省略号(尽管至少会显示 1 个字符)。该问题仅出现在 Vista / Windows 7 以及相当于 2008、2008 R2 的服务器上。我想 Windows 8 和 Server 2012 也会显示它,但尚未测试。

剥离换行符显示该项目的全部荣耀!

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!

夜空下最亮的亮点 2025-01-07 23:53:38

就我而言,调整列表大小后,每行末尾都会出现省略号。解决方案是在填充列表之前调用“SetRedraw (false)”,并调用“SetRedraw (true)”来最终完成检查列表(Tom Archer 的解决方案)。

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.

寄人书 2025-01-07 23:53:38

这个评论有点晚了,但我也遇到了与所述完全相同的问题(Tank 这个词总是变成 Ta...)。查看其他建议后发现这是因为没有为控件配置字体。 ListCtrl 需要字体来测量提供的文本,以确定文本是否适合该区域。如果没有设置字体,像(Tank,BLT,Vegetables,...)这样的词都会以省略号结尾。要解决此问题,您应该添加代码以将字体设置为父窗口字体,如下所示:

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

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);
梦里寻她 2025-01-07 23:53:37

这可能对你有帮助。插入所有列和行后调用此函数。

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

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();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文