Search code examples
visual-c++mfcmfc-feature-pack

Tool tip for disabled check boxes in Check List Box control in MFC


I am working in an MFC windows application. I am using check boxes in Check List Box control. Some of the check boxes are disabled. How can I implement the tool tips for disabled check boxes?


Solution

  • Ran Wainstein was implemented the tool tip for each item in the list box control. This can be extended to the Check List Box control also.

    MyCheckListBox.h

    class CMyCheckListBox : public CCheckListBox
    {
        DECLARE_DYNAMIC(CMyCheckListBox)
    
    public:
      CMyCheckListBox(){};
      virtual ~CMyCheckListBox(){};
      afx_msg int OnToolHitTest(CPoint point, TOOLINFO * pTI) const;
      UINT ItemFromPoint2(CPoint pt, BOOL& bOutside) const;
      BOOL OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult );
    
    protected:
      virtual void PreSubclassWindow();
      DECLARE_MESSAGE_MAP()
    };   
    

    MyCheckListBox.cpp
    This will work only for Unicode strings.

    IMPLEMENT_DYNAMIC(CMyCheckListBox, CCheckListBox)
    
    BEGIN_MESSAGE_MAP(CMyCheckListBox, CCheckListBox)
      ON_NOTIFY_EX_RANGE(TTN_NEEDTEXT, 0, 0xFFFF, OnToolTipText)
    END_MESSAGE_MAP()
    
    void CMyCheckListBox::PreSubclassWindow() {
        CCheckListBox::PreSubclassWindow();
        EnableToolTips(TRUE);
    }
    int CMyCheckListBox::OnToolHitTest(CPoint point, TOOLINFO * pTI) const{
        int row;
        RECT cellrect;  
        BOOL tmp = FALSE;
        row  = ItemFromPoint(point,tmp);  
        if ( row == -1 ) 
            return -1;
        GetItemRect(row,&cellrect);
        pTI->rect = cellrect;
        pTI->hwnd = m_hWnd;
        pTI->uId = (UINT)((row));  
        pTI->lpszText = LPSTR_TEXTCALLBACK;
        return pTI->uId;
    }
    BOOL CMyCheckListBox::OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult ){
        TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
        CString strTipText;
        UINT nID = pNMHDR->idFrom;
    
        GetText( nID ,strTipText);
        lstrcpyn(pTTTW->szText, strTipText, 80);
    
        *pResult = 0;
      return TRUE;    
    }
    UINT CMyCheckListBox::ItemFromPoint2(CPoint pt, BOOL& bOutside) const{
        int nFirstIndex, nLastIndex;
        nFirstIndex = GetTopIndex();
        nLastIndex = nFirstIndex  + GetCount(); 
        bOutside = TRUE;
        CRect Rect;
        int nResult = -1;
        for (int i = nFirstIndex; nResult == -1 && i <= nLastIndex; i++){
            if (GetItemRect(i, &Rect) != LB_ERR){
                if (Rect.PtInRect(pt)){
                    nResult  = i;
                    bOutside = FALSE;
                }
            }   
        }
        return nResult;
    }  
    

    Finally implement Check List Box control in the corresponding dialog box.The output is

    enter image description here