using System; using System.Globalization; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; namespace Vanara.PInvoke { /// /// CLIPFORMAT is a 2-byte value representing a clipboard format. /// /// This cannot be used as a drop-in replacement for many of the winuser.h function as they expect a 4-byte value. However, this can /// automatically convert between the 4-byte values and the 2-byte value. /// /// /// /// /// [StructLayout(LayoutKind.Sequential)] [PInvokeData("wtypes.h", MSDNShortId = "fe42baec-6b00-4816-b379-7f335da8a197")] public partial struct CLIPFORMAT : IComparable, IComparable, IEquatable { internal readonly ushort _value; /// Initializes a new instance of the structure. /// The raw clipboard format value. public CLIPFORMAT(ushort rawValue) => _value = rawValue; /// Compares the current object with another object of the same type. /// An object to compare with this object. /// /// A value that indicates the relative order of the objects being compared. The return value has the following /// meanings: Value Meaning Less than zero This object is less than the parameter.Zero This object is equal /// to . Greater than zero This object is greater than . /// public int CompareTo(CLIPFORMAT other) => _value.CompareTo(other._value); /// /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current /// instance precedes, follows, or occurs in the same position in the sort order as the other object. /// /// An object to compare with this instance. /// /// A value that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less /// than zero This instance precedes in the sort order. Zero This instance occurs in the same position in the /// sort order as . Greater than zero This instance follows in the sort order. /// public int CompareTo(object obj) { if (obj is not IConvertible c) throw new ArgumentException(@"Object cannot be converted to a UInt16 value for comparison.", nameof(obj)); return _value.CompareTo(c.ToUInt16(null)); } /// Determines whether the specified , is equal to this instance. /// The to compare with this instance. /// true if the specified is equal to this instance; otherwise, false. public override bool Equals(object obj) => obj is IConvertible c && _value.Equals(c.ToUInt16(null)); /// Indicates whether the current object is equal to another object of the same type. /// An object to compare with this object. /// true if the current object is equal to the parameter; otherwise, false. public bool Equals(CLIPFORMAT other) => other._value == _value; /// Returns a hash code for this instance. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. public override int GetHashCode() => _value.GetHashCode(); /// Returns a that represents this instance. /// A that represents this instance. public override string ToString() { var type = GetType(); foreach (var fi in type.GetFields(BindingFlags.Static | BindingFlags.Public).Where(f => f.FieldType == type && f.IsInitOnly)) if (fi.GetValue(null) is CLIPFORMAT cf && cf._value == _value) return $"{fi.Name} ({HexStr(this)})"; return HexStr(this); static string HexStr(in CLIPFORMAT cf) => string.Format(CultureInfo.InvariantCulture, "0x{0:X4}", cf._value); } /// Implements the operator ==. /// The first . /// The second . /// The result of the operator. public static bool operator ==(CLIPFORMAT hrLeft, CLIPFORMAT hrRight) => hrLeft._value == hrRight._value; /// Implements the operator ==. /// The first . /// The second . /// The result of the operator. public static bool operator ==(CLIPFORMAT hrLeft, ushort hrRight) => hrLeft._value == hrRight; /// Implements the operator !=. /// The first . /// The second . /// The result of the operator. public static bool operator !=(CLIPFORMAT hrLeft, CLIPFORMAT hrRight) => !(hrLeft == hrRight); /// Implements the operator !=. /// The first . /// The second . /// The result of the operator. public static bool operator !=(CLIPFORMAT hrLeft, ushort hrRight) => !(hrLeft == hrRight); /// Performs an implicit conversion from to . /// The value. /// The result of the conversion. public static implicit operator CLIPFORMAT(ushort value) => new(value); /// Performs an implicit conversion from to . /// The value. /// The result of the conversion. public static implicit operator CLIPFORMAT(short value) => new(unchecked((ushort)value)); /// Performs an implicit conversion from to . /// The value. /// The result of the conversion. public static implicit operator CLIPFORMAT(uint value) => new((ushort)value); /// Performs an implicit conversion from to . /// The value. /// The result of the conversion. public static implicit operator CLIPFORMAT(int value) => new((ushort)value); /// Performs an explicit conversion from to . /// The value. /// The result of the conversion. public static implicit operator ushort(CLIPFORMAT value) => value._value; /// Performs an explicit conversion from to . /// The value. /// The result of the conversion. public static implicit operator short(CLIPFORMAT value) => unchecked((short)value._value); /// Performs an explicit conversion from to . /// The value. /// The result of the conversion. public static implicit operator uint(CLIPFORMAT value) => value._value; /// Performs an explicit conversion from to . /// The value. /// The result of the conversion. public static implicit operator int(CLIPFORMAT value) => value._value; /// A handle to a bitmap (HBITMAP). [ClipCorrespondingType(typeof(HBITMAP))] public static readonly CLIPFORMAT CF_BITMAP = 2; /// A memory object containing a BITMAPINFO structure followed by the bitmap bits. public static readonly CLIPFORMAT CF_DIB = 8; /// /// A memory object containing a BITMAPV5HEADER structure followed by the bitmap color space information and the bitmap bits. /// public static readonly CLIPFORMAT CF_DIBV5 = 17; /// Software Arts' Data Interchange Format. public static readonly CLIPFORMAT CF_DIF = 5; /// /// Bitmap display format associated with a private format. The hMem parameter must be a handle to data that can be displayed in /// bitmap format in lieu of the privately formatted data. /// public static readonly CLIPFORMAT CF_DSPBITMAP = 0x0082; /// /// Enhanced metafile display format associated with a private format. The hMem parameter must be a handle to data that can be /// displayed in enhanced metafile format in lieu of the privately formatted data. /// public static readonly CLIPFORMAT CF_DSPENHMETAFILE = 0x008E; /// /// Metafile-picture display format associated with a private format. The hMem parameter must be a handle to data that can be /// displayed in metafile-picture format in lieu of the privately formatted data. /// public static readonly CLIPFORMAT CF_DSPMETAFILEPICT = 0x0083; /// /// Text display format associated with a private format. The hMem parameter must be a handle to data that can be displayed in text /// format in lieu of the privately formatted data. /// public static readonly CLIPFORMAT CF_DSPTEXT = 0x0081; /// A handle to an enhanced metafile (HENHMETAFILE). [ClipCorrespondingType(typeof(HENHMETAFILE), TYMED.TYMED_ENHMF)] public static readonly CLIPFORMAT CF_ENHMETAFILE = 14; /// /// Start of a range of integer values for application-defined GDI object clipboard formats. The end of the range is CF_GDIOBJLAST. /// /// Handles associated with clipboard formats in this range are not automatically deleted using the GlobalFree function when the /// clipboard is emptied. Also, when using values in this range, the hMem parameter is not a handle to a GDI object, but is a handle /// allocated by the GlobalAlloc function with the GMEM_MOVEABLE flag. /// /// [ClipCorrespondingType(typeof(int))] public static readonly CLIPFORMAT CF_GDIOBJFIRST = 0x0300; /// See CF_GDIOBJFIRST. [ClipCorrespondingType(typeof(int))] public static readonly CLIPFORMAT CF_GDIOBJLAST = 0x03FF; /// /// A handle to type HDROP that identifies a list of files. An application can retrieve information about the files by passing the /// handle to the DragQueryFile function. /// public static readonly CLIPFORMAT CF_HDROP = 15; /// /// The data is a handle to the locale identifier associated with text in the clipboard. When you close the clipboard, if it contains /// CF_TEXT data but no CF_LOCALE data, the system automatically sets the CF_LOCALE format to the current input language. You can use /// the CF_LOCALE format to associate a different locale with the clipboard text. /// /// An application that pastes text from the clipboard can retrieve this format to determine which character set was used to generate /// the text. /// /// /// Note that the clipboard does not support plain text in multiple character sets. To achieve this, use a formatted text data type /// such as RTF instead. /// /// /// The system uses the code page associated with CF_LOCALE to implicitly convert from CF_TEXT to CF_UNICODETEXT. Therefore, the /// correct code page table is used for the conversion. /// /// [ClipCorrespondingType(typeof(LCID))] public static readonly CLIPFORMAT CF_LOCALE = 16; /// /// Handle to a metafile picture format as defined by the METAFILEPICT structure. When passing a CF_METAFILEPICT handle by means of /// DDE, the application responsible for deleting hMem should also free the metafile referred to by the CF_METAFILEPICT handle. /// [ClipCorrespondingType(typeof(HMETAFILE), TYMED.TYMED_MFPICT)] public static readonly CLIPFORMAT CF_METAFILEPICT = 3; /// /// Text format containing characters in the OEM character set. Each line ends with a carriage return/linefeed (CR-LF) combination. A /// null character signals the end of the data. /// [ClipCorrespondingType(typeof(string), TYMED.TYMED_HGLOBAL, EncodingType = typeof(System.Text.ASCIIEncoding))] public static readonly CLIPFORMAT CF_OEMTEXT = 7; /// /// Owner-display format. The clipboard owner must display and update the clipboard viewer window, and receive the /// WM_ASKCBFORMATNAME, WM_HSCROLLCLIPBOARD, WM_PAINTCLIPBOARD, WM_SIZECLIPBOARD, and WM_VSCROLLCLIPBOARD messages. The hMem /// parameter must be NULL. /// [ClipCorrespondingType(null, TYMED.TYMED_NULL)] public static readonly CLIPFORMAT CF_OWNERDISPLAY = 0x0080; /// /// Handle to a color palette. Whenever an application places data in the clipboard that depends on or assumes a color palette, it /// should place the palette on the clipboard as well. /// /// If the clipboard contains data in the CF_PALETTE (logical color palette) format, the application should use the SelectPalette and /// RealizePalette functions to realize (compare) any other data in the clipboard against that logical palette. /// /// /// When displaying clipboard data, the clipboard always uses as its current palette any object on the clipboard that is in the /// CF_PALETTE format. /// /// public static readonly CLIPFORMAT CF_PALETTE = 9; /// Data for the pen extensions to the Microsoft Windows for Pen Computing. public static readonly CLIPFORMAT CF_PENDATA = 10; /// /// Start of a range of integer values for private clipboard formats. The range ends with CF_PRIVATELAST. Handles associated with /// private clipboard formats are not freed automatically; the clipboard owner must free such handles, typically in response to the /// WM_DESTROYCLIPBOARD message. /// [ClipCorrespondingType(typeof(int))] public static readonly CLIPFORMAT CF_PRIVATEFIRST = 0x0200; /// See CF_PRIVATEFIRST. [ClipCorrespondingType(typeof(int))] public static readonly CLIPFORMAT CF_PRIVATELAST = 0x02FF; /// Represents audio data more complex than can be represented in a CF_WAVE standard wave format. public static readonly CLIPFORMAT CF_RIFF = 11; /// Microsoft Symbolic Link (SYLK) format. public static readonly CLIPFORMAT CF_SYLK = 4; /// /// Text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character signals the end of the data. /// Use this format for ANSI text. /// [ClipCorrespondingType(typeof(string), TYMED.TYMED_HGLOBAL, EncodingType = typeof(System.Text.ASCIIEncoding))] public static readonly CLIPFORMAT CF_TEXT = 1; /// Tagged-image file format. public static readonly CLIPFORMAT CF_TIFF = 6; /// /// Unicode text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character signals the end of the data. /// [ClipCorrespondingType(typeof(string), TYMED.TYMED_HGLOBAL, EncodingType = typeof(System.Text.UnicodeEncoding))] public static readonly CLIPFORMAT CF_UNICODETEXT = 13; /// Represents audio data in one of the standard wave formats, such as 11 kHz or 22 kHz PCM. public static readonly CLIPFORMAT CF_WAVE = 12; } /// Indicates the type, medium and method for getting and setting the payload associated with known clipboard formats. /// [AttributeUsage(AttributeTargets.Field | AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = true)] public class ClipCorrespondingTypeAttribute : Vanara.InteropServices.CorrespondingTypeAttribute { /// Initializes a new instance of the class. /// The type that corresponds to this entity. /// The medium type used to store the payload. public ClipCorrespondingTypeAttribute(Type typeRef, TYMED medium = TYMED.TYMED_HGLOBAL) : base(typeRef) { Medium = medium; } /// Gets the medium type used to store the payload. /// The medium type. public TYMED Medium { get; } /// /// Gets or sets the formatter type used to place the contents of the object of . If not specified, it can be assumed the type will be directly marshaled. /// /// The optional formatter type. public Type Formatter { get; set; } } /// A formatter used to get and set objects on the clipboard. When implemented, use the property to /// associate a clipboard format public interface IClipboardFormatter { /// Extracts the object from an HGLOBAL handle. /// The HGLOBAL handle. /// The extracted object. public object Read(IntPtr hGlobal); /// Inserts the specified object into an allocated HGLOBAL handle. /// The object value. /// /// A pointer to allocated memory holding the content. This should be created using GlobalAlloc with the GMEM_MOVEABLE flag. /// public IntPtr Write(object value); } }