none
给TREEVIEW发送消息失败 RRS feed

  • 问题

  •  HWND hWnd = ::FindWindow(_T("#32770"), _T("下载数据")); 
     if(hWnd)
     {
      HWND hButton = ::FindWindowEx(hWnd, NULL, NULL, _T("Tree1"));
      if(hButton)
      {
       NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)hButton;
        HTREEITEM hItem = (HTREEITEM)::SendMessage(hButton,TVM_GETNEXTITEM,TVGN_ROOT,0x0);
       
         ::SendMessage(hButton, TVM_SELECTITEM, TVGN_CARET, (LPARAM)&hItem);
    2011年5月14日 2:46

答案

  • // 模拟向其它进程窗口中的CTreeCtrl控件中的选中的节点项发送右键点击消息
    HWND hWnd = ::FindWindow(NULL, _T("XX"));
     if(hWnd)
     {
      HWND hTreeView = ::FindWindowEx(hWnd, NULL, _T("SysTreeView32"), NULL);
      if(hTreeView)
      {
       DWORD pid = 0;
       ::GetWindowThreadProcessId(hTreeView, &pid);
       HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
       if(hProcess)
       {
        HTREEITEM hItem = TreeView_GetSelection(hTreeView); // 得到当前CTreeCtrl控件selected HTREEITEM

        RECT rc = {0};
        
        RECT* pRect = NULL;
        pRect = (RECT*)::VirtualAllocEx(hProcess, NULL, sizeof(RECT), MEM_COMMIT, PAGE_READWRITE);
        ASSERT(pRect);    
        DWORD dwWrite = 0;
        DWORD dwRead = 0;
        *(HTREEITEM*)&rc = hItem;
        ::WriteProcessMemory(hProcess, pRect, &rc, sizeof(RECT), &dwRead);
        LRESULT lResult = ::SendMessage(hTreeView, TVM_GETITEMRECT, FALSE, (LPARAM)pRect);
        BOOL bRet = ::ReadProcessMemory(hProcess, pRect, &rc, sizeof(RECT), &dwRead);

        POINT pt = {0};
        pt.x = rc.left + (rc.right - rc.left) / 2;
        pt.y = rc.top + (rc.bottom - rc.top) / 2;

        ::PostMessage(hTreeView, WM_RBUTTONDOWN, MK_RBUTTON, MAKELPARAM(pt.x, pt.y)); // 向CTreeCtrl发送WM_RBUTTONDOWN右键点击消息
        ::VirtualFreeEx(hProcess, pRect, sizeof(RECT), MEM_RELEASE);
        ::CloseHandle(hProcess);
       }
      }
     }


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    • 已标记为答案 Honny_yeyh 2011年5月16日 4:44
    2011年5月14日 17:58
    版主

全部回复

  • 跨进程?您是要给指定的窗口中的CTreeCtrl控件上某个指定的HTREEITEM模拟右键点击事件吗?
    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    2011年5月14日 15:52
    版主
  • // 模拟向其它进程窗口中的CTreeCtrl控件中的选中的节点项发送右键点击消息
    HWND hWnd = ::FindWindow(NULL, _T("XX"));
     if(hWnd)
     {
      HWND hTreeView = ::FindWindowEx(hWnd, NULL, _T("SysTreeView32"), NULL);
      if(hTreeView)
      {
       DWORD pid = 0;
       ::GetWindowThreadProcessId(hTreeView, &pid);
       HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
       if(hProcess)
       {
        HTREEITEM hItem = TreeView_GetSelection(hTreeView); // 得到当前CTreeCtrl控件selected HTREEITEM

        RECT rc = {0};
        
        RECT* pRect = NULL;
        pRect = (RECT*)::VirtualAllocEx(hProcess, NULL, sizeof(RECT), MEM_COMMIT, PAGE_READWRITE);
        ASSERT(pRect);    
        DWORD dwWrite = 0;
        DWORD dwRead = 0;
        *(HTREEITEM*)&rc = hItem;
        ::WriteProcessMemory(hProcess, pRect, &rc, sizeof(RECT), &dwRead);
        LRESULT lResult = ::SendMessage(hTreeView, TVM_GETITEMRECT, FALSE, (LPARAM)pRect);
        BOOL bRet = ::ReadProcessMemory(hProcess, pRect, &rc, sizeof(RECT), &dwRead);

        POINT pt = {0};
        pt.x = rc.left + (rc.right - rc.left) / 2;
        pt.y = rc.top + (rc.bottom - rc.top) / 2;

        ::PostMessage(hTreeView, WM_RBUTTONDOWN, MK_RBUTTON, MAKELPARAM(pt.x, pt.y)); // 向CTreeCtrl发送WM_RBUTTONDOWN右键点击消息
        ::VirtualFreeEx(hProcess, pRect, sizeof(RECT), MEM_RELEASE);
        ::CloseHandle(hProcess);
       }
      }
     }


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    • 已标记为答案 Honny_yeyh 2011年5月16日 4:44
    2011年5月14日 17:58
    版主