通过进程获取窗口的句柄

通过 EnumWindows 枚举所有窗口的句柄,利用 GetWindowThreadProcessId 获取窗口句柄与查找的进程ID比较以获得查找进程的窗口的句柄。

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lparam)
{
  DWORD threadid;
  GetWindowThreadProcessId(hwnd,&threadid);
  ::SendMessage((HWND)lparam,WM_SHOWTEXT,(WPARAM)&strTitle,threadid);
  return TRUE;
}
void CTESTDlg::OnButton1()
{
  ::EnumWindows(EnumWindowsProc,(LPARAM)GetSafeHwnd());
}

DWORD CTESTDlg::FindAppProcessID(CString lFileName)
{
  HANDLE handle=::CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
  PROCESSENTRY32 Info;
  Info.dwSize = sizeof(PROCESSENTRY32);
  if(::Process32First(handle,&Info))
  {
    do{
      CString ss=Info.szExeFile;
      if(!ss.CompareNoCase(lFileName))
      {
        ::CloseHandle(handle);
        return Info.th32ProcessID;
      }
    }
    while(::Process32Next(handle,&Info));  
    ::CloseHandle(handle);
  }
  return -1;
}  

你可能感兴趣的:(通过进程获取窗口的句柄)