Скрыть ярлыки рабочего стола - C#

Узнай цену своей работы

Формулировка задачи:

Можно сделать так чтоб при запуске программы исчезали все ярлыки рабочего стола?

Решение задачи: «Скрыть ярлыки рабочего стола»

textual
Листинг программы
  1. static class Program
  2. {
  3.     #region Declarations
  4.     delegate bool EnumCallback(IntPtr hwnd, IntPtr lParam);
  5.  
  6.     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  7.     [return: MarshalAs(UnmanagedType.Bool)]
  8.     static extern bool EnumWindows(EnumCallback lpEnumFunc, IntPtr lParam);
  9.  
  10.     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  11.     static extern IntPtr GetWindow(IntPtr hWnd, GWConstants iCmd);
  12.  
  13.     enum GWConstants : int
  14.     {
  15.         GW_HWNDFIRST,
  16.         GW_HWNDLAST,
  17.         GW_HWNDNEXT,
  18.         GW_HWNDPREV,
  19.         GW_OWNER,
  20.         GW_CHILD,
  21.         GW_MAX
  22.     }
  23.  
  24.     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  25.     public static extern int GetClassName(IntPtr hWnd, [Out] StringBuilder buf, int nMaxCount);
  26.  
  27.     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  28.     [return: MarshalAs(UnmanagedType.Bool)]
  29.     static extern bool ShowWindow(IntPtr hWnd, int nShow);
  30.     #endregion
  31.  
  32.     #region Methods
  33.     static string GetClassNameFromHWND(IntPtr hWnd)
  34.     {
  35.         StringBuilder sb = new StringBuilder(256);
  36.         int len = GetClassName(hWnd, sb, sb.Capacity);
  37.         if (len > 0)
  38.             return sb.ToString(0, len);
  39.  
  40.         throw new Win32Exception(Marshal.GetLastWin32Error());
  41.     }
  42.  
  43.     static void ShowHideDesktopIcons(bool show)
  44.     {
  45.         EnumWindows(new EnumCallback(EnumWins), show ? (IntPtr)5 : IntPtr.Zero);
  46.     }
  47.  
  48.     static bool EnumWins(IntPtr hWnd, IntPtr lParam)
  49.     {
  50.         if (hWnd != IntPtr.Zero)
  51.         {
  52.             IntPtr hDesk = GetWindow(hWnd, GWConstants.GW_CHILD);
  53.             if (hDesk != IntPtr.Zero && GetClassNameFromHWND(hDesk) == "SHELLDLL_DefView")
  54.             {
  55.                 hDesk = GetWindow(hDesk, GWConstants.GW_CHILD);
  56.                 if (hDesk != IntPtr.Zero && GetClassNameFromHWND(hDesk) == "SysListView32")
  57.                 {
  58.                     ShowWindow(hDesk, lParam.ToInt32());
  59.                     return false;
  60.                 }
  61.             }
  62.         }
  63.         return true;
  64.     }
  65.     #endregion
  66.     /// <summary>
  67.     /// The main entry point for the application.
  68.     /// </summary>
  69.     [STAThread]
  70.     static void Main(string[] args)
  71.     {
  72.         ShowHideDesktopIcons(false);
  73.         Application.EnableVisualStyles();
  74.         Application.SetCompatibleTextRenderingDefault(false);
  75.         Application.Run(new Form1());
  76.     }
  77. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 3.818 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут