protected virtual void ConvertToCursor(NativeMethods.FileHeader Header, List <NativeMethods.FileRecord> RecordList) { using (MemoryStream ms = new MemoryStream()) { byte[] FieldData; FieldData = BitConverter.GetBytes(Header.Reserved); ms.Write(FieldData, 0, FieldData.Length); FieldData = BitConverter.GetBytes(Header.Type); ms.Write(FieldData, 0, FieldData.Length); short ImageCount = (short)RecordList.Count; FieldData = BitConverter.GetBytes(ImageCount); ms.Write(FieldData, 0, FieldData.Length); int DataOffset = 6 + (RecordList.Count * 16); foreach (NativeMethods.FileRecord Record in RecordList) { ms.WriteByte(Record.bWidth); ms.WriteByte(Record.bHeight); ms.WriteByte(Record.bColorCount); ms.WriteByte(Record.bReserved); FieldData = BitConverter.GetBytes(Record.HotspotX); ms.Write(FieldData, 0, FieldData.Length); FieldData = BitConverter.GetBytes(Record.HotspotY); ms.Write(FieldData, 0, FieldData.Length); int DataLength = Record.Data.Length - 4; FieldData = BitConverter.GetBytes(DataLength); ms.Write(FieldData, 0, FieldData.Length); FieldData = BitConverter.GetBytes(DataOffset); ms.Write(FieldData, 0, FieldData.Length); DataOffset += DataLength; } foreach (NativeMethods.FileRecord Record in RecordList) { ms.Write(Record.Data, 4, Record.Data.Length - 4); } ms.Seek(0, SeekOrigin.Begin); AsCursor = new Cursor(ms); } }
/// <summary> /// Load the cursor directory with images. /// </summary> protected virtual bool LoadDirectory(IntPtr hMod, out NativeMethods.FileHeader Header, out List <NativeMethods.FileRecord> RecordList) { IntPtr hResDir = NativeMethods.FindResource(hMod, (IntPtr)ResourceID, (IntPtr)NativeMethods.RT_GROUP_CURSOR); if (hResDir == IntPtr.Zero) { Header = null; RecordList = null; return(false); } uint size = NativeMethods.SizeofResource(hMod, hResDir); IntPtr pt = NativeMethods.LoadResource(hMod, hResDir); byte[] bPtr = new byte[size]; Marshal.Copy(pt, bPtr, 0, (int)size); if (size < 6) { Header = null; RecordList = null; return(false); } Header = new NativeMethods.FileHeader(); Header.Reserved = BitConverter.ToInt16(bPtr, 0); Header.Type = BitConverter.ToInt16(bPtr, 2); Header.ImageCount = BitConverter.ToInt16(bPtr, 4); List <NativeMethods.FileRecord> TempRecordList = new List <NativeMethods.FileRecord>(); NativeMethods.FileRecord GoodSizedImage = null; for (short i = 0; i < Header.ImageCount; i++) { int Offset = (i * 14) + 6; NativeMethods.FileRecord Record = new NativeMethods.FileRecord(); Record.bWidth = bPtr[Offset + 0]; Record.bHeight = Record.bWidth; short wBitCount = BitConverter.ToInt16(bPtr, Offset + 6); Record.bColorCount = (byte)Math.Pow(2, wBitCount); Record.bReserved = 0; Record.nID = BitConverter.ToUInt16(bPtr, Offset + 12); TempRecordList.Add(Record); if (Record.bWidth == PreferredSize) { GoodSizedImage = Record; } } RecordList = new List <NativeMethods.FileRecord>(); if (GoodSizedImage != null) { RecordList.Add(GoodSizedImage); } else { RecordList.AddRange(TempRecordList); } return(true); }
/// <summary> /// Load the cursor directory with images. /// </summary> /// <param name="hMod">Handle of the resource file.</param> /// <param name="header">Header of the resource file.</param> /// <param name="recordList">List of record.</param> /// <returns>True if successful; otherwise, false.</returns> protected virtual bool LoadDirectory(IntPtr hMod, out NativeMethods.FileHeader header, out List <NativeMethods.FileRecord> recordList) { IntPtr hResDir = NativeMethods.FindResource(hMod, (IntPtr)ResourceID, (IntPtr)NativeMethods.RT_GROUP_CURSOR); if (hResDir == IntPtr.Zero) { Contract.Unused(out header); Contract.Unused(out recordList); return(false); } uint size = NativeMethods.SizeofResource(hMod, hResDir); if (size < 6) { Contract.Unused(out header); Contract.Unused(out recordList); return(false); } IntPtr pt = NativeMethods.LoadResource(hMod, hResDir); byte[] bPtr = new byte[size]; Marshal.Copy(pt, bPtr, 0, (int)size); header = new NativeMethods.FileHeader { Reserved = BitConverter.ToInt16(bPtr, 0), Type = BitConverter.ToInt16(bPtr, 2), ImageCount = BitConverter.ToInt16(bPtr, 4), }; recordList = new List <NativeMethods.FileRecord>(); List <NativeMethods.FileRecord> TempRecordList = new List <NativeMethods.FileRecord>(); bool IsGoodSizedImageAdded = false; for (short i = 0; i < header.ImageCount; i++) { int Offset = (i * 14) + 6; NativeMethods.FileRecord Record = new NativeMethods.FileRecord(); Record.bWidth = bPtr[Offset + 0]; Record.bHeight = Record.bWidth; short wBitCount = BitConverter.ToInt16(bPtr, Offset + 6); Record.bColorCount = (byte)Math.Pow(2, wBitCount); Record.bReserved = 0; Record.nID = BitConverter.ToUInt16(bPtr, Offset + 12); TempRecordList.Add(Record); if (Record.bWidth == PreferredSize) { IsGoodSizedImageAdded = true; recordList.Add(Record); } } if (!IsGoodSizedImageAdded) { recordList.AddRange(TempRecordList); } return(true); }