码迷,mamicode.com
首页 > Windows程序 > 详细

Win32 sdk 下树形控件响应鼠标单击与双击,获得选中项的名称

时间:2020-09-18 02:25:01      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:poi   param   tor   style   class   窗口   highlight   get   str   

//窗口过程函数

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中测试通过

技术图片

 

Win32 sdk 下树形控件响应鼠标单击与双击,获得选中项的名称

标签:poi   param   tor   style   class   窗口   highlight   get   str   

原文地址:https://www.cnblogs.com/greenleaf1976/p/13676559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!