总结的有关ListCtrl的知识

来源:互联网 发布:中车待遇知乎 编辑:程序博客网 时间:2024/05/23 11:11

首先构造一个带有 勾选框listCtrl

LONG lStyle; 
lStyle = GetWindowLong(m_list_stock.m_hWnd, GWL_STYLE);// 获取当前窗口style 
lStyle &= ~LVS_TYPEMASK; // 清除显示方式位 
lStyle |= LVS_REPORT; // 设置style 
SetWindowLong(m_list_stock.m_hWnd, GWL_STYLE, lStyle);// 设置style 
DWORD dwStyle = m_list_stock.GetExtendedStyle(); 
dwStyle |= LVS_EX_FULLROWSELECT;// 选中某行使整行高亮(只适用与report 风格的listctrl ) 
dwStyle |= LVS_EX_GRIDLINES;// 网格线(只适用与report 风格的listctrl ) 
dwStyle |= LVS_EX_CHECKBOXES;//item 前生成checkbox 控件 
m_list_stock.SetExtendedStyle(dwStyle); // 设置扩展风格


CRect mRect;
m_list_stock.GetWindowRect(&mRect);     //获取控件矩形区域
float kuan = (mRect.Width() -50)/4;
m_list_stock.InsertColumn( 0, _T("行号"), LVCFMT_LEFT, 50 );// 插入列
m_list_stock.InsertColumn( 1, _T("证券代码"), LVCFMT_LEFT, kuan );// 插入列
m_list_stock.InsertColumn( 2, _T("证券名称"), LVCFMT_LEFT, kuan );// 插入列
m_list_stock.InsertColumn( 3, _T("证券数量"), LVCFMT_LEFT, kuan );


m_list_stock.InsertColumn( 4, _T("昨日收盘价"), LVCFMT_LEFT, kuan-3);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

1. 插入数据 (这种插入方法,新插入的在最前面

int i = 0;

int nloop = m_v_stock_info.size();

for (; i < nloop; i++)
{
strTemp = "";
strTemp.Format("%d",lineNum);
nRow = m_list_stock.InsertItem(0, strTemp);// 插入行
m_list_stock.SetItemText(nRow, 1, m_v_stock_info[i].name);// 股票代码

        。。。。。。。。。。。。。。。。。。。。。。。。。。。

。。。。。。。。。。。。。。。。。。。。。。。。。。。。

}


2. 删除指定的数据,可以删一行,也可以一次删除多行 ( 网上关于删除 多行数据的blog很多,我几乎挨个都试过了,可以说,没有一个可以用的。统统都是错的。很坑爹

   我发一个可以用的:

int totalItemCount = m_list_stock.GetItemCount();
if (  0 == totalItemCount)
{
//没有记录,返回
return;
}
if ( 0 == JugSeclectItem())
{
//没勾选返回
return;
}
//end

//删除所选,一个或者一次删除多个都可以
for (int i = totalItemCount-1; i >= 0; i--)  
{  
if (m_list_stock.GetCheck(i))
{
m_list_stock.DeleteItem(i);

}
}


关键就是:要倒过来删除”


3. 获取指定行的数据


int iCount = m_list_stock.GetItemCount();
CString strID, strSum;
for (int i = 0; i < iCount; i ++)
{

if ( FALSE == m_list_stock.GetCheck(i))
{
//直接在界面读取选中的行的 内容
strID = m_list_stock.GetItemText(i, 1);
strSum = m_list_stock.GetItemText(i, 3);
}
}


4. 全选或者全反选

int iCount = m_list_stock.GetItemCount();
for (int i = 0; i < iCount; i ++)
{
m_list_stock.SetCheck(i);//全选

m_list_stock.SetCheck(i,FALSE);//全部反选

}


5.  获取 当前点击的那一行(选中过,再点中,就反选;  没选过,点中了,就选上)

POSITION Pos = m_list_stock.GetFirstSelectedItemPosition();
int nSelect = -1;

bool bRet = false;
while ( Pos )
{
nSelect = m_list_stock.GetNextSelectedItem(Pos);    //nSelect能获得第几行


bRet = m_list_stock.GetCheck(nSelect);
if (bRet)//说明已经选中过
{
m_list_stock.SetCheck(nSelect,FALSE);
 
}
else
{
m_list_stock.SetCheck(nSelect);

}

////////////

在这里,可以 获取被选中行的内容,方法参考3.

}

0 0