gpt4 book ai didi

c++ - 如何将列宽调整为CListCtrl中最长字符串的宽度?

转载 作者:搜寻专家 更新时间:2023-10-31 00:39:08 25 4
gpt4 key购买 nike

我正在尝试:

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%。我希望列的宽度等于具有最大长度的字符串的宽度。

提前致谢!

最佳答案

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

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

关于c++ - 如何将列宽调整为CListCtrl中最长字符串的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16793689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com