2011-11-20 100 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