如何在Windows 7上删除Java程序的标题栏和任务栏图标?

2022-09-01 09:06:31

我写了一个小应用程序,禁用了C#中Windows操作系统所有窗口的标题栏和任务栏图标。代码如下:

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

此代码似乎适用于本机 Windows 应用程序。我现在唯一的问题是,Java显然使用其应用程序图标的不同实例在任务栏中显示。这意味着我的小应用程序删除了Java程序标题栏中的图标,但删除了任务栏中的图标(Netbeans就是一个很好的例子)。

如何解决此问题?是否有可能通过JVM向这些程序传递消息,类似于我在Windows API中使用的技巧,以调用正在运行的Java应用程序或类似的东西?JFrame.setIconImage()

编辑:我不仅限于C#,我非常愿意用java编写一些类似于“帮助器”应用程序的东西,如果有必要,我会在我的主应用程序中执行。


答案 1

问题是使用EnumDesktopWindows而不是EnumWindows。以下代码在我的电脑上工作正常:

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

答案 2

这是你要找的吗?

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

推荐