输入窗口标题中含有的字符串,调用EnumWindows查找所有符合条件的窗口。
#include "windows.h"
#include "psapi.h"
#include "iostream"
#include "vector"
#include "algorithm"
#include "string"
#include "cstring"
using namespace std;
typedef vector<pair<HWND, string> > ret_type;
typedef pair<const char *, ret_type *> param_type;
string wnd_title;
vector<pair<HWND, string> > find_window_substr(char *title_part)
{
ret_type ret;
int i=0; while (title_part[i]=tolower(title_part[i])) i++; //convert to lower case
EnumWindows([](HWND hw, LPARAM lparam) CALLBACK {
char buf[400];
//parsing params
const char *title = ((param_type *)lparam)->first;
ret_type *vec = ((param_type *)lparam)->second;
//work
GetWindowText(hw, buf, sizeof(buf));
int i=0; while (buf[i]=tolower(buf[i])) i++; //convert to lower case
string tmp(buf);
if (tmp.find(title)!=string::npos)
vec->push_back(make_pair(hw, tmp));
return 1;
}
, (LPARAM)&make_pair(title_part, &ret));
return ret;
}
int main(int argc, char ** argv)
{
if (argc<2)
{
cout << "window title: ";
cin >> wnd_title;
}
else wnd_title=argv[1];
ret_type result = find_window_substr(wnd_title.c_str());
for_each(result.begin(), result.end(), [](pair<HWND, string> x)
{cout << hex << x.first << ": " << x.second << endl;}
);
return 0;
}
code