• VC++ CTreeCtrl 使用NM_CLICK和TVN_SELCHANGED


    //这是当CTREECTRL控件点击时NM_CLICK的处理函数
    void CDriverSelCtrl::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult)
    {
        CPoint curPoint;
        UINT nFlags;
        HTREEITEM hItem;
    
        GetCursorPos(&curPoint); //当前点坐标
        ScreenToClient(&curPoint); //屏幕坐标转换为TREE内坐标
        hItem = HitTest(curPoint, &nFlags); //坐标是否有ITEM
    
        if (hItem && (TVHT_ONITEM & nFlags)) //判断是否有HTREEITEM
        {
            this->SelectItem(hItem);//在这里处理点击后的结果
        }
    // TODO: 在此添加控件通知处理程序代码 *pResult = 0; }

    在这里需要指出 HitTest 函数不但可以检测出是否在item上 也可以检测出 位于 item项的 哪个位置请看 MSDN的说明

    ValueMeaning
    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.

    由此可以更加灵活的使用的CTREECTL控件了

    例如下面 例子:

    void CDriverSelCtrl::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult)
    {
        CPoint point;
        UINT uFlag;
        BOOL bCheck;
    
    
        GetCursorPos(&point);
        CTreeCtrl::ScreenToClient(&point);
        HTREEITEM hTree = CTreeCtrl::HitTest(point, &uFlag);
    
    if (hTree && (TVHT_ONITEMSTATEICON & uFlag))
        {
            CTreeCtrl::SelectItem(hTree);
            bCheck = CTreeCtrl::GetCheck(hTree);
    
            
        }
    
    
        // TODO:  在此添加控件通知处理程序代码
        *pResult = 0;
    }

     下面是关于 选项改变时的函数

    //这是当控件的选择发生变化时的处理函数
    void CDriverSelCtrl::OnTvnSelchanged(NMHDR *pNMHDR, LRESULT *pResult)
    {    
    
    LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
    pNMTreeView->itemNew //选中HTREEITEM
    pNMTreeView->itemOld //上次选中HTREEITEM
    // TODO:  在此添加控件通知处理程序代码
    *pResult = 0;
    }
  • 相关阅读:
    PHP + JQuery 实现多图上传并预览
    推荐40个专业的CMS下载
    10 个有用的 PHP 代码
    2012年最值得关注最具颠覆性的10个创业公司
    在线生成 CSS3 的工具
    循环显示/隐藏图片
    100个推荐的图片/内容滑动条
    jQuery 下拉菜单插件
    20个网站测试工具
    20个免费在线生成网站
  • 原文地址:https://www.cnblogs.com/lujin49/p/4771765.html
Copyright © 2020-2023  润新知