根據視窗部分標題,尋找視窗hwnd

來源:互聯網
上載者:User

輸入視窗標題中含有的字串,調用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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.