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

Post date: 2012/4/24 上午 06:41:38

[cpp] view plaincopy

  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. TCHAR szText[MAX_COUNT] = {0};
  20. ti.item.mask = TVIF_TEXT;
  21. ti.item.cchTextMax = MAX_COUNT;
  22. ti.item.pszText = szText;
  23. _stprintf(szText, _T("%s"), _T("AAA"));
  24. TreeView_InsertItem(hTreeView, &ti);
  25. _stprintf(szText, _T("%s"), _T("BBB"));
  26. TreeView_InsertItem(hTreeView, &ti);
  27. _stprintf(szText, _T("%s"), _T("CCC"));
  28. TreeView_InsertItem(hTreeView, &ti);
  29. }
  30. }
  31. break;

[cpp] view plaincopy

  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;