你好,如何获取window10任务栏上的图标和名称
时间: 2024-02-27 14:51:36 浏览: 23
你可以使用Windows API函数来获取任务栏上的图标和名称。以下是C++代码示例:
```
#include <Windows.h>
#include <iostream>
int main() {
HWND taskbar = FindWindow(L"Shell_TrayWnd", NULL); // 获取任务栏的窗口句柄
HWND tray = FindWindowEx(taskbar, NULL, L"TrayNotifyWnd", NULL); // 获取托盘区域的窗口句柄
// 获取托盘区域的所有子窗口
HWND hChildWnd = NULL;
while (true) {
hChildWnd = FindWindowEx(tray, hChildWnd, NULL, NULL);
if (hChildWnd == NULL) break;
WCHAR szClassName[256] = { 0 };
GetClassName(hChildWnd, szClassName, 256);
// 如果是图标窗口,则获取窗口名称和图标
if (wcscmp(szClassName, L"TrayClockWClass") != 0) {
WCHAR szWindowText[256] = { 0 };
GetWindowText(hChildWnd, szWindowText, 256);
std::wcout << "Window Name: " << szWindowText << std::endl;
HICON hIcon = (HICON)SendMessage(hChildWnd, WM_GETICON, ICON_SMALL2, 0);
if (hIcon == NULL) {
hIcon = (HICON)SendMessage(hChildWnd, WM_GETICON, ICON_SMALL, 0);
}
if (hIcon == NULL) {
hIcon = (HICON)GetClassLongPtr(hChildWnd, GCLP_HICONSM);
}
if (hIcon == NULL) {
hIcon = (HICON)GetClassLongPtr(hChildWnd, GCLP_HICON);
}
if (hIcon != NULL) {
std::cout << "Icon found!" << std::endl;
}
}
}
return 0;
}
```
这段代码可以获取任务栏上所有图标窗口的名称和图标。注意,这可能不包括某些系统图标,例如音量图标。