有朋友在工作中需要得到托盘窗口,请我帮忙,于是我就简单地写下来了。想想或许对大家有帮助,所以我就转到这里来,但愿能帮到你。

CWnd* GetTrayWindow()
{
    CWnd* pTrayWnd = CWnd::FindWindow(_T("Shell_TrayWnd"), NULL);
    if(pTrayWnd != NULL)
    {
        CWnd* pNotifyWnd = CWnd::FindWindowEx(pTrayWnd->m_hWnd, NULL, _T("TrayNotifyWnd"), NULL);
        if(pNotifyWnd != NULL)
        {
            CWnd* pSysPager = CWnd::FindWindowEx(pNotifyWnd->m_hWnd, NULL, _T("SysPager"), NULL);
            if(pSysPager != NULL)
            {
                return CWnd::FindWindowEx(pSysPager->m_hWnd, NULL, _T("ToolbarWindow32"), NULL);
            }

            return  CWnd::FindWindowEx(pNotifyWnd->m_hWnd, NULL, _T("ToolbarWindow32"), NULL);
        }
    }

    return NULL;
}

 

为了验证得到的窗口是否是自己需要的,写一个测试小程序,把得到的内容绘制出来看看(其它代码省略)。

void CChildView::OnPaint()
{
    CPaintDC dc(this); // 用于绘制的设备上下文
   
    CWnd* pToolbarWindow = GetTrayWindow();
    if(pToolbarWindow != NULL)
    {
        CRect rect;
        pToolbarWindow->GetClientRect(&rect);

        CClientDC dcWin(pToolbarWindow);
        dc.BitBlt(100, 100, rect.Width(), rect.Height(), &dcWin, 0, 0, SRCCOPY);
    }           
   
    // 不要为绘制消息而调用 CWnd::OnPaint()
}

如果代码可以正常工作并能帮到你,这段代码是我自己写的。如果不能正常工作,对不起,我也不知道是谁写的。