程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> VC++ CTreeCtrl 使用NM_CLICK和TVN_SELCHANGED,ctreectrlnm_click

VC++ CTreeCtrl 使用NM_CLICK和TVN_SELCHANGED,ctreectrlnm_click

編輯:C++入門知識

VC++ CTreeCtrl 使用NM_CLICK和TVN_SELCHANGED,ctreectrlnm_click


//這是當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;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved