2012-01-19 10 views
35

मैंने एक छोटा ऐप लिखा है जो सी # में विंडोज ओएस की सभी विंडो के टाइटलबार और टास्कबार आइकन अक्षम करता है। यहां कोड है:मैं विंडोज 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; 
     } 
    } 
} 

यह कोड देशी विंडोज अनुप्रयोगों के साथ ठीक काम करता प्रतीत होता है। मेरी एकमात्र समस्या यह है कि जावा स्पष्ट रूप से टास्कबार में प्रदर्शित करने के लिए अपने एप्लिकेशन आइकन के एक अलग उदाहरण का उपयोग करता है। मतलब यह है कि मेरा छोटा ऐप जावा प्रोग्राम के टाइटलबार में आइकन को हटा देता है, लेकिन टास्कबार में से एक नहीं (नेटबीन्स एक अच्छा उदाहरण है)।

उस समस्या को हल करने के लिए कैसे करें? जावा अनुप्रयोगों या उन पंक्तियों के साथ कुछ चलाने पर JFrame.setIconImage() पर कॉल करने के लिए, विंडोज एपीआई के साथ उपयोग की जाने वाली चाल के समान, जेवीएम के माध्यम से उन कार्यक्रमों के लिए एक संदेश पास करना संभव हो सकता है?

संपादित करें: मैं सिर्फ सी # के लिए बाध्य नहीं हूं, मैं जावा में "सहायक" ऐप की तरह कुछ लिखने के लिए तैयार हूं, जिसे मैं अपने मुख्य ऐप में निष्पादित करता हूं, यह आवश्यक होना चाहिए।

+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

समस्या EnumWindows के बजाय EnumDesktopWindows का उपयोग कर रही है। निम्नलिखित कोड मेरे पीसी पर ठीक काम करता है:

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 यह मेरे पीसी पर बहुत तेज़ करता है और यही वह करना चाहता है ... जब तक कि आपके पास अधिक कुशल समाधान न हो। –

संबंधित मुद्दे