如何判断鼠标点击在树控件的图标上

解决方案 »

  1.   

    BOOL CFormViewRestore::PreTranslateMessage(MSG* pMsg) 
    {
    //---------------------2002.8.30 by yang -----------------------
    if(pMsg->hwnd == m_tree.m_hWnd && pMsg->message == WM_LBUTTONDOWN)
    {
    RECT rc;
    m_tree.GetWindowRect(&rc);
    pMsg->pt.x -= rc.left;
    pMsg->pt.y -= rc.top;

    unsigned flag;
    HTREEITEM SelItem = m_tree.HitTest(pMsg->pt,&flag);
    if(SelItem != NULL && flag == TVHT_ONITEMICON)
    {
    HTREEITEM SubItem = m_tree.GetChildItem(SelItem);

    int nImage,nSelectedImage;
    m_tree.GetItemImage(SelItem,nImage,nSelectedImage); if(nImage == 1 && SubItem != NULL)
    {
    m_tree.SetItemImage(SelItem,0,0);
    SetAllItemChecked(SelItem,FALSE);

    HTREEITEM preItem;
    preItem = m_tree.GetParentItem(SelItem);
    while(preItem)
    {
    m_tree.SetItemImage(preItem,2,2);
    preItem = m_tree.GetParentItem(preItem);
    }
    }
    if(nImage == 1 && SubItem == NULL)
    {
    m_tree.SetItemImage(SelItem,0,0);

    HTREEITEM preItem;
    preItem = m_tree.GetParentItem(SelItem);
    while(preItem)
    {
    m_tree.SetItemImage(preItem,2,2);
    preItem = m_tree.GetParentItem(preItem);
    }
    }
    if(nImage == 0 && SubItem != NULL)
    {
    m_tree.SetItemImage(SelItem,1,1);
    SetAllItemChecked(SelItem,TRUE); HTREEITEM preItem;
    preItem = m_tree.GetParentItem(SelItem);
    while(preItem)
    {
    m_tree.SetItemImage(preItem,2,2);
    preItem = m_tree.GetParentItem(preItem);
    }
    }
    if(nImage == 0 && SubItem == NULL)
    {
    m_tree.SetItemImage(SelItem,1,1);
    HTREEITEM preItem;
    preItem = m_tree.GetParentItem(SelItem);
    while(preItem)
    {
    m_tree.SetItemImage(preItem,2,2);
    preItem = m_tree.GetParentItem(preItem);
    }
    }
    if(nImage == 2)
    {
    m_tree.SetItemImage(SelItem,1,1);
    SetAllItemChecked(SelItem,TRUE);
    } m_tree.SelectItem(SelItem);

    }

    } //---------------------------------------------------------------------
    return CFormView::PreTranslateMessage(pMsg);
    }
    主要用CTreeCtrl的HitTest函数
    他的flag表示击中的位置:TVHT_ABOVE  Above the client area.  
    TVHT_BELOW  Below the client area.  
    TVHT_NOWHERE  In the client area, but below the last item.  
    TVHT_ONITEM  On the bitmap or label associated with an item.  
    TVHT_ONITEMBUTTON  On the button associated with an item.  
    TVHT_ONITEMICON  On the bitmap associated with an item.  
    TVHT_ONITEMINDENT  In the indentation associated with an item.  
    TVHT_ONITEMLABEL  On the label (string) associated with an item.  
    TVHT_ONITEMRIGHT  In the area to the right of an item.  
    TVHT_ONITEMSTATEICON  On the state icon for a tree view item that is in a user-defined state.  
    TVHT_TOLEFT  To the left of the client area.  
    TVHT_TORIGHT  To the right of the client area.  我以前写的给你参考一下^-^
      

  2.   

    Leoparder(狩猎者)说得太好了..
    怎么不结贴?