获取ctreectrl选项的值全错,chatgpt回答正确
/* DWORD dwpos = GetMessagePos(); TVHITTESTINFO ht = {0}; ht.pt.x = GET_X_LPARAM(dwpos); ht.pt.y = GET_Y_LPARAM(dwpos); ::MapWindowPoints(HWND_DESKTOP,pNMHDR->hwndFrom,&ht.pt,1); //把屏幕坐标转换成控件坐标 TreeView_HitTest(pNMHDR->hwndFrom,&ht); //确定点击的是哪一项 */ TVHITTESTINFO HitTestInfo; ZeroMemory(&HitTestInfo, sizeof(HitTestInfo)); HitTestInfo.flags = TVHT_ONITEM; GetCursorPos(&HitTestInfo.pt); ScreenToClient(&HitTestInfo.pt); TreeView_HitTest(pNMHDR->hwndFrom, &HitTestInfo); CString strOpcServerName = m_treectrl1.GetItemText(HitTestInfo.hItem); ::MessageBox(NULL, strOpcServerName, NULL, MB_ICONWARNING); /* // TODO: 在此添加控件通知处理程序代码 LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR); DWORD dwpos = GetMessagePos(); TVHITTESTINFO ht = {0}; ht.pt.x = GET_X_LPARAM(dwpos); ht.pt.y = GET_Y_LPARAM(dwpos); ::MapWindowPoints(HWND_DESKTOP,pNMHDR->hwndFrom,&ht.pt,1); //把屏幕坐标转换成控件坐标 HTREEITEM hItem = m_treectrl1.HitTest(ht.pt); if (hItem) { CString strOpcServerName = m_treectrl1.GetItemText(hItem); int nID = m_treectrl1.GetItemData(hItem); CString strID; strID.Format(L"ID = %d \r\n" , nID); } */
void CYourDialog::OnTreeCtrlDoubleClick(NMHDR* pNMHDR, LRESULT* pResult) { // 获取 NM_TREEVIEW 消息的结构体 NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; // 获取选中项的句柄 HTREEITEM hSelectedItem = m_treeCtrl.GetSelectedItem(); // 获取选中项的文本 CString selectedText = m_treeCtrl.GetItemText(hSelectedItem); // 在此处可以使用选中项的值进行后续操作 // ... *pResult = 0; }