2013-01-08 34 views
2

好吧最後一次嘗試,我試圖解決URL下面詳細說明的問題。我查看了網址中詳細介紹的文章,但我仍然無法隱藏相關圖標。任何任何想法?從C中的另一個應用程序隱藏系統托盤#

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解決方案,但都無濟於事。它似乎是最好的方式前進。將使用以下代碼:上述

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函數的第一個參數定義

// NOTIFYICONDATA結構(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); 
      } 
     } 
然而即使有手柄和我不知道如何ennumerate圖標手柄的圖標的數量

如果任何人都可以在C#中給我一個工作解決方案,我會很樂意支付諮詢費用,就像我說的那樣,您可以通過加載Windows語音識別功能輕鬆嘗試它,而Windows 7和Windows 8免費提供,我的意思是。我能活一個C++解決方案,但它必須在託管C是完全++(.NET)

+0

退房此鏈接http://www.codeproject.com/Articles/10497/A-tool-to-order-the-window-buttons- in-your-taskbar – MethodMan

回答

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) 
    { 
     // ... 
    } 
} 
+0

爲什麼你認爲MainWindowHandle == systemTrayHandle有任何進程?對於我的進程(最小化爲系統托盤)MainWindowHandle = 0。 – Kate

+0

所以你真的已經嘗試了代碼sysyTrayHandle將是什麼你會代替你的實際變量抱歉的錯字或混淆 – MethodMan

+0

你能給我發電子郵件我會很樂意幫助你凱特。 [email protected]謝謝 – MethodMan

相關問題