Using a 256 x 256 Windows Vista icon in an application

前端 未结 5 1166
离开以前
离开以前 2020-12-08 09:03

I have an application which I have made a 256 x 256 Windows Vista icon for.

I was wondering how I would be able to use a 256x256 PNG file in the ico file used a

5条回答
  •  旧时难觅i
    2020-12-08 09:09

    Found info here. To get the large Vista icon, you need to use Shell32's SHGetFileInfo method. I've copied the relevant text below, of course you'll want to replace the filename variable with "Assembly.GetExecutingAssembly().Location".

    using System.Runtime.InteropServices;
    

    A bunch of constants we will use in the call to SHGetFileInfo() to specify the size of the icon we wish to retrieve:

    // Constants that we need in the function call
    private const int SHGFI_ICON = 0x100;
    private const int SHGFI_SMALLICON = 0x1;
    private const int SHGFI_LARGEICON = 0x0;
    

    The SHFILEINFO structure is very important as it will be our handle to various file information, among which is the graphic icon.

    // This structure will contain information about the file
    public struct SHFILEINFO
    {
        // Handle to the icon representing the file
        public IntPtr hIcon;
        // Index of the icon within the image list
        public int iIcon;
        // Various attributes of the file
        public uint dwAttributes;
        // Path to the file
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
        public string szDisplayName;
        // File type
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
        public string szTypeName;
    };
    

    The final preparation for the unmanaged code is to define the signature of SHGetFileInfo, which is located inside the popular Shell32.dll:

    // The signature of SHGetFileInfo (located in Shell32.dll)
    [DllImport("Shell32.dll")]
    public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, uint uFlags);
    

    Now that we have everything prepared, it's time to make the call to the function and display the icon that we retrieved. The object that will be retrieved is an Icon type (System.Drawing.Icon) but we want to display it in a PictureBox so we'll convert the Icon to a Bitmap using the ToBitmap() method.

    But first of all there are 3 controls you need to add to the form, a Button btnExtract that has "Extract Icon" for its Text property, picIconSmall which is a PictureBox and a picIconLarge which is also a PictureBox. That's because we will get two icons sizes. Now double click btnExtract in Visual Studio's Design view and you'll get to its Click event. Inside it is the rest of the code:

    private void btnExtract_Click(object sender, EventArgs e)
    {
        // Will store a handle to the small icon
        IntPtr hImgSmall;
        // Will store a handle to the large icon
        IntPtr hImgLarge;
    
        SHFILEINFO shinfo = new SHFILEINFO();
    
        // Open the file that we wish to extract the icon from
        if(openFile.ShowDialog() == DialogResult.OK)
        {
            // Store the file name
            string FileName = openFile.FileName;
            // Sore the icon in this myIcon object
            System.Drawing.Icon myIcon;
    
            // Get a handle to the small icon
            hImgSmall = SHGetFileInfo(FileName, 0, ref shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON);
            // Get the small icon from the handle
            myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
            // Display the small icon
            picIconSmall.Image = myIcon.ToBitmap();
    
            // Get a handle to the large icon
            hImgLarge = SHGetFileInfo(FileName, 0, ref shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON);
            // Get the large icon from the handle
            myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
            // Display the large icon
            picIconLarge.Image = myIcon.ToBitmap();
    
        }
    }
    

    UPDATE: found even more info here.

提交回复
热议问题