2012-01-19 8 views
35

He escrito una pequeña aplicación que desactiva la barra de título y los iconos de la barra de tareas de todas las ventanas del sistema operativo Windows en C#. Aquí está el código:¿Cómo puedo eliminar la barra de título y los iconos de la barra de tareas de los programas Java en Windows 7?

using System; 
using System.Runtime.InteropServices; 
using System.Windows.Forms; 

namespace IconKiller 
{ 
    class Program 
    { 
     /// Import the needed Windows-API functions: 
     // ... for enumerating all running desktop windows 
     [DllImport("user32.dll")] 
     static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDesktopWindowsDelegate lpfn, IntPtr lParam); 
     private delegate bool EnumDesktopWindowsDelegate(IntPtr hWnd, int lParam); 

     // ... for loading an icon 
     [DllImport("user32.dll")] 
     static extern IntPtr LoadImage(IntPtr hInst, string lpsz, uint uType, int cxDesired, int cyDesired, uint fuLoad); 

     // ... for sending messages to other windows 
     [DllImport("user32.dll")] 
     static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam); 


     /// Setup global variables 
     // Pointer to empty icon used to replace all other application icons 
     static IntPtr m_pIcon = IntPtr.Zero; 

     // Windows API standard values 
     const int IMAGE_ICON = 1; 
     const int LR_LOADFROMFILE = 0x10; 
     const int WM_SETICON = 0x80; 
     const int ICON_SMALL = 0;   

     static void Main(string[] args) 
     { 
      // Load the empty icon 
      string strIconFilePath = @"blank.ico"; 
      m_pIcon = LoadImage(IntPtr.Zero, strIconFilePath, IMAGE_ICON, 16, 16, LR_LOADFROMFILE); 

      // Setup the break condition for the loop 
      int counter = 0; 
      int max = 10 * 60 * 60; 

      // Loop to catch new opened windows    
      while (counter < max) 
      { 
       // enumerate all desktop windows 
       EnumDesktopWindows(IntPtr.Zero, new EnumDesktopWindowsDelegate(EnumDesktopWindowsCallback), IntPtr.Zero); 
       counter++; 
       System.Threading.Thread.Sleep(100); 
      } 

      // ... then restart application 
      Application.Restart(); 
     } 

     private static bool EnumDesktopWindowsCallback(IntPtr hWnd, int lParam) 
     { 
      // Replace window icon 
      SendMessage(hWnd, WM_SETICON, ICON_SMALL, m_pIcon); 

      return true; 
     } 
    } 
} 

Este código parece funcionar bien con las aplicaciones nativas de Windows. Mi único problema ahora es que Java aparentemente usa una instancia diferente de su icono de aplicación para mostrar en la barra de tareas. Lo que significa que mi pequeña aplicación elimina el icono en la barra de título de los programas de Java, pero no el de la barra de tareas (Netbeans es un buen ejemplo).

¿Cómo resolver ese problema? ¿Es posible enviar un mensaje a esos programas a través de la JVM, similar al truco que utilicé con la API de Windows, para llamar al JFrame.setIconImage() al ejecutar aplicaciones Java o algo similar?

EDITAR: No estoy obligado a simplemente C#, estoy muy dispuesto a escribir algo así como una aplicación de "ayuda" en Java que ejecutaría en mi aplicación principal, en caso de ser necesario.

+2

Tengo curiosidad ... ¿por qué quiere deshacerse de los iconos que proporcionan una manera fácil de distinguir visualmente las aplicaciones – Gus

+0

¿Has probado esto? http://stackoverflow.com/questions/50398/calling-c-sharp-code-from-java – Diego

+0

@Diego ¿Cómo sería eso de ayuda? – hvd

Respuesta

0

Es esto lo que está buscando ?:

[DllImport("user32.dll")] 
     static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDesktopWindowsDelegate lpfn, IntPtr lParam); 
     private delegate bool EnumDesktopWindowsDelegate(IntPtr hWnd, int lParam); 

[DllImport("user32.dll")] 
     private static extern int GetWindowText(IntPtr hWnd, StringBuilder title, int size); 
     [DllImport("user32.dll")] 
     private static extern bool IsWindowVisible(IntPtr hWnd); 

