C++程序利用Windows API获取GUI程序的主窗口

2 篇文章 0 订阅
2 篇文章 0 订阅

首先要根据进程名称获取进程的pid:

int ProcessUtilities::lookupProcesses(const std::string &procName, std::vector<PROCESSENTRY32> &pes)
{
   string lowProcName = procName;
   StringUtility::lower(lowProcName);
   vector<PROCESSENTRY32> totalEntries;
   int errorCode = snapshotProcesses(totalEntries);
   if (errorCode == NO_ERROR)
   {
      pes.clear();
      for(const PROCESSENTRY32& entry : totalEntries)
      {
         string execName = entry.szExeFile;
         StringUtility::lower(execName);
         if (lowProcName.compare(execName) == 0)
         {
            pes.push_back(entry);
         }
      }
   }
   return errorCode;
}

int ProcessUtilities::getPids(const std::string &procName, std::vector<DWORD> &pids)
{
   vector<PROCESSENTRY32> entries;
   int errorCode = lookupProcesses(procName, entries);
   if (errorCode == NO_ERROR)
   {
      for(PROCESSENTRY32 entry : entries)
      {
         pids.push_back(entry.th32ProcessID);
      }
   }
   return errorCode;
}

上述代码里之所以用vector来存放pid,是因为同名进程可能会有多个实例,每个实例会有自己的pid,需要找出进程的所有实例。

找到进程的所有实例pid之后,对每个pid,找到其所有的窗口:

struct WindowInformation
{
   public:
      HWND handle;
      HWND parent;
      HWND owner;
      LONG style;
      DWORD pid;  // process id
      DWORD tid;  // creator thread id
      char title[MAX_TITLE_LENGTH];
      char cls[MAX_CLASS_LENGTH];      // window class name.

   public:
      std::string& toString(int level = 0);

   private:
      std::string text;
};

struct WindowTree
{
   public:
      WindowInformation info;
      std::vector<WindowTree> children;

   public:
      std::string& toString(int level = 0);

   private:
      std::string text;
};

void WindowUtilities::getWindowInformation(const HWND hWnd, WindowUtilities::WindowInformation& windInfo)
{
   windInfo.handle = hWnd;
   windInfo.parent = GetParent(hWnd);
   windInfo.owner = GetWindow(hWnd, GW_OWNER);
   windInfo.style = GetWindowLong(hWnd, GWL_STYLE);
   GetWindowText(hWnd, windInfo.title, WindowUtilities::MAX_TITLE_LENGTH);
   GetClassName(hWnd, windInfo.cls, WindowUtilities::MAX_CLASS_LENGTH);
   windInfo.tid = GetWindowThreadProcessId(hWnd, &windInfo.pid);
}

bool WindowUtilities::enumWindowsProc(const HWND hWnd, LPARAM lParam)
{
   WindowUtilities::WindowTree thisWindow;
   getWindowInformation(hWnd, thisWindow.info);
   WindowUtilities::WindowTree *winTreeRoot = (WindowUtilities::WindowTree *)lParam;
   EnumChildWindows(hWnd, (WNDENUMPROC)enumChildProc, (LPARAM)(&thisWindow));
   winTreeRoot->children.push_back(thisWindow);

   return true;
}

int WindowUtilities::getWindows(WindowTree& windTree)
{
   getWindowInformation(GetDesktopWindow(), windTree.info);
   EnumWindows((WNDENUMPROC)enumWindowsProc, (LPARAM)(&windTree));

   return NO_ERROR;
}

int WindowUtilities::filterWindows(WindowUtilities::WindowTree& totalTree, DWORD pid, vector<WindowUtilities::WindowTree>& windTree)
{
   if (totalTree.info.pid == pid)
   {
      windTree.push_back(totalTree);
   }
   else
   {
      for(WindowUtilities::WindowTree wt : totalTree.children)
      {
         filterWindows(wt, pid, windTree);
      }
   }

   return windTree.size();
}

int WindowUtilities::getWindows(DWORD pid, vector<WindowUtilities::WindowTree>& windTree)
{
   WindowTree totalTree;
   getWindows(totalTree);
   filterWindows(totalTree, pid, windTree);
   return 0;
}

int WindowUtilities::getWindows(DWORD pid, std::vector<HWND> &hWnds)
{
   vector<WindowUtilities::WindowTree> wts;
   getWindows(pid, wts);
   hWnds.clear();
   for(WindowUtilities::WindowTree& wt : wts)
   {
      hWnds.push_back(wt.info.handle);
   }
   return 0;
}

最后,从pid的所有窗口中过滤出主窗口。主窗口的标志是:窗口有标题,窗口style为WS_VISIBLE,窗口无owner。代码如下:

bool WindowUtilities::isMainWindow(HWND hWnd)
{
   WindowInformation windInfo = getWindowInformation(hWnd);
   DWORD visibleStyle = WS_VISIBLE;
   bool result = (((string(windInfo.title).length() != 0) && (windInfo.style & (visibleStyle)) != 0) && (windInfo.owner == nullptr)) ? true : false;
   return result;
}

int WindowUtilities::getMainWindows(DWORD pid, std::vector<HWND> &hWnds)
{
   vector<HWND> totalWindows;
   getWindows(pid, totalWindows);
   for(HWND hWnd : totalWindows)
   {
      if (isMainWindow(hWnd))
      {
         hWnds.push_back(hWnd);
      }
   }
   return 0;
}

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值