} } }

    CTreeCtrl和CListCtrl落空核心时高亮选中项

    添加时间:2013-7-25 点击量:

    设置CTreeCtrl的Always Show Selection:True
    CListCtrl的Always Show Selection:False
    在NM_CUSTOMDRAW事务中添加如下代码:

    void CHighLightDlg::OnNMCustomdrawTree1(NMHDR pNMHDR, LRESULT pResult)
    {
        LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
        // TODO: 在此添加控件通知处理惩罚法度代码
        pResult = 0;

        LPNMTVCUSTOMDRAW pDraw=(LPNMTVCUSTOMDRAW)pNMHDR;
        DWORD dwDrawStage=pDraw->nmcd.dwDrawStage;
        UINT uItemState =pDraw->nmcd.uItemState;
        //
        pResult=CDRF_NOTIFYITEMDRAW;
        //|CDRF_NOTIFYPOSTPAINT|CDRF_NOTIFYSUBITEMDRAW|CDRF_NOTIFYPOSTERASE;
        CDC pdc=CDC::FromHandle(pDraw->nmcd.hdc);
        CRect rc;
        HTREEITEM hItem=(HTREEITEM) pDraw->nmcd.dwItemSpec;
        m_tree.GetItemRect(hItem,&rc,TRUE);//FALSE);text only
        CString txt=m_tree.GetItemText(hItem);
        if((dwDrawStage & CDDS_ITEM) && (uItemState & CDIS_SELECTED))
        {// 
            pdc->FillSolidRect(&rc,RGB(49106197));//clr);
            
    //
            pdc->SetTextColor(RGB(255255255));//white
            pdc->SetBkColor(RGB(49106197));//clr);
            
    //

            CFont pfnt=pdc->GetCurrentFont();
            //
            pdc->TextOut(rc.left+2,rc.top+2,txt);
            pdc->SelectObject(pfnt);

            pResult |= CDRF_SKIPDEFAULT;
            // afxDump << 1\n;
        }
        else // without these ,1st blue !
        {
            pdc->FillSolidRect(&rc, GetSysColor(COLOR_WINDOW));
            pdc->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
            pdc->TextOut(rc.left+2, rc.top+2, txt);
            // afxDump << 2\n;
        }
    }

    void CHighLightDlg::OnNMCustomdrawList2(NMHDR pNMHDR, LRESULT pResult)
    {
        LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
        // TODO: 在此添加控件通知处理惩罚法度代码
        pResult = 0;

        //This code based on Michael Dunns excellent article on
        
    //list control custom draw at http://www.codeproject.com/listctrl/lvcustomdraw.asp

        NMLVCUSTOMDRAW pLVCD = reinterpret_cast<NMLVCUSTOMDRAW>( pNMHDR );

        // Take the default processing unless we set this to something else below.
        pResult = CDRF_DODEFAULT;
        // First thing - check the draw stage. If its the controls prepaint
        
    // stage, then tell Windows we want messages for every item.
        if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
        {
            pResult = CDRF_NOTIFYITEMDRAW;
        }
        else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
        {
            // This is the notification message for an item.  Well request
            
    // notifications before each subitems prepaint stage.

            pResult = CDRF_NOTIFYSUBITEMDRAW;
        }
        else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
        {

            COLORREF clrNewTextColor, clrNewBkColor;

            int    nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );

            BOOL bSelect = FALSE;
            POSITION pos =m_lst.GetFirstSelectedItemPosition(); 
            while(pos) 
            { 
                int index = m_lst.GetNextSelectedItem(pos); 
                if(index==nItem) 
                { 
                    bSelect = TRUE; 
                    break
                } 
            }     
            if(bSelect)
            {
                //clrNewTextColor = RGB(255,0,0);     //Set the text to red
                clrNewTextColor = RGB(02550);     //Set the text to red
                clrNewBkColor = RGB(00255);     //Set the bkgrnd color to blue
            }
            else
            {    
                clrNewTextColor = RGB(000);     //Leave the text black
                clrNewBkColor = RGB(255255255);    //leave the bkgrnd color white
            }

            pLVCD->clrText = clrNewTextColor;
            pLVCD->clrTextBk = clrNewBkColor;


            // Tell Windows to paint the control itself.
            pResult = CDRF_DODEFAULT;

        }
    }
    这个比来斗劲常用,记录一下。
    须要源码的留下邮箱。
    url:http://greatverve.cnblogs.com/p/ctreectrl-clistctrl-highlight.html

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





    1. CListCtrl 风格


          LVS_ICON: 为每个item显示大图标
          LVS_SMALLICON: 为每个item显示小图标
          LVS_LIST: 显示一列带有小图标的item
          LVS_REPORT: 显示item具体材料


          直观的懂得:windows资料经管器,&#8220;查看&#8221;标签下的&#8220;大图标,小图标,列表,具体材料&#8221;







    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, &#8220;11&#8221;);//插入行
          m_list.SetItemText(nRow, 1, &#8220;jacky&#8221;);//设置数据


     





    4. 一向选中item

        选中style中的Show ion 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<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 ed!\n);
          else
          {
               while (pos)
               {
                    int nItem = m_list.GetNextSelectedItem(pos);
                    TRACE1(Item %d was ed!\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>=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->iItem != -1)
               {
                    CString strtemp;
                    strtemp.Format(单击的是第%d行第%d列,
                                    pNMListView->iItem, pNMListView->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->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->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->uOldState & LVIS_FOCUSED) == LVIS_FOCUSED && 
                (pNMListView->uNewState & LVIS_FOCUSED) == 0)
               {
                    sTemp.Format(%d losted focus,pNMListView->iItem);
               }
               else if((pNMListView->uOldState & LVIS_FOCUSED) == 0 &&
                   (pNMListView->uNewState & LVIS_FOCUSED) == LVIS_FOCUSED)
               {
                    sTemp.Format(%d got focus,pNMListView->iItem);
               } 
      
               if((pNMListView->uOldState & LVIS_SELECTED) == LVIS_SELECTED &&
                (pNMListView->uNewState & LVIS_SELECTED) == 0)
               {
                    sTemp.Format(%d losted ed,pNMListView->iItem);
               }
               else if((pNMListView->uOldState & LVIS_SELECTED) == 0 &&
                (pNMListView->uNewState & LVIS_SELECTED) == LVIS_SELECTED)
               {
                    sTemp.Format(%d got ed,pNMListView->iItem);
               }
        
               pResult = 0;
          }







    17. 获得另一个过程里的listctrl控件的item内容


    http://www.codeproject.com/threads/int64_memsteal.asp






    18. 选中listview中的item

    Q131284: How To Select a Listview Item Programmatically
    http://support.microsoft.com/kb/131284/en-us






    19. 如安在CListView中应用CListCtrl的派生类


    http://www.codeguru.com/cpp/controls/listview/introduction/article.php/c919/







    20. listctrl的subitem添加图标


          m_list.SetExtendedStyle(LVS_EX_SUBITEMIMAGES);
          m_list.SetItem(..); //具体参数请参考msdn


     





    21. 在CListCtrl显示文件,并按照文件类型来显示图标


          网上找到的代码,share
          BOOL CTest6Dlg::OnInitDialog()
          {
               CDialog::OnInitDialog();
       
               HIMAGELIST himlSmall;
               HIMAGELIST himlLarge;
               SHFILEINFO sfi;
               char  cSysDir[MAX_PATH];
               CString  strBuf;
      
               memset(cSysDir, 0, MAX_PATH);
       
               GetWindowsDirectory(cSysDir, MAX_PATH);
               strBuf = cSysDir;
               sprintf(cSysDir, %s, strBuf.Left(strBuf.Find(\\)+1));
      
               himlSmall = (HIMAGELIST)SHGetFileInfo ((LPCSTR)cSysDir,  
                          0,  
                          &sfi, 
                          sizeof(SHFILEINFO),  
                          SHGFI_SYSICONINDEX | SHGFI_SMALLICON );
       
               himlLarge = (HIMAGELIST)SHGetFileInfo((LPCSTR)cSysDir,  
                          0,  
                          &sfi,  
                          sizeof(SHFILEINFO),  
                          SHGFI_SYSICONINDEX | SHGFI_LARGEICON);
       
               if (himlSmall && himlLarge)
               {
                    ::SendMessage(m_list.m_hWnd, LVM_SETIMAGELIST,
                                 (WPARAM)LVSIL_SMALL, (LPARAM)himlSmall);
                    ::SendMessage(m_list.m_hWnd, LVM_SETIMAGELIST,
                                 (WPARAM)LVSIL_NORMAL, (LPARAM)himlLarge);
               }
               return TRUE;  // return TRUE  unless you set the focus to a control
          }
      
          void CTest6Dlg::AddFiles(LPCTSTR lpszFileName, BOOL bAddToDocument)
          {
               int nIcon = GetIconIndex(lpszFileName, FALSE, FALSE);
               CString strSize;
               CFileFind filefind;
      
               //  get file size
               if (filefind.FindFile(lpszFileName))
               {
                    filefind.FindNextFile();
                    strSize.Format(%d, filefind.GetLength());
               }
               else
                    strSize = 0;
       
               // split path and filename
               CString strFileName = lpszFileName;
               CString strPath;
      
               int nPos = strFileName.ReverseFind(\\);
               if (nPos != -1)
               {
                    strPath = strFileName.Left(nPos);
                    strFileName = strFileName.Mid(nPos + 1);
               }
       
               // to list
               int nItem = m_list.GetItemCount();
               m_list.InsertItem(nItem, strFileName, nIcon);
               m_list.SetItemText(nItem, 1, strSize);
               m_list.SetItemText(nItem, 2, strFileName.Right(3));
               m_list.SetItemText(nItem, 3, strPath);
          }
      
          int CTest6Dlg::GetIconIndex(LPCTSTR lpszPath, BOOL bIsDir, BOOL bSelected)
          {
               SHFILEINFO sfi;
               memset(&sfi, 0, sizeof(sfi));
       
               if (bIsDir)
               {
                SHGetFileInfo(lpszPath,  
                             FILE_ATTRIBUTE_DIRECTORY,  
                             &sfi,  
                             sizeof(sfi),  
                             SHGFI_SMALLICON | SHGFI_SYSICONINDEX |
                             SHGFI_USEFILEATTRIBUTES |(bSelected ? SHGFI_OPENICON : 0));  
                return  sfi.iIcon;
               }
               else
               {
                SHGetFileInfo (lpszPath,  
                             FILE_ATTRIBUTE_NORMAL,  
                             &sfi,  
                             sizeof(sfi),  
                             SHGFI_SMALLICON | SHGFI_SYSICONINDEX |  
                             SHGFI_USEFILEATTRIBUTES | (bSelected ? SHGFI_OPENICON : 0));
                return   sfi.iIcon;
               }
               return  -1;
          }







    22. listctrl内容进行大数据量更新时,避免闪烁


          m_list.SetRedraw(FALSE);
          //更新内容
          m_list.SetRedraw(TRUE);
          m_list.Invalidate();
          m_list.UpdateWindow();
      
    或者参考 


    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_cwnd.3a3a.setredraw.asp







    23. listctrl排序


    Q250614:How To Sort Items in a CListCtrl in Report View
    http://support.microsoft.com/kb/250614/en-us







    24. 在listctrl中选中某个item时动态改变其icon或bitmap

    Q141834: How to change the icon or the bitmap of a CListCtrl item in Visual C++
    http://support.microsoft.com/kb/141834/en-us






    25. 在添加item后,再InsertColumn()后导致整列数据移动的题目


    Q151897: CListCtrl::InsertColumn() Causes Column Data to Shift 
    http://support.microsoft.com/kb/151897/en-us







    26. 关于listctrl第一列始终居左的题目


    解决办法:把第一列当一个虚列,从第二列开端插入列及数据,最后删除第一列。
          
    具体申明参阅   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/listview/structures/lvcolumn.asp


     





    27. 锁定column header的拖动


    http://msdn.microsoft.com/msdnmag/issues/03/06/CQA/







    28. 如何隐蔽clistctrl的列


        把需隐蔽的列的宽度设为0,然后检测当该列为隐蔽列时,用上方第27点的锁定column 的拖动来实现





    29. listctrl进行大数据量操纵时,应用virtual list   


    http://www.codeguru.com/cpp/controls/listview/advanced/article.php/c4151/
    http://www.codeproject.com/listctrl/virtuallist.asp







    30. 关于item只能显示259个字符的题目


    解决办法:须要在item上放一个edit。







    31. 响应在listctrl的column header上的鼠标右键单击


    Q125694: How To Find Out Which Listview Column Was Right-Clicked
    http://support.microsoft.com/kb/125694/en-us







    32. 类似于windows资料经管器的listview


    Q234310: How to implement a ListView control that is similar to Windows Explorer by using DirLV.exe
    http://support.microsoft.com/kb/234310/en-us


     





    33. 在ListCtrl中OnTimer只响应两次的题目


    Q200054:
    PRB: OnTimer() Is Not Called Repeatedly for a List Control
    http://support.microsoft.com/kb/200054/en-us





    34. 以下为一些为实现各类自定义功能的listctrl派生类


              (1)    拖放        
                       http://www.codeproject.com/listctrl/dragtest.asp


                       在CListCtrl和CTreeCtrl间拖放
                       http://support.microsoft.com/kb/148738/en-us
      
              (2)    多功能listctrl
                       支撑subitem可编辑,图标,radiobutton,checkbox,字符串改变色彩的类
                       http://www.codeproject.com/listctrl/quicklist.asp
      
                       支撑排序,subitem可编辑,subitem图标,subitem改变色彩的类
                       http://www.codeproject.com/listctrl/ReportControl.asp


              (3)    subitem中显示超链接
                       http://www.codeproject.com/listctrl/CListCtrlLink.asp


              (4)    subitem的tooltip提示
                       http://www.codeproject.com/listctrl/ctooltiplistctrl.asp


              (5)    subitem中显示进度条    
                       http://www.codeproject.com/listctrl/ProgressListControl.asp
                       http://www.codeproject.com/listctrl/napster.asp
                       http://www.codeguru.com/Cpp/controls/listview/article.php/c4187/


              (6)    动态改变subitem的色彩和靠山色
                        http://www.codeproject.com/listctrl/highlightlistctrl.asp
                        http://www.codeguru.com/Cpp/controls/listbox/colorlistboxes/article.php/c4757/
     
              (7)    类vb属性对话框
                        http://www.codeproject.com/listctrl/propertylistctrl.asp
                        http://www.codeguru.com/Cpp/controls/listview/propertylists/article.php/c995/ 
                        http://www.codeguru.com/Cpp/controls/listview/propertylists/article.php/c1041/ 
      
              (8)    选中subitem(只高亮选中的item)
                        http://www.codeproject.com/listctrl/SubItemSel.asp
                        http://www.codeproject.com/listctrl/ListSubItSel.asp
      
              (9)    改变行高
                        http://www.codeproject.com/listctrl/changerowheight.asp
      
              (10)   改变行色彩
                        http://www.codeproject.com/listctrl/coloredlistctrl.asp
      
              (11)   可编辑subitem的listctrl
                        http://www.codeproject.com/listctrl/nirs2000.asp
                        http://www.codeproject.com/listctrl/editing_subitems_in_listcontrol.asp
      
              (12)   subitem可编辑,插入combobox,改变行色彩,subitem的tooltip提示
                        http://www.codeproject.com/listctrl/reusablelistcontrol.asp
      
              (13)   header 中允很多行字符串
                        http://www.codeproject.com/listctrl/headerctrlex.asp
      
              (14)   插入combobox
                        http://www.codeguru.com/Cpp/controls/listview/editingitemsandsubitem/article.php/c979/
      
              (15)   添加靠山
                        http://www.codeguru.com/Cpp/controls/listview/backgroundcolorandimage/article.php/c4173/
                        http://www.codeguru.com/Cpp/controls/listview/backgroundcolorandimage/article.php/c983/
                        http://www.vchelp.net/vchelp/archive.asp?type_id=9&class_id=1&cata_id=1&article_id=1088&search_term=
        
              (16)  自适应宽度的listctrl
                        http://www.codeproject.com/useritems/AutosizeListCtrl.asp

              (17)  改变ListCtrl高亮时的色彩(默认为蓝色)
                       处理惩罚 NM_CUSTOMDRAW 
               http://www.codeproject.com/listctrl/lvcustomdraw.asp
    我俩之间有着强烈的吸引力。短短几个小时后,我俩已经明白:我们的心是一个整体的两半,我俩的心灵是孪生兄妹,是知己。她让我感到更有活力,更完美,更幸福。即使她不在我身边,我依然还是感到幸福,因为她总是以这样或者那样的方式出现在我心头。——恩里克·巴里奥斯《爱的文明》
    分享到: