2011-11-20 103 views
4

前一個問題,有一個解釋如何隱藏桌面項目:隱藏桌面項目 - 不工作

How to hide desktop icons programatically?

出於某種原因,這個代碼不爲我工作。

我會簡單地評論上面的鏈接,但我沒有足夠的權限對​​別人的問題發表評論......

任何想法是怎麼回事?桌面根本不隱藏。

更新:另外,我嘗試使用下面的代碼(如建議here),但仍然沒有效果:

struct SHELLSTATE 
{ 
     bool fShowAllObjects; 
     bool fShowExtensions; 
     bool fNoConfirmRecycle; 
     bool fShowSysFiles; 
     bool fShowCompColor; 
     bool fDoubleClickInWebView; 
     bool fDesktopHTML; 
     bool fWin95Classic; 
     bool fDontPrettyPath; 
     bool fShowAttribCol; 
     bool fMapNetDrvBtn; 
     bool fShowInfoTip1; 
     bool fHideIcons1; 
     bool fWebView1; 
     bool fFilter1; 
     bool fShowSuperHidden1; 
     bool fNoNetCrawling1; 
     UInt32 dwWin95Unused; 
     uint uWin95Unused; 
     long lParamSort; 
     int iSortDirection; 
     uint version; 
     uint uNotUsed; 
     bool fSepProcess; 
     bool fStartPanelOn; 
     bool fShowStartPage; 
     bool fAutoCheckSelect; 
     bool fIconsOnly; 
     bool fShowTypeOverlay; 
     uint fSpareFlags; 
} 

class MyClass 
{ 
    const UInt32 SSF_HIDEICONS = 0x00004000; 

    [DllImport("Shell32.dll")] 
    static extern void SHGetSetSettings(ref SHELLSTATE state, UInt32 dwMask, bool bSet); 
    static void Foobar() 
    { 
     SHELLSTATE stateOfMind = new SHELLSTATE(); 
     Console.WriteLine("Set to true:"); 
     SHGetSetSettings(ref stateOfMind, SSF_HIDEICONS, true); 
     Console.ReadKey(); 
     Console.WriteLine("Set to false:"); 
     SHGetSetSettings(ref stateOfMind, SSF_HIDEICONAS, false); 
     Console.ReadKey(); 
    } 
} 
+0

你嘗試重新啓動Explorer.exe的* *,例如*提筆*評論? – Otiel

+2

這個答案是一個嚴重的問題。我並不感到驚訝,它不起作用。您是否使用IShellFolder尋找基於shell API的方法? –

+0

@Otiel,我試了一下。 (也就是說,我結束了explorer.exe並開始了一個名爲explorer的新任務。) –

回答

1

這裏是C#示例代碼,將切換桌面圖標。

[DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 
[DllImport("user32.dll", SetLastError = true)] static extern IntPtr GetWindow(IntPtr hWnd, GetWindow_Cmd uCmd); 
enum GetWindow_Cmd : uint 
{ 
    GW_HWNDFIRST = 0, 
    GW_HWNDLAST = 1, 
    GW_HWNDNEXT = 2, 
    GW_HWNDPREV = 3, 
    GW_OWNER = 4, 
    GW_CHILD = 5, 
    GW_ENABLEDPOPUP = 6 
} 
[DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); 

private const int WM_COMMAND = 0x111; 

static void ToggleDesktopIcons() 
{ 
    var toggleDesktopCommand = new IntPtr(0x7402); 
    IntPtr hWnd = GetWindow(FindWindow("Progman", "Program Manager"), GetWindow_Cmd.GW_CHILD); 
    SendMessage(hWnd, WM_COMMAND, toggleDesktopCommand, IntPtr.Zero); 
} 

這將消息發送到普羅格曼的SHELLDLL_DefView子窗口,告訴它切換可見性(通過添加或刪除的WS_VISIBLE風格),它是唯一的孩子,「文件夾視圖」。 「FolderView」是包含圖標的實際窗口。

要測試,看看是否圖標是可見或不可見,你可以查詢使用GetWindowInfo功能WS_VISIBLE風格,如下圖所示:

[return: MarshalAs(UnmanagedType.Bool)] 
[DllImport("user32.dll", SetLastError = true)] 
private static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi); 

[StructLayout(LayoutKind.Sequential)] 
public struct RECT 
{ 
    private int _Left; 
    private int _Top; 
    private int _Right; 
    private int _Bottom; 
} 

[StructLayout(LayoutKind.Sequential)] 
struct WINDOWINFO 
{ 
    public uint cbSize; 
    public RECT rcWindow; 
    public RECT rcClient; 
    public uint dwStyle; 
    public uint dwExStyle; 
    public uint dwWindowStatus; 
    public uint cxWindowBorders; 
    public uint cyWindowBorders; 
    public ushort atomWindowType; 
    public ushort wCreatorVersion; 

    public WINDOWINFO(Boolean? filler) 
     : this() // Allows automatic initialization of "cbSize" with "new WINDOWINFO(null/true/false)". 
    { 
     cbSize = (UInt32)(Marshal.SizeOf(typeof(WINDOWINFO))); 
    } 

} 

下面是調用上面的代碼,並返回true功能如果窗口可見,則返回false。

static bool IsVisible() 
{ 
    IntPtr hWnd = GetWindow(GetWindow(FindWindow("Progman", "Program Manager"), GetWindow_Cmd.GW_CHILD), GetWindow_Cmd.GW_CHILD); 
    WINDOWINFO info = new WINDOWINFO(); 
    info.cbSize = (uint)Marshal.SizeOf(info); 
    GetWindowInfo(hWnd, ref info); 
    return (info.dwStyle & 0x10000000) == 0x10000000; 
} 

與有關窗口樣式的更多信息,以及Windows API的代碼可以在這裏找到:http://www.pinvoke.net/default.aspx/user32/GetWindowInfo.html