Puede hacerlo utilizando la API de Windows. Aquí hay un código de muestra en C# que alternará iconos de escritorio.
[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);
}
Esto envía un mensaje a la ventana secundaria SHELLDLL_DefView de Progman, que dice que para cambiar la visibilidad (añadiendo o eliminando el estilo WS_VISIBLE) de él es hijo único, "FolderView". "FolderView" es la ventana real que contiene los iconos.
Para probar para ver si los iconos son visibles o no, se puede consultar por el estilo WS_VISIBLE mediante el uso de la función GetWindowInfo, se muestra a continuación:
[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)));
}
}
Aquí es una función que se llama el código anterior y devuelve verdadero si la ventana es visible, falso si no.
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;
}
el código de Windows API, junto con más información sobre los estilos de ventana se puede encontrar aquí: http://www.pinvoke.net/default.aspx/user32/GetWindowInfo.html
Impresionante, voy a poner eso en todas mis aplicaciones a partir de ahora y alternar() al azar. :) – Gleno
Parece que no funciona en mi computadora ... Estoy usando Windows 7. ¿Es este sistema operativo dependiente? ¿Debería funcionar en todas las versiones de Windows? Si es así, buscaré otra solución que funcione en múltiples versiones de Windows ... – Tibi
Actualización: Funciona, aparentemente tuve que reiniciar explorer.exe, pero ahora funciona. Muchas gracias. Otra pregunta ... ¿cómo puedo saber si está activada o desactivada? – Tibi