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 hardware-independent icon resource. /// public class DirectoryResource : Resource where ImageResourceType : IconImageResource, new() { Kernel32.GRPICONDIR _header = new Kernel32.GRPICONDIR(); List _icons = new List(); /// /// Returns the type of the resource in this group. /// public Kernel32.ResourceTypes ResourceType { get { switch (_header.wType) { case 1: return Kernel32.ResourceTypes.RT_ICON; case 2: return Kernel32.ResourceTypes.RT_CURSOR; default: throw new NotSupportedException(); } } } /// /// Icons contained in this hardware-independent icon resource. /// public List Icons { get { return _icons; } set { _icons = value; } } /// /// A hardware-independent icon resource. /// /// Module handle. /// Resource ID. /// Resource type. /// Resource name. /// Language ID. /// Resource size. internal DirectoryResource(IntPtr hModule, IntPtr hResource, ResourceId type, ResourceId name, UInt16 language, int size) : base(hModule, hResource, type, name, language, size) { } /// /// A new hardware-independent icon resource. /// public DirectoryResource(Kernel32.ResourceTypes resourceType) : base(IntPtr.Zero, IntPtr.Zero, new ResourceId(resourceType), new ResourceId(1), ResourceUtil.NEUTRALLANGID, Marshal.SizeOf(typeof(Kernel32.GRPICONDIR))) { switch(resourceType) { case Kernel32.ResourceTypes.RT_GROUP_CURSOR: _header.wType = 2; break; case Kernel32.ResourceTypes.RT_GROUP_ICON: _header.wType = 1; break; default: throw new NotSupportedException(); } } /// /// Save a hardware-independent icon resource to an executable file. /// /// Name of an executable file (.exe or .dll). public override void SaveTo(string filename) { base.SaveTo(filename); foreach (ImageResourceType icon in _icons) { icon.SaveIconTo(filename); } } /// /// Read a hardware-independent icon resource from a loaded module. /// /// Loaded executable module. /// Pointer to the beginning of a hardware-independent icon resource. /// Pointer to the end of the hardware-independent icon resource. internal override IntPtr Read(IntPtr hModule, IntPtr lpRes) { _icons.Clear(); _header = (Kernel32.GRPICONDIR)Marshal.PtrToStructure( lpRes, typeof(Kernel32.GRPICONDIR)); IntPtr pEntry = new IntPtr(lpRes.ToInt32() + Marshal.SizeOf(_header)); for (UInt16 i = 0; i < _header.wImageCount; i++) { ImageResourceType iconImageResource = new ImageResourceType(); pEntry = iconImageResource.Read(hModule, pEntry); _icons.Add(iconImageResource); } return pEntry; } /// /// Write a hardware-independent icon resource to a binary stream. /// /// Binary stream. internal override void Write(BinaryWriter w) { w.Write((UInt16)_header.wReserved); w.Write((UInt16)_header.wType); w.Write((UInt16)_icons.Count); ResourceUtil.PadToWORD(w); foreach (ImageResourceType icon in _icons) { icon.Write(w); } } } }