2012-01-19 60 views
35

Tôi đã viết một ứng dụng nhỏ vô hiệu hóa thanh tiêu đề và biểu tượng thanh tác vụ của tất cả các cửa sổ của HĐH Windows trong C#. Đây là mã:Làm thế nào tôi có thể loại bỏ các thanh tiêu đề và thanh tác vụ của các chương trình Java trên 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; 
     } 
    } 
} 

Mã này có vẻ hoạt động tốt với các ứng dụng cửa sổ gốc. Vấn đề duy nhất của tôi bây giờ là Java rõ ràng sử dụng một thể hiện khác của biểu tượng ứng dụng của nó để hiển thị trong thanh tác vụ. Có nghĩa là ứng dụng nhỏ của tôi loại bỏ biểu tượng trong thanh tiêu đề của các chương trình Java, nhưng không phải là một trong thanh tác vụ (Netbeans là một ví dụ tốt).

Cách giải quyết vấn đề đó? Có thể truyền thông điệp tới các chương trình đó thông qua JVM, tương tự như mẹo tôi đã sử dụng với API cửa sổ, để gọi JFrame.setIconImage() khi chạy các ứng dụng Java hay gì đó dọc theo các dòng đó không?

EDIT: Tôi không bị ràng buộc chỉ C#, tôi rất sẵn sàng viết một cái gì đó giống như một ứng dụng "trợ giúp" trong java mà tôi sẽ thực hiện trong ứng dụng chính của tôi, nếu cần thiết.

+2

Tôi tò mò ... tại sao bạn muốn loại bỏ các biểu tượng cung cấp cách dễ dàng phân biệt các ứng dụng – Gus

+0

Bạn đã thử cách này chưa? http://stackoverflow.com/questions/50398/calling-c-sharp-code-from-java – Diego

+0

@Diego Điều đó sẽ giúp ích như thế nào? – hvd

Trả lời

0

Đây có phải là những gì bạn đang tìm kiếm ?:

[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

Sự cố đang sử dụng EnumDesktopWindows thay vì EnumWindows. Mã sau đây hoạt động tốt trên máy tính của tôi:

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

tất nhiên, bạn cần thực hiện một số chỉnh sửa nhỏ để tạo mã phù hợp nhưng bạn có ý tưởng ... –

+0

Isn ' t nó quá chậm để liệt kê tất cả các cửa sổ của hệ thống? – remio

+0

@remio Nó thực hiện khá nhanh trên máy tính của tôi và đó là những gì anh ấy muốn làm ... trừ khi bạn có một giải pháp hiệu quả hơn. –

Các vấn đề liên quan