2016-05-02 77 views

回答

1

Ran Wainstein實現了列表框控件中每個項目的工具提示。這也可以擴展到Check List Box控件。

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
這將只能用於Unicode字符串。

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

最後落實檢查列表框控件在相應的對話框box.The輸出

enter image description here