[DllImport("user32.dll")] 
     static extern IntPtr LoadImage(IntPtr hInst, string lpsz, uint uType, int cxDesired, int cyDesired, uint fuLoad); 

[DllImport("user32.dll")] 
     static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); 

static IntPtr m_pIcon = IntPtr.Zero; 

static string[] m_astrFilter = new string[] { "Start", "Program Manager" }; 

     static void Main(string[] args) 
     { 

     string strIconFilePath = @"H:\IconEmpty.ico"; 
     const int IMAGE_ICON = 1; 
     const int LR_LOADFROMFILE = 0x10; 
     m_pIcon = LoadImage(IntPtr.Zero, strIconFilePath, IMAGE_ICON, 0, 0, LR_LOADFROMFILE); 
     while (true) 
     { 
     EnumDesktopWindows(IntPtr.Zero, new EnumDesktopWindowsDelegate(EnumDesktopWindowsCallback), IntPtr.Zero);      System.Threading.Thread.Sleep(100); 
     } 
       Console.ReadKey(); 
     } 

private static bool EnumDesktopWindowsCallback(IntPtr hWnd, int lParam) 
     { 

     StringBuilder title = new StringBuilder(256); 
      GetWindowText(hWnd, title, 256); 
      string strTitle = title.ToString(); 
     bool bVisible = IsWindowVisible(hWnd); 

if (bVisible && // ... visible 
       !String.IsNullOrEmpty(strTitle) && // ... has title 
       !m_astrFilter.Contains(strTitle)) // ... not in filter list 
      { 

     SendMessage(hWnd, 0x80, IntPtr.Zero, m_pIcon); 
     } 

      return true; 
     } 
2

El problema es usar EnumDesktopWindows en lugar de EnumWindows. El siguiente código funciona bien en mi PC:

using System; 
using System.Runtime.InteropServices; 

namespace IconKiller 
{ 
    class Program 
    { 
     /// Import the needed Windows-API functions: 
     // ... for enumerating all running desktop windows 
     [DllImport("user32.dll")] 
     static extern bool EnumWindows(EnumDesktopWindowsDelegate lpfn, IntPtr lParam); 
     private delegate bool EnumDesktopWindowsDelegate(IntPtr hWnd, int lParam); 

     // ... for loading an icon 
     [DllImport("user32.dll")] 
     static extern IntPtr LoadImage(IntPtr hInst, string lpsz, uint uType, int cxDesired, int cyDesired, uint fuLoad); 

     // ... for sending messages to other windows 
     [DllImport("user32.dll")] 
     static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam); 


     /// Setup global variables 
     // Pointer to empty icon used to replace all other application icons 
     static IntPtr m_pIcon = IntPtr.Zero; 

     // Windows API standard values 
     const int IMAGE_ICON = 1; 
     const int LR_LOADFROMFILE = 0x10; 
     const int WM_SETICON = 0x80; 
     const int ICON_SMALL = 0; 

     static void Main(string[] args) 
     { 
      // Load the empty icon 
      string strIconFilePath = @"C:\clicknrun.ico"; 
      m_pIcon = LoadImage(IntPtr.Zero, strIconFilePath, IMAGE_ICON, 16, 16, LR_LOADFROMFILE); 

      // Setup the break condition for the loop 
      int counter = 0; 
      int max = 10 * 60 * 60; 

      // Loop to catch new opened windows    
      while (counter < max) 
      { 
       // enumerate all desktop windows 
       EnumWindows((EnumDesktopWindowsCallback), IntPtr.Zero); 
       counter++; 
       System.Threading.Thread.Sleep(100); 
      } 

      // ... then restart application 
      Console.WriteLine("done"); 
      Console.ReadLine(); 
     } 

     private static bool EnumDesktopWindowsCallback(IntPtr hWnd, int lParam) 
     { 
      // Replace window icon 
      SendMessage(hWnd, WM_SETICON, ICON_SMALL, m_pIcon); 

      return true; 
     } 
    } 
} 
+0

, por supuesto, debe hacer algunas ediciones menores para hacer el código correcto pero se entiende ... –

+0

Isn ' ¿Es demasiado lento enumerar todas las ventanas del sistema? – remio

+0

@remio Lo hace bastante rápido en mi PC y eso es lo que quiere hacer ... a menos que tenga una solución más eficiente. –

Cuestiones relacionadas