根据窗口部分标题,查找窗口hwnd

输入窗口标题中含有的字符串,调用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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值