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

时间:2009-07-23 02:23:45

标签: c++ windows taskbar

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

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

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

编辑: 我尝试了基冈埃尔南德斯的想法,但我想我可能做错了什么。代码如下(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 个答案:

答案 0 :(得分:1)

希望这足以让你开始:

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

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam);

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

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

答案 1 :(得分: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;
}