using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.IO;
namespace Vestris.ResourceLib
{
///
/// This structure depicts the organization of data in a .ico file.
///
public class IconFile
{
///
/// Resource type.
///
public enum GroupType
{
///
/// Icon.
///
Icon = 1,
///
/// Cursor.
///
Cursor = 2
}
Kernel32.FILEGRPICONDIR _header = new Kernel32.FILEGRPICONDIR();
List _icons = new List();
///
/// Type of the group icon resource.
///
public GroupType Type
{
get
{
return (GroupType)_header.wType;
}
set
{
_header.wType = (byte)value;
}
}
///
/// Collection of icons in an .ico file.
///
public List Icons
{
get
{
return _icons;
}
set
{
_icons = value;
}
}
///
/// An existing .ico file.
///
/// An existing icon (.ico) file.
public IconFile(string filename)
{
LoadFrom(filename);
}
///
/// Load from a .ico file.
///
/// An existing icon (.ico) file.
public void LoadFrom(string filename)
{
byte[] data = File.ReadAllBytes(filename);
IntPtr lpData = Marshal.AllocHGlobal(data.Length);
try
{
Marshal.Copy(data, 0, lpData, data.Length);
Read(lpData);
}
finally
{
Marshal.FreeHGlobal(lpData);
}
}
///
/// Read icons.
///
/// Pointer to the beginning of a FILEGRPICONDIR structure.
/// Pointer to the end of a FILEGRPICONDIR structure.
internal IntPtr Read(IntPtr lpData)
{
_icons.Clear();
_header = (Kernel32.FILEGRPICONDIR)Marshal.PtrToStructure(
lpData, typeof(Kernel32.FILEGRPICONDIR));
IntPtr lpEntry = new IntPtr(lpData.ToInt32() + Marshal.SizeOf(_header));
for (int i = 0; i < _header.wCount; i++)
{
IconFileIcon iconFileIcon = new IconFileIcon();
lpEntry = iconFileIcon.Read(lpEntry, lpData);
_icons.Add(iconFileIcon);
}
return lpEntry;
}
}
}