小标
2018-12-14
来源 :
阅读 1603
评论 0
摘要:本文主要向大家介绍了VC编程之VC++ 6.0 MFC List Control,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。
本文主要向大家介绍了VC编程之VC++ 6.0 MFC List Control,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。

1、List Control配置
[cpp] view plain copy
1.BOOL CDialog1::OnInitDialog()
2.{
3. CDialog::OnInitDialog();
4.
5. // TODO: Add extra initialization here
6.
7. LONG lStyle;
8. lStyle = GetWindowLong(m_list.m_hWnd, GWL_STYLE);// 获取当前窗口style
9. lStyle &= ~LVS_TYPEMASK; // 清除显示方式位
10. lStyle |= LVS_REPORT; // 设置style
11. SetWindowLong(m_list.m_hWnd, GWL_STYLE, lStyle);// 设置style
12. DWORD dwStyle = m_list.GetExtendedStyle();
13. dwStyle |= LVS_EX_FULLROWSELECT;// 选中某行使整行高亮(只适用与report 风格的listctrl )
14. dwStyle |= LVS_EX_GRIDLINES;// 网格线(只适用与report 风格的listctrl )
15. //dwStyle |= LVS_EX_CHECKBOXES;//item 前生成checkbox 控件
16. dwStyle |= LVS_SHOWSELALWAYS;
17. m_list.SetExtendedStyle(dwStyle); // 设置扩展风格
18.
19. return TRUE; // return TRUE unless you set the focus to a control
20. // EXCEPTION: OCX Property Pages should return FALSE
21.}
2、List Control 添加数据
[cpp] view plain copy
1. m_list.InsertColumn(0,"编号",LVCFMT_RIGHT,50,0);
2. m_list.InsertColumn(1,"地址",LVCFMT_LEFT,200,0);
3. m_list.InsertColumn(2,"端口",LVCFMT_LEFT,50,0);
4. m_list.InsertColumn(3,"状态",LVCFMT_LEFT,200,0);
5.
6.
7. int i = 0;
8. for(i=0; i<100; i++)
9. {
10. m_list.InsertItem(i,"");
11. m_list.SetItemText(i,0,"file");
12. m_list.SetItemText(i,1,"file");
13. m_list.SetItemText(i,2,"file");
14. m_list.SetItemText(i,3,"file");
15. }
3、List Control 人性化处理
重载函数OnCustomdrawList(),分三步走。
(1)添加响应事件
[cpp] view plain copy
1.BEGIN_MESSAGE_MAP(CDialog1, CDialog)
2. //{{AFX_MSG_MAP(CDialog1)
3. ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST, OnCustomdrawList)
4. //}}AFX_MSG_MAP
5.END_MESSAGE_MAP()
(2)添加函数声明
[cpp] view plain copy
1.// Implementation
2.protected:
3.
4. // Generated message map functions
5. //{{AFX_MSG(CDialog1)
6. virtual BOOL OnInitDialog();
7. afx_msg void OnCustomdrawList(NMHDR*, LRESULT*);
8. //}}AFX_MSG
9. DECLARE_MESSAGE_MAP()
(3)添加重载函数
[cpp] view plain copy
1.void CDialog1::OnCustomdrawList(NMHDR *pNMHDR, LRESULT *pResult)
2.{
3. //////////////////////////////////////////////////////
4. NMLVCUSTOMDRAW* pLVCD = reinterpret_cast( pNMHDR );
5.
6. // Take the default processing unless we
7.
8. // set this to something else below.
9.
10. *pResult = CDRF_DODEFAULT;
11.
12. // First thing - check the draw stage. If it's the control's prepaint
13.
14. // stage, then tell Windows we want messages for every item.
15.
16.
17. if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
18. {
19. *pResult = CDRF_NOTIFYITEMDRAW;
20. }
21. else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
22. {
23. COLORREF crText,crBk;//奇偶判断
24. if ( (pLVCD->nmcd.dwItemSpec % 2) == 0 ){
25. crText = RGB(0,0,0);//RGB(32,32,255);
26. crBk = RGB(229,232,239);
27. }
28. else if ( (pLVCD->nmcd.dwItemSpec % 2) == 1 ){
29. crText = RGB(0,0,0);
30. crBk = RGB(240,247,249);
31. }
32. else{
33. crText = RGB(0,0,0);
34. crBk = RGB(0,0,126);
35. }
36.
37. // Store the color back in the NMLVCUSTOMDRAW struct.
38.
39. pLVCD->clrText = crText;
40. pLVCD->clrTextBk = crBk;
41. //设置选择项的颜色
42. if( this->m_list.GetItemState(pLVCD->nmcd.dwItemSpec, CDIS_SELECTED) ){
43. crBk =RGB(75, 149, 229);//itunes//RGB(10, 36, 106);//RGB(0, 0, 64);
44. crText = RGB(255,255,255);
45. pLVCD->clrText = crText;
46. pLVCD->clrTextBk = crBk;
47. *pResult = CDRF_NEWFONT;
48. }
49. if(LVIS_SELECTED == m_list.GetItemState(pLVCD->nmcd.dwItemSpec,LVIS_SELECTED))
50. {
51. //清除选择状态,如果不清除的话,还是会显示出蓝色的高亮条
52. BOOL b = m_list.SetItemState(pLVCD->nmcd.dwItemSpec,0,LVIS_SELECTED);
53. pLVCD->clrText = crText;
54. pLVCD->clrTextBk = crBk;
55.
56. *pResult = CDRF_NEWFONT;
57. return;
58. }
59. *pResult = CDRF_NEWFONT;
60. //*pResult = CDRF_DODEFAULT;
61. }
62.}
OK,This ALL!
以上就介绍了VC/MFC的学习,希望对VC/MFC有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言VC/MFC频道!
喜欢 | 1
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号