【问题标题】:How to adjust width of a column to the width of the longest string in CListCtrl?如何将列的宽度调整为 CListCtrl 中最长字符串的宽度?
【发布时间】:2013-05-28 13:44:37
【问题描述】:

我正在尝试:

tstring subItemText;
    CDC* pDc = GetListCtrl().GetDC(); 
    for (int row = GetItemCount() - 1; row >= 0; --row) 
    {
      subItemText = _T("");
      for (int col = 0; col < NumCol; ++col)  
      {
        subItemText = this->GetSubItemString( GetItemData(row), col);
        CSize sz; 
        // get length of the string in logical units, by default 1 unit == 1 pixel, type of font is accounted
        sz = pDc->GetOutputTextExtent(subItemText.c_str());
        if (static_cast<int>(sz.cx) > ColWidth[col])
          ColWidth[col] = sz.cx; 
      }
    } 
    GetListCtrl().ReleaseDC (pDc);
    for (int col = 0; col < NumCol; ++col) 
    {
      SetColumnWidth(col, ColWidth[col]);
    }

结果列的宽度比该列中最大的字符串之一大 20/30%。 我希望列的宽度等于最大长度的字符串的宽度。

提前致谢!

【问题讨论】:

    标签: c++ mfc clistctrl gdi


    【解决方案1】:

    这可能是因为您没有在设备上下文中选择正确的字体。试试这个:

        tstring subItemText;
        CDC* pDc = GetListCtrl().GetDC(); 
    
        CFont *normalfont = GetListCtrl().GetFont() ;
        CFont *oldfont = pDc->SelectObject(normalfont) ;
    
        for (int row = GetItemCount() - 1; row >= 0; --row) 
        {
          subItemText = _T("");
          for (int col = 0; col < NumCol; ++col)  
          {
            subItemText = this->GetSubItemString( GetItemData(row), col);
            CSize sz; 
            // get length of the string in logical units, by default 1 unit == 1 pixel, type of font is accounted
            sz = pDc->GetOutputTextExtent(subItemText.c_str());
            if (static_cast<int>(sz.cx) > ColWidth[col])
              ColWidth[col] = sz.cx; 
          }
        } 
    
        pDc->SelectObject(oldfont) ;
    
        GetListCtrl().ReleaseDC (pDc);
        for (int col = 0; col < NumCol; ++col) 
        {
          SetColumnWidth(col, ColWidth[col]);
        }
    

    【讨论】:

    • 你的列表控件所有者被绘制了吗?
    • 在线“CFont *normalfont = GetListCtrl().GetFont() ;”您必须选择在 DrawItem 中使用的字体才能显示文本。在我的示例中,您将获得列表控件的字体,它不一定与 Drawitem 中使用的字体相同。但是如果没有看到你的代码,我无法告诉更多。
    【解决方案2】:

    我想你只需要这个:

    SetColumnWidth(col, LVSCW_AUTOSIZE);
    

    在我的项目中,我从CListCtrl 派生出自己的类并使用以下函数

    void CMyListCtrl::AutoSizeColumnWidths()
    {
        // size column widths to content
        int nNumColumns = GetHeaderCtrl()->GetItemCount();
    
        // for all columns ...
        for (int i = 0; i < nColumnCount; i++)
        {
            // find max of content vs header
            SetColumnWidth(i, LVSCW_AUTOSIZE);
            int nColumnWidth = GetColumnWidth(i);
            SetColumnWidth(i, LVSCW_AUTOSIZE_USEHEADER);
            int nHeaderWidth = GetColumnWidth(i); 
    
            // set width to max 
            SetColumnWidth(i, max(nColumnWidth, nHeaderWidth));
        }
        SetRedraw(TRUE);
    } 
    

    这样可以确保没有内容的列仍然根据标题文本调整大小。

    【讨论】:

    • 不,我在这里发布我的问题之前已经尝试过了,这并没有解决我的问题。
    • @spin_eight - 真的吗?这很奇怪 - 它对我来说很好 - 我已经编辑了我的答案以显示我使用的代码。
    • 我知道如何使用它,尽管感谢代码。我与第 3 方班级合作,您的解决方案不适用。
    猜你喜欢
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 2014-08-28
    • 2020-03-02
    • 1970-01-01
    • 2012-12-03
    • 2011-02-06
    相关资源
    最近更新 更多