**********如何得到系统托盘中图标所属的应用程序的窗口的hwnd,及图标数目

sunyuzhe 2002-09-23 09:05:30
比如说系统托盘中有qq,输入法,nordon等,我想得到它们的窗口的hwnd,如何编程实现
...全文
76 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
webber84 2002-09-28
  • 打赏
  • 举报
回复
所有的托盘区图标都是在一个窗口中的,这个窗口的窗口类是TrayNotifyWnd.我写了一段代码演示如何得到这个窗口的句柄及如何计算图标数:
#include "windows.h"
#include "stdio.h"

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
HWND hTaskBarWnd = FindWindow( "Shell_TrayWnd", NULL );
HWND hTrayNotifyWnd = FindWindowEx(
hTaskBarWnd,
NULL,
"TrayNotifyWnd",
NULL );
HWND hTrayClockWnd = FindWindowEx(
hTrayNotifyWnd,
NULL,
"TrayClockWClass",
NULL );
RECT rectNotify,rectClock;
GetWindowRect(hTrayNotifyWnd,&rectNotify);
GetWindowRect(hTrayClockWnd,&rectClock);
int nIcons=((rectNotify.right-rectNotify.left)-(rectClock.right-rectClock.left))/16;
char szText[256];
sprintf(szText,"托盘区图标数:%d",nIcons);
MessageBox(NULL,szText,"ok",MB_OK);
return 0;
}
这段代码用到了一个托盘区图标的特点:它们都是16×16的小图标.
babytiger 2002-09-28
  • 打赏
  • 举报
回复
CTrayToolBar::CTrayToolBar()
{
if(m_handle = ::FindWindow("Shell_TrayWnd",NULL))

if(m_handle = FindWindowEx(m_handle,NULL,"TrayNotifyWnd",NULL))
if(m_handle = FindWindowEx(m_handle,NULL,"ToolbarWindow32",NULL))
if(m_handle)
this->Attach(m_handle);
else
m_handle = NULL;

}

CTrayToolBar::~CTrayToolBar()
{
this->Detach();
}


BEGIN_MESSAGE_MAP(CTrayToolBar, CToolBarCtrl)
//{{AFX_MSG_MAP(CTrayToolBar)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTrayToolBar message handlers

void CTrayToolBar::ShowAll()
{
if(!m_handle) {AfxMessageBox("获取system tray失败!"); return;}

int i = 0;
while(i < ICON_MAX )
{
this->ShowIcon(i,TRUE);
i++;
}

Refresh();

}

void CTrayToolBar::ShowIcon(int index ,BOOL bShow)
{
if(!m_handle) {AfxMessageBox("获取system tray失败!"); return;}
//本来这里可以通过index来获得 nID,但是虽然函数成功,但idCommand却是错误的
/*
TBBUTTON * ptbbtn= new TBBUTTON;
this->SendMessage(TB_GETBUTTON,(WPARAM) index,(LPARAM) ptbbtn);
*/
if(bShow)
this->PostMessage(TB_HIDEBUTTON, index ,(LPARAM) MAKELONG(FALSE, 0));
else
this->PostMessage(TB_HIDEBUTTON, index ,(LPARAM) MAKELONG(TRUE, 0));
// delete ptbbtn;
}

void CTrayToolBar::HideAll()
{

if(!m_handle) {AfxMessageBox("获取system tray失败!"); return;}

int i = 0;
while(i < ICON_MAX )
{
this->ShowIcon(i,FALSE);
i++;
}

Refresh();

}

void CTrayToolBar::Refresh()
{

NOTIFYICONDATAW Icon;
Icon.cbSize = sizeof( NOTIFYICONDATAW );
Icon.hWnd = this->m_hWnd ;
Icon.uID = 0;
Icon.uFlags = NIF_MESSAGE | NIF_TIP;
Icon.uCallbackMessage = WM_USER + 777;
Icon.hIcon = NULL;
Shell_NotifyIconW( NIM_ADD , &Icon );
Shell_NotifyIconW( NIM_DELETE , &Icon );

}

void CTrayToolBar::DeleteIcon(int index)
{
if(!m_handle) {AfxMessageBox("获取system tray失败!"); return;}

this->DeleteButton(index);
Refresh();
}


int CTrayToolBar::GetCount()
{
if(!m_handle) {AfxMessageBox("获取system tray失败!"); return 0;}
return this->GetButtonCount();
}
nuaazwg 2002-09-23
  • 打赏
  • 举报
回复
gz
wuxuan 2002-09-23
  • 打赏
  • 举报
回复
http://www.codeproject.com/shell/trayposition.asp
2ndboy 2002-09-23
  • 打赏
  • 举报
回复
之后可以用“工具栏”相关API去获得图标数量
2ndboy 2002-09-23
  • 打赏
  • 举报
回复
// 得到任务栏窗口句柄
HWND hTaskBarWnd = FindWindow( "Shell_TrayWnd", NULL );

// 得到通知区窗口句柄
HWND hTrayNotifyWnd = FindWindowEx(
hTaskBarWnd,
NULL,
"TrayNotifyWnd",
NULL );

// 得到系统托盒窗口句柄
HWND hTrayClockWnd = FindWindowEx(
hTrayNotifyWnd,
NULL,
"ToolbarWindow32",
NULL );
yuxuguang 2002-09-23
  • 打赏
  • 举报
回复
你可以先使用spy获得应用程序的classname或者windowname,然后用FindWindow(...)就可以获得句柄。直接通过系统托盘的方式我不太清楚,不知道那位高手可以提示一下。
sunyuzhe 2002-09-23
  • 打赏
  • 举报
回复
upup

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