CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2012
    Posts
    2

    Removing titlebar and taskbar icons

    Hi all,

    I'm a fairly competent java developer, but have next no knowledge about c#.

    For my own personal use, I'd like to code an app that disables the icons on the titlebars of all visible windows of the windows os. I want all icons of the taskbar gone (exept the system tray area). This is how it should look when it's done: http://www.askvg.com/how-to-remove-p...skbar-buttons/ (this little autohotkey script is broken in win 7)
    I use PInvoke to enumerate all visible windows to send a message to them to replace their icon by a blank one.

    This is what I got so far from various code sniplets I found on the interwebs:

    Code:
    using System;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
     
    namespace RemoveAllApplicationIcons
    {
        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 getting window informations only from a handle
            [DllImport("user32.dll")]
            private static extern int GetWindowText(IntPtr hWnd, StringBuilder title, int size);
            [DllImport("user32.dll")]
            private static extern bool IsWindowVisible(IntPtr hWnd);
     
            // ... 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, IntPtr wParam, IntPtr lParam);
     
            // Setup some "global" variables
    
            /// Pointer to an empty icon used to replace all other application icons.
            static IntPtr m_pIcon = IntPtr.Zero;
     
            // List of filter window-titles
            static string[] m_astrFilter = new string[] { "Start", "Program Manager" };
     
            static void Main(string[] args)
            {
                // Load the empty icon (from a file in this example)
                string strIconFilePath = @"C:\blank.ico"; // Replace with a valid path on your system!
                const int IMAGE_ICON = 1;
                const int LR_LOADFROMFILE = 0x10;
                IntPtr pIcon = LoadImage(IntPtr.Zero, strIconFilePath, IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
     
                // enumerate all desktop windows
                EnumDesktopWindows(IntPtr.Zero, new EnumDesktopWindowsDelegate(EnumDesktopWindowsCallback), IntPtr.Zero);
     
                Console.ReadKey();
            }
     
            private static bool EnumDesktopWindowsCallback(IntPtr hWnd, int lParam)
            {
                // Get window title
                StringBuilder title = new StringBuilder(256);
                GetWindowText(hWnd, title, 256);
                string strTitle = title.ToString();
     
                // Get window visibillity
                bool bVisible = IsWindowVisible(hWnd);
     
                // window meets all criteria?
                if (bVisible && // ... visible
                   !String.IsNullOrEmpty(strTitle) && // ... has title
                   !m_astrFilter.Contains(strTitle)) // ... not in filter list
                {
                    // replace icon now
                    SendMessage(hWnd, 0x80, IntPtr.Zero, m_pIcon);
     
                    Console.WriteLine(title); // for debugging only
                }
     
                return true;
            }
        }
    }
    I get all the windows I want to send a message to (titles appear in the output) and the screen flashes when I execute the compiled exe, but all the icons remain unchanged.

    I would appreciate any help to get rid of those pesky icons. Thank you,
    ISOmorph

  2. #2
    Join Date
    Jan 2012
    Posts
    2

    Re: Removing titlebar and taskbar icons

    Someone in another forum was so kind to help me out.
    Here is the corrected version:

    Code:
    namespace IconKiller
    {
        class IconKiller
        {
            /// <summary>
            /// filter function
            /// </summary>
            public delegate bool EnumDelegate(IntPtr hWnd, int lParam);
     
            /// <summary>
            /// check if windows visible
            /// </summary>>
            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool IsWindowVisible(IntPtr hWnd);
     
            /// <summary>
            /// return windows text
            /// </summary>
            [DllImport("user32.dll", EntryPoint = "GetWindowText",
            ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
            public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);
     
            /// <summary>
            /// enumarator on all desktop windows
            /// </summary>
            [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
            ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
            public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
     
            /// <summary>
            /// Load icon
            /// </summary>
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            static extern IntPtr LoadImage(IntPtr hInst, string lpsz, IntPtr uType, IntPtr cxDesired, IntPtr cyDesired, IntPtr fuLoad);
     
            /// <summary>
            /// send message to windows
            /// </summary>
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
     
            /// <summary>
            /// entry point of the program
            /// </summary>
    
            private const string FILE = "blank.ico";
            private const UInt32 WM_SETICON = 0x80;
            private const int IMAGE_ICON = 1;
            private const int LR_DEFAULTSIZE = 0x40;
            private const int ICON_SMALL = 0;
           
            static void Main(string[] args)
            {
                // Load external icon file
                IntPtr icon = LoadImage(new IntPtr(0), FILE, new IntPtr(IMAGE_ICON), new IntPtr(0), new IntPtr(0), new IntPtr(LR_DEFAULTSIZE));
     
                // Enumerate all windows
                EnumDelegate filter = delegate(IntPtr hWnd, int lParam)
                {
                    // Get the windows title
                    StringBuilder strbTitle = new StringBuilder(255);
                    int nLength = GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
                    string strTitle = strbTitle.ToString();
     
                    // Choose all active windows with a title
                    if (IsWindowVisible(hWnd) && string.IsNullOrEmpty(strTitle) == false && strTitle != "Start" && strTitle != "Program Manager")
                    {
                        // Output the windows title and send a message to replace the icon
                        Console.WriteLine(strTitle);
                        SendMessage(hWnd, WM_SETICON, new IntPtr(ICON_SMALL), icon);
                    }
                    return true;
                };
     
                Console.Read();
            }
        }
    }

    I still could need some help figuring out how to listen to new window open events, so that I may re-execute the method in a while(true) loop.

    Thank you in advance,
    ISOmorph

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Removing titlebar and taskbar icons

    Have a look at this article - it is in VB.NEt, but it may still help you, especially with the correct API's to use :

    http://www.codeguru.com/vb/gen/vb_ge...le.php/c15757/

    Just for interest sake

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured