2012-01-19 15 views
35

Napisałem małą aplikację, która wyłącza ikony paska tytułu i paska zadań wszystkich okien systemu Windows OS w języku C#. Oto kod:Jak mogę usunąć ikony paska tytułu i paska zadań programów Java w systemie 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; 
     } 
    } 
} 

Ten kod wydaje się dobrze działać z natywnymi aplikacjami systemu Windows. Mój jedyny problem polega na tym, że Java najwyraźniej używa innej instancji swojej ikony aplikacji do wyświetlania na pasku zadań. Oznacza to, że moja mała aplikacja usuwa ikonę na pasku tytułowym programów Java, ale nie tę na pasku zadań (dobrym przykładem jest Netbeans).

Jak rozwiązać ten problem? Czy jest możliwe, aby przekazać wiadomość do tych programów za pośrednictwem JVM, podobnie do sztuczki używanej z interfejsem API systemu Windows, aby wywołać JFrame.setIconImage() na uruchamianie aplikacji Java lub coś podobnego do nich?

EDYCJA: Nie jestem związany tylko z C#, jestem bardzo chętny napisać coś w rodzaju aplikacji "pomocnika" w Javie, którą wykonałbym w mojej głównej aplikacji, gdyby było to konieczne.

+2

... Jestem ciekaw dlaczego chcesz pozbyć się ikony, które zapewniają łatwy sposób wizualnie odróżnianie aplikacji – Gus

+0

Próbował pan jest? http://stackoverflow.com/questions/50398/calling-c-sharp-code-from-java – Diego

+0

@Diego Jak by to miało pomóc? – hvd

Odpowiedz

0

Czy tego szukasz ?:

[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

Problem polega na użyciu EnumDesktopWindows zamiast EnumWindows. Poniższy kod działa poprawnie na moim komputerze:

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

oczywiście, musisz wprowadzić drobne poprawki, aby kod był prawidłowy, ale masz pomysł ... –

+0

Isn ' t zbyt wolno, aby wyliczyć wszystkie okna systemu? – remio

+0

@remio Robi to dość szybko na moim komputerze i to właśnie chce zrobić ... chyba że masz bardziej wydajne rozwiązanie. –

Powiązane problemy