在Windows 7中隐藏桌面图标-无法工作

4
在之前的问题中,有一份关于如何隐藏桌面项的解释: 如何以编程方式隐藏桌面图标? 由于某种原因,这段代码对我无效。
我本来想在上面的链接中发表评论,但我没有足够的权限评论其他人的问题...
有什么想法是怎么出了问题?桌面根本没有被隐藏。
更新:此外,我尝试使用以下代码(正如此处所建议的),但仍然没有效果:
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();
    }
}

2
那个答案是一个很粗糙的hack。我不惊讶它不起作用。你有没有寻找基于Shell API的方法,比如使用IShellFolder? - David Heffernan
3
另一个值得检查的API是SHGetSetSettings,使用SHELLSTATE.fHideIcons位,这在此帖子中提到过。虽然我自己没有尝试过,但也许值得一试。(注意 - 那里的其他两个答案也是粗暴的hack方法。) - BrendanMcK
1
另一个回答并不是真正的答案。它是基于未记录的实现细节的黑客。Shell有一个非常丰富的COM接口。获取桌面的IShellFolder并进行操作。 - David Heffernan
2
@Jacob,你可能需要提供更多关于你在这里尝试做什么的背景信息。你是想完全替换shell,就像在专用的信息亭上一样,还是只是替换桌面背景,或者运行全屏,或者其他什么?这是一个永久性的替换,还是只是在运行某个应用程序的期间? - BrendanMcK
2
@Jacob - 这应该是一个相当简单的使用P/Invoke的案例;请参阅MSDN上的这个教程。但要记住,有许多绕过此类应用程序的方法;右键单击桌面仍可能会弹出菜单,Ctrl-ESC将弹出Windows菜单,Alt-Tab将切换到其他应用程序,而Ctrl-alt-del可以用于打开锁定的桌面,从那里打开任务管理器。如果“kiosk-lite”可以接受,那么成为顶部全屏应用程序(像许多游戏、媒体播放器等)可能是一个更简单/更清洁的方法... - BrendanMcK
显示剩余10条评论
1个回答

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);
}

该代码会向Progman的SHELLDLL_DefView子窗口发送一条消息,告诉它切换其唯一子项"FolderView"的可见性(通过添加或删除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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接