gpt4 book ai didi

c++ - 如何遍历 CListCtrl 的元素?

转载 作者:行者123 更新时间:2023-11-30 01:59:29 24 4
gpt4 key购买 nike

简要说明:
I got an instance of CTreectrl class where objects are stored, when an object is selected corresponding list report for that object is displayed.
我需要调整列的宽度派生自 CListCtrl 的类 MyListCtrl 的实例,以便用户无需调整列的大小即可看到该列中的数据。

我如何完成我的任务:
我实现了从树中选择对象时调用的方法 InitColumnWidth

    void COwnListCtrl::InitColumnWidth ( unsigned const * defwidth, BOOL chars )
{
/* find the largest string in each column and set column width accordingly
so that user no longer need to manually adjust columns width */

RECT r;
GetClientRect ( & r );
int* arrOfMaxColumnWidth = new int[NumCol]; // array where max length for columns will be stored
for (int i = 0; i < NumCol; ++i)
arrOfMaxColumnWidth[i] = 0; // initialize
tstring dataInColumn;
double Scale = ( defwidth && chars ) ? GetCharAveWidth ( * GetFont () ) : ( r.right * 0.01 );
int numberOfVisitedItems = 0;
do
{
dataInColumn = _T(""); // initialize
for (int col = 0; col < NumCol; ++col)
{
//DWORD const itemData = this->GetItemData(numberOfVisitedItems);

dataInColumn = this->GetSubItemString(this->GetItemData(numberOfVisitedItems), col);
/* 1-st varaint returns only first row of the list
because GetItemData always returns zero, but I need to visit all rows */
dataInColumn = this->GetSubItemString(numberOfVisitedItems, col);
/* 2-nd variant, works for most objects from CTreeCtrl, but for a few of them assertion is raised, system reports on the corrupted heap and application terminates*/
if (dataInColumn.length() > arrOfMaxColumnWidth[col])
arrOfMaxColumnWidth[col] = dataInColumn.length();
}
++numberOfVisitedItems;
}
while(dataInColumn.length() != 0); /* do{} while loop is used to get number of rows in the table
as long as an empty string is read, which means that all rows were read*/
for (int col = 0; col < NumCol; ++col)
int tmp = ColWidth[col] = arrOfMaxColumnWidth[col] * Scale;
ColWidth[0] = 100;

需要帮助!

最佳答案

使用这个(用下面的代码替换你的 for (int col = 0; col < NumCol; ++col)):

for (int col = 0; col < NumCol; ++col)
{
int len = 0;
for(int row = 0; this->GetItemCount(); ++row)
{
CString item = this->GetItemText(row, col);
if(item.GetLength() > len) len = item.GetLength();
}
if (len > arrOfMaxColumnWidth[col])
arrOfMaxColumnWidth[col] = len; // should you multiple it to Scale?
}

关于c++ - 如何遍历 CListCtrl 的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16171549/

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