在C#中隐藏另一个应用程序的系统托盘

时间:2013-01-08 23:11:03

标签: c#

好的,最后一次尝试,我正在尝试解决URL下面详述的问题。我查看了URL中详述的文章,但我仍无法隐藏相关图标。任何想法?

http://www.codeproject.com/Articles/10807/Shell-Tray-Info-Arrange-your-system-tray-icons

http://social.msdn.microsoft.com/Forums/da/vbgeneral/thread/a11faa45-a3ea-4060-8de4-a6bc22e1516d

  
    
      
        

我希望能够隐藏由Windows语音识别加载的系统托盘图标(作为Windows 7和Windows 8和Windows Vista的一部分)。我需要在C#中完成这项工作,过去几天一直在试用Google解决方案,但无济于事。似乎最好的方法是使用此代码:

      
    
  

//上面定义的NotifyIconData结构

private void button1_Click(object sender, EventArgs e)
{
    NOTIFYICONDATA pnid = new NOTIFYICONDATA();
    pnid.uCallbackMessage = 0x800;
    pnid.uFlags = 1;
    pnid.hwnd = ???;
    pnid.uID = 1;
    pnid.szTip = null;
    pnid.uFlags |= 2;
    pnid.hIcon = ???;
    pnid.uFlags |= 4;
    pnid.szTip = "Speech Recognition is listening";

    bool b = Shell_NotifyIcon(2, ref pnid);

Shell_NotifyIcon API函数(2)的第一个参数是删除。问题是我不知道如何找到带有问号的参数,包括图标句柄。我尝试使用ExtractIcon使用窗口语音识别快捷方式指示的可执行文件和任务管理器文件位置(%windir%\ Speech \ Common \ sapisvr.exe -SpeechUX)中指示的文件位置,但它告诉我可执行文件已经没有相关的图标。我还使用我下载的免费应用程序对其进行了验证,以检查带有该可执行文件的图标,它也说了同样的事情。

我可以使用以下方式获取图标托盘的窗口句柄:

    IntPtr hWnd = Win32API.FindWindow("Shell_TrayWnd", null);
    if(hWnd.ToInt32() > 0)
        {
            hWnd = Win32API.FindWindowEx(hWnd, IntPtr.Zero, "TrayNotifyWnd", null);
            if (hWnd.ToInt32() > 0)
            {
                hWnd = Win32API.FindWindowEx(hWnd,IntPtr.Zero, "SysPager", null);
                if (hWnd.ToInt32() > 0)
               {
                    hWnd = Win32API.FindWindowEx(hWnd, IntPtr.Zero, "ToolbarWindow32", null);
                }                    
               // count = Win32API.SendMessage(hWnd, 1048 , 0, 0);
            }
        }

然而,即使使用句柄和图标计数,我也不知道如何枚举图标句柄。

如果有人能给我一个C#的工作解决方案,我很乐意支付咨询费用,就像我说你可以通过加载Windows 7和Windows 8免费提供的Windows语音识别轻松尝试它,你会看到我的图标意思。我可以使用C ++解决方案,但它必须完全在托管C ++(.NET)

1 个答案:

答案 0 :(得分:1)

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpClassName, string lpWindowName);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

static IntPtr GetSystemTrayHandle()
{           
    IntPtr hWndTray = FindWindow("Shell_TrayWnd", null);
    if (hWndTray != IntPtr.Zero)
    {
        hWndTray = FindWindowEx(hWndTray, IntPtr.Zero, "TrayNotifyWnd", null);
        if (hWndTray != IntPtr.Zero)
        {
            hWndTray = FindWindowEx(hWndTray, IntPtr.Zero, "SysPager", null);
            if (hWndTray != IntPtr.Zero)
            {
                hWndTray = FindWindowEx(hWndTray, IntPtr.Zero, "ToolbarWindow32", null);
                return hWndTray;
            }
        }
    }

    return IntPtr.Zero;
}

拥有窗口句柄后,您可以选择遍历这些进程以查找系统托盘中的进程并执行您需要的任何工作:

using System.Diagnostics;
Process [] processes = System.Diagnostics.Process.GetProcesses();

foreach (System.Diagnostics.Process process in processes)
{
    if (process.MainWindowHandle == hWndTray)
    {
        // ...
    }
}