Скрыть ярлыки рабочего стола - C#
Формулировка задачи:
Можно сделать так чтоб при запуске программы исчезали все ярлыки рабочего стола?
Решение задачи: «Скрыть ярлыки рабочего стола»
textual
Листинг программы
static class Program
{
#region Declarations
delegate bool EnumCallback(IntPtr hwnd, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumWindows(EnumCallback lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr GetWindow(IntPtr hWnd, GWConstants iCmd);
enum GWConstants : int
{
GW_HWNDFIRST,
GW_HWNDLAST,
GW_HWNDNEXT,
GW_HWNDPREV,
GW_OWNER,
GW_CHILD,
GW_MAX
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetClassName(IntPtr hWnd, [Out] StringBuilder buf, int nMaxCount);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, int nShow);
#endregion
#region Methods
static string GetClassNameFromHWND(IntPtr hWnd)
{
StringBuilder sb = new StringBuilder(256);
int len = GetClassName(hWnd, sb, sb.Capacity);
if (len > 0)
return sb.ToString(0, len);
throw new Win32Exception(Marshal.GetLastWin32Error());
}
static void ShowHideDesktopIcons(bool show)
{
EnumWindows(new EnumCallback(EnumWins), show ? (IntPtr)5 : IntPtr.Zero);
}
static bool EnumWins(IntPtr hWnd, IntPtr lParam)
{
if (hWnd != IntPtr.Zero)
{
IntPtr hDesk = GetWindow(hWnd, GWConstants.GW_CHILD);
if (hDesk != IntPtr.Zero && GetClassNameFromHWND(hDesk) == "SHELLDLL_DefView")
{
hDesk = GetWindow(hDesk, GWConstants.GW_CHILD);
if (hDesk != IntPtr.Zero && GetClassNameFromHWND(hDesk) == "SysListView32")
{
ShowWindow(hDesk, lParam.ToInt32());
return false;
}
}
}
return true;
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ShowHideDesktopIcons(false);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}