gpt4 book ai didi

c# - 在 Windows 7 中隐藏桌面项目 - 不工作

转载 作者:行者123 更新时间:2023-11-30 12:50:52 27 4
gpt4 key购买 nike

在上一个问题中,有一个解释如何隐藏桌面项目:

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

最佳答案

这是 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

关于c# - 在 Windows 7 中隐藏桌面项目 - 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8200385/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com