C++ 如何遍历CListCtrl的元素?

C++ 如何遍历CListCtrl的元素?,c++,mfc,clistctrl,C++,Mfc,Clistctrl,简要说明: 我得到了一个存储对象的CTreectrl类的实例,当选择一个对象时,会显示该对象的相应列表报告。 我需要调整MyListCtrl类实例的列宽度,该类派生自CListCtrl,这样用户就可以看到该列中的数据,而无需调整列的大小 我如何完成任务: 我实现了从树中选择对象时调用的InitColumnWidth方法 void COwnListCtrl::InitColumnWidth ( unsigned const * defwidth, BOOL chars )

简要说明:
我得到了一个存储对象的
CTreectrl
类的实例,当选择一个对象时,会显示该对象的相应列表报告。
我需要调整MyListCtrl类实例的列宽度,该类派生自
CListCtrl
,这样用户就可以看到该列中的数据,而无需调整列的大小

我如何完成任务:
我实现了从树中选择对象时调用的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;
void COwnListCtrl::InitColumnWidth(无符号常量*defwidth,布尔字符)
{
/*找到每列中最大的字符串并相应地设置列宽
这样用户就不再需要手动调整列宽*/
矩形r;
GetClientRect&r;
int*arrOfMaxColumnWidth=new int[NumCol];//存储列的最大长度的数组
对于(int i=0;iGetItemData(numberOfVisitedItems);
dataInColumn=this->GetSubItemString(this->GetItemData(numberOfVisitedItems),col);
/*1-st varaint仅返回列表的第一行
因为GetItemData总是返回零,但我需要访问所有行*/
dataInColumn=this->GetSubItemString(numberofvisiteds,col);
/*第二个变体,适用于CTreeCtrl中的大多数对象,但对于其中一些对象,会引发断言,系统会报告损坏的堆,应用程序会终止*/
if(dataInColumn.length()>arrOfMaxColumnWidth[col])
arrOfMaxColumnWidth[col]=dataInColumn.length();
}
++访问项目的数量;
}
while(dataInColumn.length()!=0);/*do{}while循环用于获取表中的行数
只要读取一个空字符串,这意味着所有行都已读取*/
for(int col=0;col
需要帮助!

使用此选项(将(int col=0;col替换为以下代码):

for(int col=0;colGetItemCount();++行)
{
CString item=this->GetItemText(行、列);
如果(item.GetLength()>len)len=item.GetLength();
}
if(len>arrOfMaxColumnWidth[col])
arrOfMaxColumnWidth[col]=len;//是否应将其乘以比例?
}

无需重新设计轮子。如果您只想调整列的大小以适应文本,请尝试:

for (int i = 0; i < NumCol; ++i)
    ListView_SetColumnWidth( m_hWnd, i, LVSCW_AUTOSIZE );
for(int i=0;i
这不起作用,因为this->GetItemCount()返回0,可能是这样???如果您有一个虚拟列表,您需要调用SetItemCount()方法。因此,我需要找到将项目添加到我的列表中的代码段,并在那里调用SetItemCount()with argument==我要添加的项目数???不完全是。你只需要找到这段代码,并检查哪些全局变量被用作行数。我打赌它将是
NumRow
no没有NumRow或任何更相似的变量,相反,我找到了插入项目的代码段,并对其进行了修改,使每一行me项目已插入项目计数已更新,但这不会影响int COWNLISTCRL::Insert(DWORD itemData,int pos){int newItemCount=this->GetItemCount()+1;this->SetItemCount(newItemCount);/*OK 24.04.2013,最初未实现,使用COWNLISTCRL操作时导致大量错误(例如宽度调整)*/返回插入项(LVIF_参数,pos,_T(“”,0,0,0,itemData);}您可能应该为最后一列设置
LVSCW\u AUTOSIZE\u USEHEADER
,以使其使用剩余的空间。我想这是个人喜好的问题,但我只会对固定宽度的列表控件(如在对话框中)这样做,而不会对大小调整框架中包含的列表视图这样做。真的。再次阅读我的注释,这不是我想说的。MoRea:你可能想在你的答案中提到<代码> LVSCWYAutsisiuUsHealths。
for (int i = 0; i < NumCol; ++i)
    ListView_SetColumnWidth( m_hWnd, i, LVSCW_AUTOSIZE );