2010-02-19 97 views
4

我正在尋找一種方法來隱藏桌面上的特定圖標。我通常在桌面上有很多圖標(這使得找到一個文件非常麻煩),所以我想寫一個小工具,在我輸入時「過濾」它們。我不想「移動」或刪除它們,只是隱藏(或加深)它們。我知道如何一次切換顯示所有圖標的隱藏狀態,但不是基於每個圖標。有任何想法嗎?是否可以用C#隱藏特定的桌面圖標?

+1

如果你把這個應用程序,將其發送給我吧:) – anthares 2010-02-19 01:01:01

+0

當然 - 沒問題,我會打開源它反正:-) – crono 2010-02-19 01:04:10

回答

3

我試着以某種方式導航到桌面的ListView控件(使用Win32 API)。然後,我要在要隱藏的項目上繪製一些半透明的矩形(可以使用宏/消息ListItem_GetItemRect查詢項目的矩形),從列表控件中臨時刪除項目,將項目的狀態設置爲CUT (淡出),或者我會嘗試操作列表視圖的圖像列表添加一個透明的圖像,並將項目的圖像設置爲此。

但我不知道這種方法是否可行......而且我不確定我是否會在C#中嘗試這樣做(我寧願使用C++)。

+0

我真的很喜歡這個矩形的想法!我也會嘗試這個。我想我會留在.NET中,我的Win32 C++經驗幾乎爲零。 – crono 2010-02-20 16:05:15

3

@crono,我認爲最好的選擇是添加一個對COM庫「Microsoft Shell Control And Automation」的引用,並使用Shell32.Shell對象。然後枚舉快捷方式並設置快捷方式的文件屬性(FileAttributes.Hidden)。

查看這些鏈接瞭解更多信息。

看到這個簡單的例子,是不完整的,僅僅是一個草案。

using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using System.IO; 
    using Shell32; //"Microsoft Shell Control And Automation" 

    namespace ConsoleApplication1 
    { 
     class Program 
     { 
      static void Main(string[] args) 
      { 
       Shell32.Shell oShell; 
       Shell32.Folder oFldr; 
       oShell = new Shell32.Shell(); 
       oFldr = oShell.NameSpace(Shell32.ShellSpecialFolderConstants.ssfDESKTOP);//point to the desktop 

       foreach (Shell32.FolderItem oFItm in oFldr.Items()) //get the shotrcuts 
       { 

        if (oFItm.IsLink) 
        { 
         Console.WriteLine("{0} {1} ", oFItm.Name, oFItm.Path); 

         bool isArchive = ((File.GetAttributes(oFItm.Path) & FileAttributes.Archive) == FileAttributes.Archive); 
         //bool isHidden = ((File.GetAttributes(oFItm.Path) & FileAttributes.Hidden) == FileAttributes.Hidden); 

         if (isArchive) //Warning, here you must define the condition for hide the shortcut. in this case only check if has set the Archive atribute. 
         { 

          //Now you can set FileAttributes.Hidden atribute 
          //File.SetAttributes(oFItm.Path, File.GetAttributes(oFItm.Path) | FileAttributes.Hidden); 
         } 

        } 
        else 
        { 
         Console.WriteLine("{0} {1} ", oFItm.Name, oFItm.Path); 
        } 

       } 

       Console.ReadKey(); 
      } 
     } 
    } 
+0

這是一個好主意,但是我認爲只有在將「顯示隱藏文件」設置爲「false」的情況下才能正常工作,而我大部分時間都是這樣做的。也許我可以改變它「在飛行中」...我會看看。謝謝 :) – crono 2010-02-20 15:59:24

相關問題