惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 凌点

clr20r3 system.InvalidOperationException 程序终止的几种解决方案 能上Q 不能上网 JavaScript中url 传递参数(特殊字符) apache 多站点配置 C#中使用 SendMessage 向非顶端窗体发送组合键 System.Xml.XmlException: 根级别上的数据无效 XP,WIN7双系统启动问题 VM安装XP注意事项 VC 命令行 捕获输出 make 信息重定向 VC 进度条制件CProgressCtrl 用法笔记 VC List Control控件高级使用 VC中字符串取子串总结 如何禁用Windows屏保和电源管理 VC获取硬盘物理序列号 unicode cstring to char* - 凌点 将unicode的 Cstring 复制到粘贴板 VC由进程ID获取窗口句柄 嵌入式Linux操作系统学习规划
List Control 控件技巧总汇
凌点 · 2010-07-19 · via 博客园 - 凌点

以下未经说明,listctrl 默认view 风格为report

-------------------------------------------------------------------------------

1. CListCtrl 风格

       LVS_ICON: 为每个item显示大图标

       LVS_SMALLICON: 为每个item显示小图标

       LVS_LIST: 显示一列带有小图标的item

       LVS_REPORT: 显示item详细资料

直观的理解:windows资源管理器,"查看"标签下的"大图标,小图标,列表,详细资料

--------------------------------------------------------------------------------

2. 设置listctrl 风格及扩展风格

       LONG lStyle;

       lStyle = GetWindowLong(m_list.m_hWnd, GWL_STYLE);//获取当前窗口style

       lStyle &= ~LVS_TYPEMASK; //清除显示方式位

       lStyle |= LVS_REPORT; //设置style

       SetWindowLong(m_list.m_hWnd, GWL_STYLE, lStyle);//设置style

       DWORD dwStyle = m_list.GetExtendedStyle();

       dwStyle |= LVS_EX_FULLROWSELECT;//选中某行使整行高亮(只适用与report风格的listctrl)

dwStyle |= LVS_EX_GRIDLINES;//网格线(只适用与report风格的listctrl)

dwStyle |= LVS_EX_CHECKBOXES;//item前生成checkbox控件

m_list.SetExtendedStyle(dwStyle); //设置扩展风格

注:listview的style请查阅msdn      

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceshellui5/html/wce50lrflistviewstyles.asp

--------------------------------------------------------------------------------

3. 插入数据

       m_list.InsertColumn( 0, "ID", LVCFMT_LEFT, 40 );//插入列

       m_list.InsertColumn( 1, "NAME", LVCFMT_LEFT, 50 );

       int nRow = m_list.InsertItem(0, "11");//插入行

       m_list.SetItemText(nRow, 1, "jacky");//设置数据

--------------------------------------------------------------------------------

4. 一直选中item

选中style中的Show selection always,或者在上面第2点中设置LVS_SHOWSELALWAYS

--------------------------------------------------------------------------------

5. 选中和取消选中一行

int nIndex = 0;

//选中

m_list.SetItemState(nIndex, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);

//取消选中

     m_list.SetItemState(nIndex, 0, LVIS_SELECTED|LVIS_FOCUSED);

--------------------------------------------------------------------------------

6. 得到listctrl中所有行的checkbox的状态

m_list.SetExtendedStyle(LVS_EX_CHECKBOXES);

CString str;

for(int i=0; i<m_list.GetItemCount(); i++)

{

if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED || m_list.GetCheck(i))

{

str.Format(_T("第%d行的checkbox为选中状态"), i);

AfxMessageBox(str);

}

}

--------------------------------------------------------------------------------

7. 得到listctrl中所有选中行的序号

方法一:

       CString str;

       for(int i=0; i&lt;m_list.GetItemCount(); i++)

       {

           if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED )

           {

                 str.Format(_T("选中了第%d行"), i);

                 AfxMessageBox(str);

           }

       }

方法二:

       POSITION pos = m_list.GetFirstSelectedItemPosition();

       if (pos == NULL)

           TRACE0("No items were selected!\n");

       Else

       {

           while (pos)

           {

                 int nItem = m_list.GetNextSelectedItem(pos);

                 TRACE1("Item %d was selected!\n", nItem);

                 // you could do your own processing on nItem here

           }

       }

--------------------------------------------------------------------------------

8. 得到item的信息

       TCHAR szBuf[1024];

       LVITEM lvi;

       lvi.iItem = nItemIndex;

       lvi.iSubItem = 0;

       lvi.mask = LVIF_TEXT;

       lvi.pszText = szBuf;

       lvi.cchTextMax = 1024;

       m_list.GetItem(&lvi);

关于得到设置item的状态,还可以参考msdn文章

       Q173242: Use Masks to Set/Get Item States in ClistCtrl

http://support.microsoft.com/kb/173242/en-us

--------------------------------------------------------------------------------

