2014-05-01 112 views
0

我可以填充樹節點但我無法獲取選定的項目。在通知部分,你會看到我正在嘗試調用TreeView_GetItem,但電視回來爲空。TreeView - 獲取選定的節點

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    int wmId, wmEvent; 
    PAINTSTRUCT ps; 
    HDC hdc; 
    NMHDR *nmptr; 
    HTREEITEM tvi; 

    switch (message) 
    { 

     case WM_CREATE: 
     {   
      RECT rc;    
      GetClientRect(hWnd, &rc); 
      INITCOMMONCONTROLSEX icex; 
      icex.dwSize = sizeof(INITCOMMONCONTROLSEX); 
      icex.dwICC = ICC_WIN95_CLASSES | ICC_BAR_CLASSES | ICC_DATE_CLASSES | ICC_LISTVIEW_CLASSES | ICC_TREEVIEW_CLASSES; 
      InitCommonControlsEx(&icex); 

      hTreeWin = CreateWindowEx(0, 
          WC_TREEVIEW, 
          TEXT("Tree View"), 
          WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES | TVS_HASBUTTONS |TVS_TRACKSELECT | TVS_LINESATROOT, 
          0, 
          0, 
          200,//rc.right, 
          rc.bottom, 
          hWnd, 
          (HMENU)IDM_TREE_WIN, 
          hInst, 
          NULL); 

      InitTreeViewImageLists(hTreeWin); 

     } 
     break; 
     case WM_NOTIFY: 
     { 
      LPNMHDR lpnmh = (LPNMHDR) lParam; 

      if(lpnmh->idFrom == IDM_TREE_WIN ) // if this is our treeview control 
      { 
       switch(lpnmh->code) // let us filter notifications 
       {  
        case TVN_SELCHANGED: 
         {      
          TVITEM tv; 
          tv.mask = TVIF_PARAM; 


          TreeView_GetItem(hTreeWin, &tv);  
          //tv is null 
          MessageBox(NULL,std::to_wstring(tv.lParam).c_str(), L"", MB_OK); 
         } 
         break; 
        default: 
         break; 
       } 
      } 
     } 
     break; 
     case WM_COMMAND: 
      wmId = LOWORD(wParam); 
      wmEvent = HIWORD(wParam); 
      // Parse the menu selections: 
      switch (wmId) 
      { 
      case IDM_ABOUT: 
       DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); 
       break; 
      case IDM_EXIT: 
       DestroyWindow(hWnd); 
       break; 
      case IDM_OPEN_PST: 
       TestPstStuff(); 
       break; 
      default: 
       return DefWindowProc(hWnd, message, wParam, lParam); 
      } 
      break; 
     default: 
      return DefWindowProc(hWnd, message, wParam, lParam); 
     } 
    return 0; 
} 

這裏是我如何填充樹視圖

HTREEITEM writeTreeView(const pstsdk::folder fi, HTREEITEM parentTV) 
{ 
     HTREEITEM hPrev = (HTREEITEM)TVI_FIRST; 
    try 
    { 
     TVITEM tvi; 
     TVINSERTSTRUCT tvins; 
     HTREEITEM hPrevRootItem = NULL; 
     HTREEITEM hPrevLev2Item = NULL; 
     HTREEITEM hti; 
     tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM; 

     std::wstring x = fi.get_name().c_str(); 

     tvi.pszText = (LPWSTR)x.c_str(); //text to display 
     tvi.cchTextMax = sizeof(tvi.pszText)/sizeof(tvi.pszText[0]); 

     tvi.lParam = (LPARAM)fi.get_id(); 
     tvins.item = tvi; 
     tvins.hInsertAfter = parentTV; 
     tvins.hParent = parentTV; 

     // Add the item to the tree-view control. 
     hPrev = (HTREEITEM)SendMessage(hTreeWin, TVM_INSERTITEM, 
      0, (LPARAM)(LPTVINSERTSTRUCT)&tvins); 

     if (fi.get_hierarchy_table().size() > 0) 
     { 
      for (pstsdk::folder::folder_iterator i = fi.sub_folder_begin(); i != fi.sub_folder_end(); ++i) 
      { 
       writeTreeView(*i, hPrev); 
      } 
     } 
    } 
    catch(exception e) 
    { 
     std::string _str = e.what();  
    } 
    return hPrev; 
} 
+1

「[T]他TVITEM或TVITEMEX結構的hItem成員標識要檢索信息的項目,而掩碼成員指定要檢索的屬性。「 ([MSDN](http://msdn.microsoft.com/en-us/library/windows/desktop/bb773841(V = vs.85)的.aspx)) – usr2564301

回答

1

lParam已經可用,當您收到TVN_SELCHANGED通知,所以您的代碼需要看起來像

case TVN_SELCHANGED: 
{ LPNMTREEVIEW pnmtv = (LPNMTREEVIEW) lParam; 
    MessageBox(NULL,std::to_wstring(pnmtv->itemNew.lParam).c_str(), L"", MB_OK); 
    break; 
}