简体   繁体   中英

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?

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM