如何迭代CListCtrl的元素
How to iterate through elements of CListCtrl?
简要描述:
我得到了一个CTreectrl
类的实例,其中存储了对象,当选择一个对象时,会显示该对象的相应列表报告
我需要调整从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?
}
无需重新发明轮子。如果您只想调整列的大小以适应文本,请尝试:
for (int i = 0; i < NumCol; ++i)
ListView_SetColumnWidth( m_hWnd, i, LVSCW_AUTOSIZE );
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 使用不带参数的函数访问结构元素
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- C++如何通过用户输入删除列表元素
- lower_bound()返回最后一个元素
- 基于多个条件处理地图中的所有元素
- 调整大小后指向元素值的指针unordered_map有效?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 使用函数"remove"删除重复元素
- 具有最大子序列大小的序列,每个元素都相同
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将元素添加到数组的线程安全函数?
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- 我想访问std::unique_ptr中的一个特定元素
- 如何通过 getter 函数删除矢量的元素?
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 从控制台中删除最后打印的元素
- 如何迭代CListCtrl的元素