首先需要重写CtreeContrl,然后在.h中添加
afx_msg void OnDblClkTree(NMHDR* pNMHDR, LRESULT* pResult);//双击事件 afx_msg void OnClkTree(NMHDR* pNMHDR, LRESULT* pResult);//单击事件
.h文件
#pragma once ///////////////////////////////////////////////////////////////////////////// // CViewTree 窗口 class CViewTree : public CTreeCtrl { // 构造 public: CViewTree(); // 重写 protected: virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult); // 实现 public: virtual ~CViewTree(); afx_msg void OnDblClkTree(NMHDR* pNMHDR, LRESULT* pResult); afx_msg void OnClkTree(NMHDR* pNMHDR, LRESULT* pResult); protected: DECLARE_MESSAGE_MAP() };.cpp文件
#include "stdafx.h" #include "ViewTree.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CViewTree CViewTree::CViewTree() { } CViewTree::~CViewTree() { } BEGIN_MESSAGE_MAP(CViewTree, CTreeCtrl) ON_NOTIFY_REFLECT(NM_DBLCLK, &CViewTree::OnDblClkTree) ON_NOTIFY_REFLECT(NM_CLICK, &CViewTree::OnClkTree) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CViewTree 消息处理程序 BOOL CViewTree::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) { BOOL bRes = CTreeCtrl::OnNotify(wParam, lParam, pResult); NMHDR* pNMHDR = (NMHDR*)lParam; ASSERT(pNMHDR != NULL); if (pNMHDR && pNMHDR->code == TTN_SHOW && GetToolTips() != NULL) { GetToolTips()->SetWindowPos(&wndTop, -1, -1, -1, -1, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOSIZE); } return bRes; } void CViewTree::OnDblClkTree(NMHDR* pNMHDR, LRESULT* pResult) { HTREEITEM hTreeItem = GetSelectedItem(); //以GetItemText()函数为例: CString S1 = GetItemText(hTreeItem); AfxMessageBox("You have selected "+S1); *pResult = 0; } void CViewTree::OnClkTree(NMHDR* pNMHDR, LRESULT* pResult) { HTREEITEM hTreeItem = GetSelectedItem(); //以GetItemText()函数为例: CString S1 = GetItemText(hTreeItem); AfxMessageBox("You have selected "+S1); *pResult = 0; }