c++ 使用PID获取顶级窗口句柄和标题

#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
  EnumWindows([](HWND hwnd, LPARAM lParam)  {
	DWORD  pid = 0;
	GetWindowThreadProcessId(hwnd, &pid);
	if (pid == GetCurrentProcessId()) // 判断pid
	{
		char text[1024];
		GetWindowTextA(hwnd, (LPSTR)text, 1024); // 必须含有标题文字
		if (strlen(text) != 0 && IsWindowVisible(hwnd))
		{
			// printf("%s\n", text);
			targetWindow = hwnd;
			return FALSE;
		}
	}
	return TRUE;
  }, 7632);

  return 0;
}
posted @ 2020-08-30 11:03  Ajanuw  阅读(1782)  评论(0编辑  收藏  举报