9. 得到listctrl的所有列的header字符串内容

       LVCOLUMN lvcol;

       char   str[256];

       int   nColNum;

       CString   strColumnName[4];//假如有4列

       nColNum = 0;

       lvcol.mask = LVCF_TEXT;

       lvcol.pszText = str;

       lvcol.cchTextMax = 256;

       while(m_list.GetColumn(nColNum, &lvcol))

       {

           strColumnName[nColNum] = lvcol.pszText;

           nColNum++;

       }

--------------------------------------------------------------------------------

10. 使listctrl中一项可见,即滚动滚动条

m_list.EnsureVisible(i, FALSE);

--------------------------------------------------------------------------------

11. 得到listctrl列数

int nHeadNum = m_list.GetHeaderCtrl()->GetItemCount();

--------------------------------------------------------------------------------

12. 删除所有列

方法一:

         while ( m_list.DeleteColumn (0))

因为你删除了第一列后,后面的列会依次向上移动。

方法二:

       int nColumns = 4;

       for (int i=nColumns-1; i&gt;=0; i--)

           m_list.DeleteColumn (i);

--------------------------------------------------------------------------------

13. 得到单击的listctrl的行列号

添加listctrl控件的NM_CLICK消息相应函数

       void CTest6Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)

       {

           // 方法一:

           /*

           DWORD dwPos = GetMessagePos();

           CPoint point( LOWORD(dwPos), HIWORD(dwPos) );

           m_list.ScreenToClient(&point);

           LVHITTESTINFO lvinfo;

           lvinfo.pt = point;

           lvinfo.flags = LVHT_ABOVE;

           int nItem = m_list.SubItemHitTest(&lvinfo);

           if(nItem != -1)

           {

                 CString strtemp;

                 strtemp.Format("单击的是第%d行第%d列", lvinfo.iItem, lvinfo.iSubItem);

                 AfxMessageBox(strtemp);

           }

           */

         // 方法二:

           /*

           NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

           if(pNMListView-&gt;iItem != -1)

           {

                 CString strtemp;

                 strtemp.Format("单击的是第%d行第%d列",

                                 pNMListView-&gt;iItem, pNMListView-&gt;iSubItem);

                 AfxMessageBox(strtemp);

           }

           */

           *pResult = 0;

       }

--------------------------------------------------------------------------------

14. 判断是否点击在listctrl的checkbox上

添加listctrl控件的NM_CLICK消息相应函数

       void CTest6Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)      

{          

DWORD dwPos = GetMessagePos();

           CPoint point( LOWORD(dwPos), HIWORD(dwPos) );  

           m_list.ScreenToClient(&point);

           LVHITTESTINFO lvinfo;

           lvinfo.pt = point;

           lvinfo.flags = LVHT_ABOVE;

           UINT nFlag;

           int nItem = m_list.HitTest(point, &nFlag);

           //判断是否点在checkbox上

           if(nFlag == LVHT_ONITEMSTATEICON)

           {

                 AfxMessageBox("点在listctrl的checkbox上");

           }

           *pResult = 0;

       }

--------------------------------------------------------------------------------

15. 右键点击listctrl的item弹出菜单

添加listctrl控件的NM_RCLICK消息相应函数

       void CTest6Dlg::OnRclickList1(NMHDR* pNMHDR, LRESULT* pResult)

       {

           NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

           if(pNMListView-&gt;iItem != -1)

           {

                 DWORD dwPos = GetMessagePos();

                 CPoint point( LOWORD(dwPos), HIWORD(dwPos) );

                 CMenu menu;

                 VERIFY( menu.LoadMenu( IDR_MENU1 ) );

                 CMenu* popup = menu.GetSubMenu(0);

                 ASSERT( popup != NULL );

               popup-&gt;TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this );

           }

           *pResult = 0;

   }

16. item切换焦点时(包括用键盘和鼠标切换item时),状态的一些变化顺序

添加listctrl控件的LVN_ITEMCHANGED消息相应函数

       void CTest6Dlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult)

       {

           NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

           // TODO: Add your control notification handler code here

           CString sTemp;

           if((pNMListView-&gt;uOldState & LVIS_FOCUSED) == LVIS_FOCUSED && (pNMListView-&gt;uNewState & LVIS_FOCUSED) == 0)

           {

                 sTemp.Format("%d losted focus",pNMListView-&gt;iItem);

           }

           else if((pNMListView-&gt;uOldState & LVIS_FOCUSED) == 0 &&

               (pNMListView-&gt;uNewState & LVIS_FOCUSED) == LVIS_FOCUSED)

           {

                 sTemp.Format("%d got focus",pNMListView-&gt;iItem);

           }

           if((pNMListView-&gt;uOldState & LVIS_SELECTED) == LVIS_SELECTED &&

             (pNMListView-&gt;uNewState & LVIS_SELECTED) == 0)

           {

                 sTemp.Format("%d losted selected",pNMListView-&gt;iItem);

           }

else if((pNMListView-&gt;uOldState & LVIS_SELECTED) == 0 && (pNMListView-&gt;uNewState & LVIS_SELECTED) == LVIS_SELECTED)

           {

sTemp.Format("%d got selected",pNMListView-&gt;iItem);

}

*pResult = 0;

本文出自 51CTO.COM技术博客 浏览原文