using System; using System.Collections.Generic; using System.Text; using CPF.Input; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; using System.Runtime.Serialization.Formatters.Binary; using CPF.Drawing; //using IOleDataObject = System.Runtime.InteropServices.ComTypes.IDataObject; namespace CPF.Windows { class OleDataObject : Input.IDataObject { private IOleDataObject _wrapped; public OleDataObject(IOleDataObject wrapped) { _wrapped = wrapped; } public bool Contains(DataFormat dataFormat) { return GetDataFormatsCore().Any(df => df == dataFormat); } //public IEnumerable GetDataFormats() //{ // return GetDataFormatsCore().Distinct(); //} public object GetData(DataFormat dataFormat) { return GetDataFromOleHGLOBAL(dataFormat, DVASPECT.DVASPECT_CONTENT); } private object GetDataFromOleHGLOBAL(DataFormat format, DVASPECT aspect) { List fs = new List(); fs.Add((short)ClipboardImpl.GetFormatId(format)); if (format == DataFormat.Text) { fs.Add((short)UnmanagedMethods.ClipboardFormat.CF_TEXT); fs.Add((short)UnmanagedMethods.ClipboardFormat.CF_OEMTEXT); } foreach (var item in fs) { FORMATETC formatEtc = new FORMATETC(); formatEtc.cfFormat = item; formatEtc.dwAspect = aspect; formatEtc.lindex = -1; formatEtc.tymed = TYMED.TYMED_HGLOBAL; if (_wrapped.QueryGetData(ref formatEtc) == 0) { _wrapped.GetData(ref formatEtc, out STGMEDIUM medium); try { if (medium.unionmember != IntPtr.Zero && medium.tymed == (int)TYMED.TYMED_HGLOBAL) { if (format == DataFormat.Text) return ReadStringFromHGlobal(medium.unionmember); if (format == DataFormat.FileNames) return ReadFileNamesFromHGlobal(medium.unionmember); if (format == DataFormat.Image) { IntPtr ptr = UnmanagedMethods.GlobalLock(medium.unionmember); //默认是24位的图,需要绘制到32位的位图里 #if Net4 var bmp = (UnmanagedMethods.BITMAPINFOHEADER)Marshal.PtrToStructure(ptr, typeof(UnmanagedMethods.BITMAPINFOHEADER)); #else var bmp = Marshal.PtrToStructure(ptr); #endif IntPtr screenDC = UnmanagedMethods.GetDC(IntPtr.Zero); IntPtr memDc = UnmanagedMethods.CreateCompatibleDC(screenDC); UnmanagedMethods.BITMAPINFOHEADER info = new UnmanagedMethods.BITMAPINFOHEADER(); info.biSize = (uint)Marshal.SizeOf(typeof(UnmanagedMethods.BITMAPINFOHEADER)); info.biBitCount = 32; info.biHeight = bmp.biHeight; info.biWidth = bmp.biWidth; info.biPlanes = 1; var hBitmap = UnmanagedMethods.CreateDIBSection(memDc, ref info, 0, out IntPtr ppvBits, IntPtr.Zero, 0); var oldBits = UnmanagedMethods.SelectObject(memDc, hBitmap);//将位图载入上下文 _ = UnmanagedMethods.StretchDIBits(memDc, 0, 0, bmp.biWidth, bmp.biHeight, 0, 0, bmp.biWidth, bmp.biHeight, (ptr + 40), ref bmp, 0, (uint)TernaryRasterOperations.SRCCOPY); var img = ClipboardImpl.ImageFormHBitmap(hBitmap); //var img = new Bitmap(bmp.biWidth, Math.Abs(bmp.biHeight), bmp.biWidth * 4, PixelFormat.PRgba, ppvBits).Clone(); UnmanagedMethods.SelectObject(memDc, oldBits); UnmanagedMethods.ReleaseDC(IntPtr.Zero, screenDC); UnmanagedMethods.DeleteDC(memDc); UnmanagedMethods.DeleteObject(hBitmap); UnmanagedMethods.GlobalUnlock(medium.unionmember); return img; } byte[] data = ReadBytesFromHGlobal(medium.unionmember); if (format == DataFormat.Html) { var html= Encoding.UTF8.GetString(data); if (!string.IsNullOrWhiteSpace(html)) { var start = html.IndexOf("