System Icons Search in C # 1.0 and Alpha Channels

In a .NET 1.0 C # application, I want to display a list of files and folders in a listview control. I want to programmatically extract icons for files or folders from windows to display them as a list accordingly.

I'm currently using Windows Shell Shell32.dll, but I'm having problems with the alpha channel in the icons (the background of the icons appears as black, not white / transparent).

Below are two code snippets showing the API I am trying to use and the implemented code to retrieve the system icon for the folder (the code for the file is similar).

    [DllImport("Shell32.dll")]
    public static extern IntPtr SHGetFileInfo(
        string pszPath,
        uint dwFileAttributes,
        ref SHFILEINFO psfi,
        uint cbFileInfo,
        uint uFlags
        );

      

... (note: Shell32 is a wrapper class for the above API)

// Get the folder icon
            Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
            Shell32.SHGetFileInfo(  null, 
                Shell32.FILE_ATTRIBUTE_DIRECTORY, 
                ref shfi, 
                (uint) System.Runtime.InteropServices.Marshal.SizeOf(shfi), 
                flags );

            System.Drawing.Icon.FromHandle(shfi.hIcon); // Load from the handle

            // Get the icon for storage in an imagelist //
            System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();

      

Is this the correct approach?

Is there a better way to do this?

Or, is there something I need to do to set the alpha channel correctly in the icon?

+1


source to share


1 answer


In .NET 1.x, there is a documented (view) in KB822488 whereby alpha channels in icons are lost during conversion to an image (as happens when loading them into an ImageList). Unfortunately, the workaround in the article is not particularly useful for ListViews.



You may be able to use the Windows API to directly load icons into a list of images in the list, bypassing faulty .NET code. This article discusses getting icons from a list of system images and loading them into a ListView via the Windows API, so you can get what you need from there.

+2


source







All Articles