c++ 在顶层窗口里搜索名称包含某个字符串的窗口,获取其窗口句柄 的代码,代码使用LPCWSTR 字符串
时间: 2023-12-06 16:45:44 浏览: 138
以下是使用Windows API函数 `FindWindowExW` 和 `GetWindowTextW` 实现的代码,可以搜索包含指定字符串的顶层窗口并获取其窗口句柄:
```c++
#include <windows.h>
HWND FindTopLevelWindow(LPCWSTR nameSubstring)
{
HWND hwnd = NULL;
while ((hwnd = FindWindowExW(NULL, hwnd, NULL, NULL)) != NULL)
{
WCHAR buffer[1024];
if (GetWindowTextW(hwnd, buffer, sizeof(buffer) / sizeof(buffer[0])) > 0)
{
if (wcsstr(buffer, nameSubstring) != NULL)
{
return hwnd;
}
}
}
return NULL;
}
```
这个函数的参数是要搜索的字符串 `nameSubstring`,返回值是找到的窗口句柄 `HWND`。在函数内部使用了 `GetWindowTextW` 函数获取窗口的文本信息,然后使用 `wcsstr` 函数来查找是否包含指定的字符串。注意,`GetWindowTextW` 使用的是 `WCHAR` 类型的字符串,需要使用 `LPCWSTR` 类型的指针作为参数传递。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)