windows 编程

创建TreeView控件,响应NM_CLICK消息得到当前点击的节点的信息(SDK版本)

分类: VC/MFC Development 493人阅读 评论(0) 收藏 举报
 
[cpp] view plain copy
  1. // 创建TreeView控件  
  2. case WM_CREATE:  
  3.         {  
  4.             INITCOMMONCONTROLSEX icc = {sizeof(icc), ICC_TREEVIEW_CLASSES};  
  5.             if(InitCommonControlsEx(&icc))  
  6.             {  
  7.                 HWND hTreeView = CreateWindow  
  8.                     (  
  9.                     _T("SysTreeView32"),  
  10.                     _T("TreeView"),  
  11.                     WS_CHILD | WS_VISIBLE | WS_BORDER | TVS_FULLROWSELECT | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT,  
  12.                     0, 0, 100, 100,   
  13.                     hWnd,   
  14.                     (HMENU)0x11,  
  15.                     hInst,  
  16.                     NULL  
  17.                     );  
  18.                 TVINSERTSTRUCT ti = {0};  
  19.   
  20.                 TCHAR szText[MAX_COUNT] = {0};  
  21.                 ti.item.mask = TVIF_TEXT;  
  22.                 ti.item.cchTextMax = MAX_COUNT;  
  23.                 ti.item.pszText = szText;  
  24.                 _stprintf(szText, _T("%s"), _T("AAA"));  
  25.                 TreeView_InsertItem(hTreeView, &ti);  
  26.                 _stprintf(szText, _T("%s"), _T("BBB"));  
  27.                 TreeView_InsertItem(hTreeView, &ti);  
  28.                 _stprintf(szText, _T("%s"), _T("CCC"));  
  29.                 TreeView_InsertItem(hTreeView, &ti);  
  30.             }             
  31.         }  
  32.         break;  
[cpp] view plain copy
  1. // 响应NM_CLICK的WM_NOTIFY消息  
  2. case WM_NOTIFY:  
  3.         {  
  4.             LPNMHDR lpnmh = (LPNMHDR)lParam;  
  5.             if(NM_CLICK == lpnmh->code)  
  6.             {  
  7.                 DWORD dwPos = GetMessagePos();  
  8.                 POINT pt;  
  9.                 pt.x = LOWORD(dwPos);  
  10.                 pt.y = HIWORD(dwPos);  
  11.                 ScreenToClient(lpnmh->hwndFrom, &pt);  
  12.                 TVHITTESTINFO ht = {0};  
  13.                 ht.pt = pt;  
  14.                 ht.flags = TVHT_ONITEM;  
  15.                 HTREEITEM hItem = TreeView_HitTest(lpnmh->hwndFrom, &ht);  
  16.                 TVITEM ti = {0};  
  17.                 ti.mask = TVIF_HANDLE | TVIF_TEXT;  
  18.                 TCHAR buf[MAX_COUNT] = {0};  
  19.                 ti.cchTextMax = MAX_COUNT;  
  20.                 ti.pszText = buf;  
  21.                 ti.hItem = hItem;  
  22.                 TreeView_GetItem(lpnmh->hwndFrom, &ti);  
  23.                 MessageBox(hWnd, buf, NULL, 0);  
  24.             }  
  25.         }  
  26.         break

你可能感兴趣的:(development,vc/mfc)