问题:
如题,如何遍历寻找指定进程?如何通过进程ID获取窗口句柄?HWND与CWnd之间是如何转换的?
解决:
1、HWND与CWnd之间的转换:使用GetSafeHwnd和CWnd::FromHandle
// 已知 CWnd 获取 HWNDCWnd* pCWnd;HWND hwnd = pCWnd->m_hWnd;HWND hwnd1 = pCWnd->GetSafeHwnd();// 已知 HWND 获取 CWndHWND hwnd2;CWnd* pCwnd1 = CWnd::FromHandle(hwnd2);
2、如何遍历寻找指定进程:使用EnumWindows
// 这里以匹配进程名称 Test Process 为例::EnumWindows([](HWND hwnd, LPARAM lparam) ->BOOL {wchar_t winText[256];::GetWindowText(hwnd, winText, 255);CString winStr = winText;if (winStr.IsEmpty())return TRUE; //!< 继续遍历if(pare(_T("Test Process")) == 0)return FALSE;//结束遍历return TRUE; //!< 继续遍历}, NULL);
还可以加以判断是否是当前进程,寻找当前进程之外的指定进程:使用_getpid和GetWindowThreadProcessId
//......if(pare(_T("Test Process")) == 0){DWORD dwId = 0;GetWindowThreadProcessId(hwnd, &dwId);if (_getpid() == dwId)return TRUE;// 是当前进程,继续遍历return FALSE;//结束遍历}//......
3、获取指定进程ID的窗口句柄:使用CALLBACK、EnumWindows和struct
//自定义枚举进程参数typedef struct {HWNDhwnd;DWORDprocessId;}WindowsArg;//回调函数BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lparam){WindowsArg * pArg = (WindowsArg *)lparam;DWORD dwId = 0;GetWindowThreadProcessId(hwnd, &dwId);if (pArg.processId == dwId){pArg->hwnd = hwnd;return FALSE;//匹配进程ID成功,结束遍历}return TRUE; //!< 继续遍历}//通过进程ID获取窗口句柄HWND GetHwndByProcessId(DWORD processId){HWND hwndRet = NULL;WindowsArg wa;wa.processId = processId;wa.hwnd = NULL;EnumWindows(EnumWindowsProc, (LPARAM)&wa);if (wa.hwnd)hwndRet = wa.hwnd;return hwndRet ;}
如果觉得《C++ 遍历寻找指定进程 通过进程ID获取窗口句柄 以及HWND与CWnd之间转换》对你有帮助,请点赞、收藏,并留下你的观点哦!