在c ++中访问Windows任务栏图标

时间:2021-10-12 15:07:43

I am looking for a way to programmatically get the current taskbar icons (not the system tray) for each program that is in the taskbar.

我正在寻找一种方法来以编程方式获取任务栏中每个程序的当前任务栏图标(而不是系统托盘)。

I haven't had much luck with MSDN or Google, because all of the results relate to the system tray.

我对MSDN或Google没有太多运气,因为所有结果都与系统托盘有关。

Any suggestions or pointers would be helpful.

任何建议或指示都会有所帮助。

EDIT: I tried Keegan Hernandez's idea but I think I might have done something wrong. The code is below (c++).

编辑:我尝试了基冈埃尔南德斯的想法,但我想我可能做错了什么。代码如下(c ++)。

#include <iostream>
#include <vector>
#include <windows.h>
#include <sstream>
using namespace std;
vector<string> xxx;
bool EnumWindowsProc(HWND hwnd,int ll)
{
    if(ll=0)
    {
        //...
        if(IsWindowVisible(hwnd)==true){
        char tyty[129];
        GetWindowText(hwnd,tyty,128);
        stringstream lmlm;
        lmlm<<tyty;
        xxx.push_back(lmlm.str());
        return TRUE;
        }
    }
}
int main()
{
    EnumWindows((WNDENUMPROC)EnumWindowsProc,0);
    vector<string>::iterator it;
    for(it=xxx.begin();it<xxx.end();it++)
    {cout<< *it <<endl;}
    bool empty;
    cin>>empty;
}

2 个解决方案

#1


There are several problems with your code, please see my corrections. Turn the warnings up (or read the build output) on your compiler, it should have warned (or did warn) you about these!

您的代码有几个问题,请参阅我的更正。在编译器上打开警告(或读取构建输出),它应该警告(或确实警告)你这些!

#include <iostream>
#include <vector>
#include <windows.h>
#include <sstream>
using namespace std;
vector<string> xxx;
// The CALLBACK part is important; it specifies the calling convention.
// If you get this wrong, the compiler will generate the wrong code and your
// program will crash.
// Better yet, use BOOL and LPARAM instead of bool and int.  Then you won't
// have to use a cast when calling EnumWindows.
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM ll)
{
    if(ll==0) // I think you meant '=='
    {
        //...
        if(IsWindowVisible(hwnd)==true){
        char tyty[129];
        GetWindowText(hwnd,tyty,128);
        stringstream lmlm;
        lmlm<<tyty;
        xxx.push_back(lmlm.str());
        //return TRUE; What if either if statement fails?  You haven't returned a value!
        }
    }
    return TRUE;
}
int main()
{
    EnumWindows(EnumWindowsProc,0);
    vector<string>::iterator it;
    for(it=xxx.begin();it<xxx.end();it++)
    {cout<< *it <<endl;}
    bool empty;
    cin>>empty;
}

#2


Hopefully this is enough to get you started:

希望这足以让你开始:

WinAPI has a function EnumWindows which will call a callback function for each HWND that is currently instantiated. To use it write a callback of the form:

WinAPI有一个函数EnumWindows,它将为当前实例化的每个HWND调用一个回调函数。要使用它,请写一个表单的回调:

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam);

Then call EnumWindows(EnumWindowsProc, lParam) so that the API will call your callback for each window, where hwnd represents one specific window.

然后调用EnumWindows(EnumWindowsProc,lParam),以便API为每个窗口调用你的回调,其中hwnd代表一个特定的窗口。

To determine if each window is visible and therefore on the taskbar, you can use the function IsWindowVisible(HWND) on each HWND that the callback receives. If you're lucky you can get whatever other information you need from the HWNDs passed to that callback.

要确定每个窗口是否可见并因此在任务栏上,您可以在回调接收的每个HWND上使用函数IsWindowVisible(HWND)。如果您很幸运,您可以从传递给该回调的HWND获得您需要的任何其他信息。

#1


There are several problems with your code, please see my corrections. Turn the warnings up (or read the build output) on your compiler, it should have warned (or did warn) you about these!

您的代码有几个问题,请参阅我的更正。在编译器上打开警告(或读取构建输出),它应该警告(或确实警告)你这些!

#include <iostream>
#include <vector>
#include <windows.h>
#include <sstream>
using namespace std;
vector<string> xxx;
// The CALLBACK part is important; it specifies the calling convention.
// If you get this wrong, the compiler will generate the wrong code and your
// program will crash.
// Better yet, use BOOL and LPARAM instead of bool and int.  Then you won't
// have to use a cast when calling EnumWindows.
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM ll)
{
    if(ll==0) // I think you meant '=='
    {
        //...
        if(IsWindowVisible(hwnd)==true){
        char tyty[129];
        GetWindowText(hwnd,tyty,128);
        stringstream lmlm;
        lmlm<<tyty;
        xxx.push_back(lmlm.str());
        //return TRUE; What if either if statement fails?  You haven't returned a value!
        }
    }
    return TRUE;
}
int main()
{
    EnumWindows(EnumWindowsProc,0);
    vector<string>::iterator it;
    for(it=xxx.begin();it<xxx.end();it++)
    {cout<< *it <<endl;}
    bool empty;
    cin>>empty;
}

#2


Hopefully this is enough to get you started:

希望这足以让你开始:

WinAPI has a function EnumWindows which will call a callback function for each HWND that is currently instantiated. To use it write a callback of the form:

WinAPI有一个函数EnumWindows,它将为当前实例化的每个HWND调用一个回调函数。要使用它,请写一个表单的回调:

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam);

Then call EnumWindows(EnumWindowsProc, lParam) so that the API will call your callback for each window, where hwnd represents one specific window.

然后调用EnumWindows(EnumWindowsProc,lParam),以便API为每个窗口调用你的回调,其中hwnd代表一个特定的窗口。

To determine if each window is visible and therefore on the taskbar, you can use the function IsWindowVisible(HWND) on each HWND that the callback receives. If you're lucky you can get whatever other information you need from the HWNDs passed to that callback.

要确定每个窗口是否可见并因此在任务栏上,您可以在回调接收的每个HWND上使用函数IsWindowVisible(HWND)。如果您很幸运,您可以从传递给该回调的HWND获得您需要的任何其他信息。