VC/MFC 在ListCtl 控件中隨鼠標移動提示單元格信息


[cpp]  view plain copy
 
  1. BEGIN_MESSAGE_MAP(CTipListCtrl, CListCtrl)  
  2.     //{{AFX_MSG_MAP(CTipListCtrl)  
  3.     ON_WM_MOUSEMOVE()  
  4.     ON_WM_DESTROY()  
  5.     //}}AFX_MSG_MAP  
  6. END_MESSAGE_MAP()  
  7.   
  8. /////////////////////////////////////////////////////////////////////////////  
  9. // CTipListCtrl message handlers  
  10.   
  11. void CTipListCtrl::OnMouseMove(UINT nFlags, CPoint point)   
  12. {  
  13.     // TODO: Add your message handler code here and/or call default  
  14.   
  15.     if(m_bEnableTips)  
  16.     {  
  17.         CString str;  
  18.         LVHITTESTINFO lvhti;  
  19.           
  20.         // 判斷鼠標當前所在的位置(行, 列)  
  21.         lvhti.pt = point;     
  22.         SubItemHitTest(&lvhti);  
  23.           
  24.         // 如果鼠標移動到另一個單元格內, 則進行處理; 否則, 不做處理  
  25.         if((lvhti.iItem != m_nItem) || (lvhti.iSubItem != m_nSubItem))  
  26.         {  
  27.             // 保存當前鼠標所在的(行,列)  
  28.             m_nItem = lvhti.iItem;  
  29.             m_nSubItem = lvhti.iSubItem;  
  30.               
  31.             // 如果鼠標移動到一個合法的單元格內,則顯示新的提示信息  
  32.             // 否則, 不顯示提示  
  33.               
  34.             if((m_nItem != -1) && (m_nSubItem != -1))  
  35.             {  
  36.                 // @@@@@@@@ 在這里修改要顯示的提示信息  
  37.                 // 這里僅僅是一個例子---獲得當前單元格的文字信息, 並設置為新的提示信息  
  38.                 str = GetItemText(m_nItem ,m_nSubItem);               
  39.                 m_toolTip.AddTool(this, str);  
  40.                 // 顯示提示框  
  41.                 m_toolTip.Pop();  
  42.             }  
  43.             else  
  44.             {  
  45.                 m_toolTip.AddTool(this, _T("雙擊記錄可查看人員詳細信息並對其修改"));  
  46.                 // 顯示提示框  
  47.                 m_toolTip.Pop();  
  48.             }  
  49.               
  50.         }  
  51.     }  
  52.       
  53.     CListCtrl::OnMouseMove(nFlags, point);  
  54. }  
  55.   
  56. BOOL CTipListCtrl::PreTranslateMessage(MSG* pMsg)   
  57. {  
  58.     // TODO: Add your specialized code here and/or call the base class  
  59.     if(::IsWindow(m_toolTip.GetSafeHwnd()))  
  60.     {  
  61.         m_toolTip.RelayEvent(pMsg);  
  62.     }  
  63.       
  64.     return CListCtrl::PreTranslateMessage(pMsg);  
  65. }  
  66.   
  67. void CTipListCtrl::OnDestroy()   
  68. {  
  69.     CListCtrl::OnDestroy();  
  70.       
  71.     // TODO: Add your message handler code here  
  72.     // listctrl銷毀時, 同時銷毀 tooltipctrl  
  73.     m_toolTip.DestroyWindow();  
  74.     m_toolTip.Detach();  
  75. }  
  76.   
  77. BOOL CTipListCtrl::EnableTips()  
  78. {  
  79.     EnableToolTips(TRUE);  
  80.     // 創建tooltip控件  
  81.     m_bEnableTips = m_toolTip.Create(this, TTS_ALWAYSTIP);  
  82.       
  83.     if(m_bEnableTips)  
  84.     {  
  85.         m_toolTip.Activate(TRUE);  
  86.         m_toolTip.SetDelayTime(TTDT_INITIAL, 0);  
  87.     }  
  88.       
  89.     return m_bEnableTips;  
  90. }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM