//窗口过程函数
INT_PTR CALLBACK myWin::myWinDlgProc(HWND dlgHwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_COMMAND: { OnCommand(wParam, lParam); return 1; } case WM_ACTIVATE: if (WA_INACTIVE != LOWORD(wParam)) { SetFocus(dlgHwnd); } return TRUE; case WM_CLOSE: DestroyWindow(dlgHwnd); return 1; case WM_DESTROY: PostQuitMessage(0); return 0; case WM_INITDIALOG: win_hWnd = dlgHwnd; OnInitDialog(); return 1; case WM_CONTEXTMENU: OnContextMenu(wParam); return 1; case WM_NOTIFY: OnNotify(wParam,lParam);//树形控件返射消息 return 1; } return 0; }
BOOL MyAppClass::OnNotify(WPARAM wParam, LPARAM lParam) { LPNMHDR lphr = (LPNMHDR)lParam; if (lphr->hwndFrom == hTree) //判断是否是树形控件发来的消息 {
switch (lphr->code)
{
case NM_CLICK: //鼠标单击
OnCloickTree(lphr);
break;
case NM_DBLCLK: //鼠标双击
OnRCloickTree(lphr);
break;
}
} return TRUE; }
//鼠标双击消息
void MyAppClass::OnRCloickTree(LPNMHDR lphr) { HTREEITEM htItem; TVITEM tvi; htItem = TreeView_GetSelection(hTree); if (htItem == NULL) return; TCHAR szText[20]; memset(&tvi, 0, sizeof(tvi)); tvi.mask = TVIF_TEXT | TVIF_PARAM; tvi.hItem = htItem; tvi.pszText = szText; tvi.cchTextMax = sizeof(szText); TreeView_GetItem(hTree, &tvi); SetDlgItemText(win_hWnd, IDC_EDIT1, szText); }
我一直用这写在单击消息中,老是得到前一个选中项的数据
弄了好几天,查了好多资料都不行,连MFC我的复制过来还是不行,晕死了.
最后在网上找了好久才找到要单击要用鼠标坐标来判断,用TreeView_HitTest()宏,
这他妈的从哪知道呀.
void MyAppClass::OnCloickTree(LPNMHDR param1) { TVHITTESTINFO thti; HTREEITEM htItem; TVITEM tvi; POINT point; GetCursorPos(&point); ScreenToClient(hTree, &point); thti.pt = point; thti.flags = TVHT_TORIGHT; htItem = TreeView_HitTest(hTree, &thti); if (htItem == NULL) return; TCHAR szText[20]; memset(&tvi, 0, sizeof(tvi)); tvi.mask = TVIF_TEXT | TVIF_PARAM; tvi.hItem = htItem; tvi.pszText = szText; tvi.cchTextMax = sizeof(szText); TreeView_GetItem(hTree, &tvi); SetDlgItemText(win_hWnd, IDC_EDIT1, szText); }
以上都在vs2017中测试通过