在上一个问题中,有一个解释如何隐藏桌面项目:
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 :(得分: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