• VC++ ListCtrl Report使用


      1、在VC++ 6.0中新建基于对话框的MFC应用程序ListCtrl;

      2、在主对话框上添加一个List Control至合适的位置及大小;

      

      3、在对话框OnInitDialog中初始化ListCtrl,代码如下:

        // 设置ListCtrl的风格及扩展风格
        LONG lStyle;
        lStyle = GetWindowLong(m_ctlListCtrl.m_hWnd, GWL_STYLE);
        lStyle &= ~LVS_TYPEMASK; 
        lStyle |= LVS_REPORT; 
        SetWindowLong(m_ctlListCtrl.m_hWnd, GWL_STYLE, lStyle);
        
        DWORD dwStyle = m_ctlListCtrl.GetExtendedStyle();
        dwStyle |= LVS_EX_FULLROWSELECT;                                        //选中某行使整行高亮(LVS_REPORT)
        dwStyle |= LVS_EX_GRIDLINES;                                            //网格线(LVS_REPORT)
        dwStyle |= LVS_EX_CHECKBOXES;                                            //CheckBox
        m_ctlListCtrl.SetExtendedStyle(dwStyle); 
    
        // 向ListCtrl插入数据
        m_ctlListCtrl.InsertColumn( 0, _T("No."), LVCFMT_LEFT, 40);
        m_ctlListCtrl.InsertColumn( 1, _T("Name"), LVCFMT_LEFT, 120 );
        m_ctlListCtrl.InsertColumn( 2, _T("Address"), LVCFMT_LEFT, 170);
        m_ctlListCtrl.InsertColumn( 3, _T("Score"), LVCFMT_LEFT, 50);
        int nRowIndex = 0;
        m_ctlListCtrl.InsertItem(nRowIndex, _T("01"));
        m_ctlListCtrl.SetItemText(nRowIndex, 1, _T("Michael Jordan"));
        m_ctlListCtrl.SetItemText(nRowIndex, 2, _T("Brooklyn,New York "));
        m_ctlListCtrl.SetItemText(nRowIndex, 3, _T("99"));
        nRowIndex++;
        m_ctlListCtrl.InsertItem(nRowIndex, _T("02"));
        m_ctlListCtrl.SetItemText(nRowIndex, 1, _T("Lebron James"));
        m_ctlListCtrl.SetItemText(nRowIndex, 2, _T("Akron,Ohio"));
        m_ctlListCtrl.SetItemText(nRowIndex, 3, _T("98"));
        nRowIndex++;
        m_ctlListCtrl.InsertItem(nRowIndex, _T("03"));
        m_ctlListCtrl.SetItemText(nRowIndex, 1, _T("Kobe Bryant"));
        m_ctlListCtrl.SetItemText(nRowIndex, 2, _T("Philadelphia,Pennsylvania"));
        m_ctlListCtrl.SetItemText(nRowIndex, 3, _T("97"));
        nRowIndex++;
        m_ctlListCtrl.InsertItem(nRowIndex, _T("04"));
        m_ctlListCtrl.SetItemText(nRowIndex, 1, _T("James Harden"));
        m_ctlListCtrl.SetItemText(nRowIndex, 2, _T("Los Angeles,California"));
        m_ctlListCtrl.SetItemText(nRowIndex, 3, _T("96"));
        nRowIndex++;
        m_ctlListCtrl.InsertItem(nRowIndex, _T("05"));
        m_ctlListCtrl.SetItemText(nRowIndex, 1, _T("Russell Westbrook"));
        m_ctlListCtrl.SetItemText(nRowIndex, 2, _T("Long Beach,CA"));
        m_ctlListCtrl.SetItemText(nRowIndex, 3, _T("95"));

      4、添加消息响应

      // Generated message map functions
      //{{AFX_MSG(CListCtrlDlg)
      virtual BOOL OnInitDialog();
      afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
      afx_msg void OnPaint();
      afx_msg HCURSOR OnQueryDragIcon();
      afx_msg void OnClickListDemo(NMHDR* pNMHDR, LRESULT* pResult);
      afx_msg void OnCustomDrawListDemo(NMHDR* pNMHDR, LRESULT* pResult);
      afx_msg void OnItemChangedListDemo(NMHDR* pNMHDR, LRESULT* pResult);
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()



      BEGIN_MESSAGE_MAP(CListCtrlDlg, CDialog)
        //{{AFX_MSG_MAP(CListCtrlDlg)
        ON_WM_SYSCOMMAND()
        ON_WM_PAINT()
        ON_WM_QUERYDRAGICON()
        ON_NOTIFY(NM_CLICK, IDC_LIST_DEMO, OnClickListDemo)
        ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST_DEMO, OnCustomDrawListDemo)
        ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_DEMO, OnItemChangedListDemo)
        //}}AFX_MSG_MAP
      END_MESSAGE_MAP()



    void
    CListCtrlDlg::OnClickListDemo(NMHDR* pNMHDR, LRESULT* pResult) { NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; if(pNMListView->iItem != -1) { int i = pNMListView->iItem; int nCheckState = 0; if( m_ctlListCtrl.GetItemState(i, m_ctlListCtrl.GetCheck(i))) { nCheckState = 1; } CString strtemp; strtemp.Format(_T("Row = %d, Column = %d CheckState = %d"), pNMListView->iItem, pNMListView->iSubItem, nCheckState); AfxMessageBox(strtemp); } *pResult = 0; } void CListCtrlDlg::OnCustomDrawListDemo( NMHDR* pNMHDR, LRESULT* pResult ) { NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR); *pResult = 0; if(CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage) { *pResult = CDRF_NOTIFYITEMDRAW; } else if(CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage) { //奇数行 if(pLVCD->nmcd.dwItemSpec % 2) pLVCD->clrTextBk = RGB(255, 255, 128); //偶数行 else pLVCD->clrTextBk = RGB(128, 255, 255); //继续 *pResult = CDRF_DODEFAULT; } } void CListCtrlDlg::OnItemChangedListDemo(NMHDR* pNMHDR, LRESULT* pResult) { NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; // TODO: Add your control notification handler code here CString sTemp; if((pNMListView->uOldState & LVIS_FOCUSED) == LVIS_FOCUSED && (pNMListView->uNewState & LVIS_FOCUSED) == 0) { sTemp.Format(_T("%d losted focus"), pNMListView->iItem); } else if((pNMListView->uOldState & LVIS_FOCUSED) == 0 && (pNMListView->uNewState & LVIS_FOCUSED) == LVIS_FOCUSED) { sTemp.Format(_T("%d got focus"), pNMListView->iItem); } if((pNMListView->uOldState & LVIS_SELECTED) == LVIS_SELECTED && (pNMListView->uNewState & LVIS_SELECTED) == 0) { sTemp.Format(_T("%d losted selected"), pNMListView->iItem); } else if((pNMListView->uOldState & LVIS_SELECTED) == 0 && (pNMListView->uNewState & LVIS_SELECTED) == LVIS_SELECTED) { sTemp.Format(_T("%d got selected"), pNMListView->iItem); } if (!sTemp.IsEmpty()) { AfxMessageBox(sTemp); } *pResult = 0; }

      BOOL CListCtrlDlg::PreTranslateMessage(MSG* pMsg)
      {
        // Ctrl + A 全选
        if( (pMsg->hwnd == GetDlgItem(IDC_LIST_DEMO)->GetSafeHwnd()))
        {
          if(pMsg->message == WM_KEYDOWN)
          {
            switch (pMsg->wParam)
            {
            case 'A':
              if (::GetKeyState(VK_CONTROL) < 0)
              {
                for (int i=0;i < m_ctlListCtrl.GetItemCount();i++)
                {
                  m_ctlListCtrl.SetItemState(i, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
                }
              }
              return TRUE;
            }
          }
        }
        return CDialog::PreTranslateMessage(pMsg);
      }

     

      5、编译运行

      

      参考:http://www.cppblog.com/finehai/archive/2009/09/17/96574.html

  • 相关阅读:
    新冲刺Sprint3(第六天)
    新冲刺Sprint3(第五天)
    新冲刺Sprint3(第四天)
    应对Gradle联网问题、长时间卡在resolve dependencies的思路
    《点厨》项目——个人总结
    Sprint总结
    快速暴力解决Eclipse ADT和Android Studio兼容问题,创建同时兼容ADT和AS的安卓工程
    如何花样展示自己的摄影作品?强大的WebGL能力展示
    团队项目——《广商百货》演示(博客版)
    软件工程学期总结
  • 原文地址:https://www.cnblogs.com/MakeView660/p/6840601.html
Copyright © 2020-2023  润新知