程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> CTreeCtrl和CListCtrl失去焦點時高亮選中項

CTreeCtrl和CListCtrl失去焦點時高亮選中項

編輯:C++入門知識


設置CTreeCtrl的Always Show Selection:True
CListCtrl的Always Show Selection:False
在NM_CUSTOMDRAW事件中添加如下代碼:  CHighLightDlg::OnNMCustomdrawTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
        *pResult = ;

    LPNMTVCUSTOMDRAW pDraw=(LPNMTVCUSTOMDRAW)pNMHDR;
    DWORD dwDrawStage=pDraw->nmcd.dwDrawStage;
    UINT uItemState =pDraw->nmcd.uItemState;
        *pResult=CDRF_NOTIFYITEMDRAW;
        CDC* pdc=CDC::FromHandle(pDraw->nmcd.hdc);
    CRect rc;
    HTREEITEM hItem=(HTREEITEM) pDraw->nmcd.dwItemSpec;
    m_tree.GetItemRect(hItem,&rc,TRUE);    CString txt=m_tree.GetItemText(hItem);
    ((dwDrawStage & CDDS_ITEM) && (uItemState & CDIS_SELECTED))
    {        pdc->FillSolidRect(&rc,RGB(,,));                pdc->SetTextColor(RGB(,,));        pdc->SetBkColor(RGB(,,));        
        CFont* pfnt=pdc->GetCurrentFont();
                pdc->TextOut(rc.left+,rc.top+,txt);
        pdc->SelectObject(pfnt);

        *pResult |= CDRF_SKIPDEFAULT;
            }
         {
        pdc->FillSolidRect(&rc, GetSysColor(COLOR_WINDOW));
        pdc->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
        pdc->TextOut(rc.left+, rc.top+, txt);
            }
}

 CHighLightDlg::OnNMCustomdrawList2(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
        *pResult = ;

        
    NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );

        *pResult = CDRF_DODEFAULT;
             ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
    {
        *pResult = CDRF_NOTIFYITEMDRAW;
    }
      ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
    {
                
        *pResult = CDRF_NOTIFYSUBITEMDRAW;
    }
      ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
    {

        COLORREF clrNewTextColor, clrNewBkColor;

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

        BOOL bSelect = FALSE;
        POSITION pos =m_lst.GetFirstSelectedItemPosition(); 
        (pos) 
        { 
             index = m_lst.GetNextSelectedItem(pos); 
            (index==nItem) 
            { 
                bSelect = TRUE; 
                ; 
            } 
        }     
        (bSelect)
        {
                        clrNewTextColor = RGB(,,);                 clrNewBkColor = RGB(,,);             }
        
        {    
            clrNewTextColor = RGB(,,);                 clrNewBkColor = RGB(,,);            }

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


                *pResult = CDRF_DODEFAULT;

    }
}這個最近比較常用,記錄一下。
需要源碼的留下郵箱。
url:http://greatverve.cnblogs.com/p/ctreectrl-clistctrl-highlight.html


      LVS_SMALLICON: 為每個item顯示小圖標
      LVS_LIST: 顯示一列帶有小圖標的item
      LVS_REPORT: 顯示item詳細資料


      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


      m_list.InsertColumn( 1, "NAME", LVCFMT_LEFT, 50 );
      int nRow = m_list.InsertItem(0, “11”);//插入行
      m_list.SetItemText(nRow, 1, “jacky”);//設置數據


 

    //選中
    m_list.SetItemState(nIndex, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
    //取消選中
    m_list.SetItemState(nIndex, 0, LVIS_SELECTED|LVIS_FOCUSED);
 


      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);
           }
      }


      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 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
           }
      }


      LVITEM lvi;
      lvi.iItem = nItemIndex;
      lvi.iSubItem = 0;
      lvi.mask = LVIF_TEXT;
      lvi.pszText = szBuf;
      lvi.cchTextMax = 1024;
      m_list.GetItem(&lvi);

      Q173242: Use Masks to Set/Get Item States in CListCtrl
               http://support.microsoft.com/kb/173242/en-us


      char  str[256];
      int   nColNum;
      CString  strColumnName[4];//假如有4列

      lvcol.mask = LVCF_TEXT;
      lvcol.pszText = str;
      lvcol.cchTextMax = 256;
      while(m_list.GetColumn(nColNum, &lvcol))
      { 
           strColumnName[nColNum] = lvcol.pszText;
           nColNum++;
      }




         while ( m_list.DeleteColumn (0))
       因為你刪除了第一列後,後面的列會依次向上移動。

      int nColumns = 4;
      for (int i=nColumns-1; i>=0; i--)
          m_list.DeleteColumn (i);


      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;
      }


      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;
      }


      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;
  }


      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 selected",pNMListView->iItem);
           }
           else if((pNMListView->uOldState & LVIS_SELECTED) == 0 &&
            (pNMListView->uNewState & LVIS_SELECTED) == LVIS_SELECTED)
           {
                sTemp.Format("%d got selected",pNMListView->iItem);
           }
    
           *pResult = 0;
      }


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


      m_list.SetItem(..); //具體參數請參考msdn


      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);
           }
   
           // insert 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;
      }


      //更新內容
      m_list.SetRedraw(TRUE);
      m_list.Invalidate();
      m_list.UpdateWindow();
  
或者參考 


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



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


      
具體解釋參閱   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/listview/structures/lvcolumn.asp





http://www.codeproject.com/listctrl/virtuallist.asp



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


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


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


                   http://www.codeproject.com/listctrl/dragtest.asp

                   http://support.microsoft.com/kb/148738/en-us
  
          (2)    
                   支持subitem可編輯,圖標,radiobutton,checkbox,字符串改變顏色的類
                   http://www.codeproject.com/listctrl/quicklist.asp
  
                   
                   http://www.codeproject.com/listctrl/ReportControl.asp

                   http://www.codeproject.com/listctrl/CListCtrlLink.asp

                   http://www.codeproject.com/listctrl/ctooltiplistctrl.asp

                   http://www.codeproject.com/listctrl/ProgressListControl.asp
                   http://www.codeproject.com/listctrl/napster.asp
                   http://www.codeguru.com/Cpp/controls/listview/article.php/c4187/

                    http://www.codeproject.com/listctrl/highlightlistctrl.asp
                    http://www.codeguru.com/Cpp/controls/listbox/colorlistboxes/article.php/c4757/
 
          (7)    
                    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)    
                    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)   
                    http://www.codeproject.com/listctrl/nirs2000.asp
                    http://www.codeproject.com/listctrl/editing_subitems_in_listcontrol.asp
  
          (12)   
                    http://www.codeproject.com/listctrl/reusablelistcontrol.asp
  
          (13)   
                    http://www.codeproject.com/listctrl/headerctrlex.asp
  
          (14)   
                    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)  
                    http://www.codeproject.com/useritems/AutosizeListCtrl.asp

 
           http://www.codeproject.com/listctrl/lvcustomdraw.asp

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved