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

3

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

我在MSDN或Google上没有找到太多相关信息,因为所有的结果都与系统托盘有关。

任何建议或指针都将很有帮助。

编辑: 我尝试了Keegan Hernandez的想法,但我认为我可能做错了什么。以下是代码(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
希望这足以让您入门:
WinAPI有一个函数EnumWindows,它将为当前实例化的每个HWND调用回调函数。要使用它,请编写以下形式的回调:
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
然后调用EnumWindows(EnumWindowsProc,lParam),以便API将为每个窗口调用您的回调,其中hwnd表示一个特定的窗口。
要确定每个窗口是否可见,因此在任务栏上,您可以在回调接收到的每个HWND上使用函数IsWindowVisible(HWND)。如果您很幸运,可以从传递给该回调的HWND获取所需的任何其他信息。

1
并非每个窗口/程序都会出现在任务栏中。在C#中,您可以轻松实现这一点... - RvdK
我同意Roy的观点,在使用EnumWindows时,它会给我们提供许多无关紧要的窗口列表(开始菜单、应用程序的临时窗口等等...)。 - jondinham

0
你的代码有几个问题,请看我的修正。把编译器的警告调高一点(或者查看构建输出),它应该已经警告过你了!
#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;
}

您还需要检查 ex 窗口样式,排除工具栏窗口,并包括 appwindow 的。 - Anders

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接