2012-01-19 4 views
35

私はWindows OSのすべてのウィンドウのタイトルバーとタスクバーのアイコンをC#で無効にする小さなアプリケーションを作成しました。コードは次のとおりです。Windows 7でJavaプログラムのタイトルバーとタスクバーアイコンを削除するにはどうすればよいですか?

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; 
     } 
    } 
} 

このコードは、ネイティブウィンドウアプリケーションで正常に動作するようです。私の唯一の問題は、Javaが明らかにアプリケーションアイコンの別のインスタンスを使用してタスクバーに表示されることです。私の小さなアプリケーションがJavaプログラムのタイトルバーのアイコンを削除するが、タスクバーのアイコンは削除されないことを意味する(Netbeansは良い例である)。

この問題を解決するにはどうすればよいですか? JVMを介してメッセージを渡すことは可能でしょうか?ウィンドウAPIで使用したトリックと同様に、実行中のJavaアプリケーションやその行にあるJFrame.setIconImage()を呼び出しますか?

編集:私はちょうどのC#にバインドされていないです、私は私が私のメインアプリで実行しますJavaで「ヘルパー」アプリのようなものを書くのはとても喜んで、それが必要でなければなりません。

+2

私は好奇心が強い...あなたが視覚的にあなたがこれを試してみました – Gus

+0

アプリケーションを区別する簡単な方法を提供し、アイコンを取り除きたいなぜ? http://stackoverflow.com/questions/50398/calling-c-sharp-code-from-java – Diego

+0

@Diegoどうすれば助けになるのでしょうか?タスクバーに表示され、アイコンを変更したくない – hvd

答えて

0

あなたはあなたは、タスクバーのアイコンをオーバーレイするWindows API Code Packを使用することができます?:

[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

問題はEnumDesktopWindows代わりのEnumWindowsを使用しています。次のコードは、私の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

を持っていますが、アイデアを得る... –

+0

Isnと」システムのすべてのウィンドウを列挙するのが遅すぎますか? – remio

+0

@remioそれは私のPC上ではかなり高速ですし、もっと効率的な解決策がない限り、彼がやりたいことです。 –

関連する問題