Beispiel #1
0
 public HRESULT TxGetNaturalSize2(
     DVASPECT dwAspect,
     IntPtr hdcDraw,
     IntPtr hicTargetDev,
     IntPtr ptd,
     TXTNATURALSIZE dwMode,
     ref tagSIZE psizelExtent,
     ref int pwidth,
     ref int pheight,
     out int pascent) => _txGetNaturalSize2(_unk, dwAspect, hdcDraw, hicTargetDev, ptd, dwMode, ref psizelExtent, ref pwidth, ref pheight, out pascent);
 public static FORMATETC GetFormatEtc(short id, int lindex = -1, DVASPECT dwAspect = DVASPECT.DVASPECT_CONTENT)
 {
     return(new FORMATETC
     {
         cfFormat = id,
         dwAspect = dwAspect,
         lindex = lindex,
         tymed = TYMED.TYMED_HGLOBAL | TYMED.TYMED_GDI | TYMED.TYMED_ISTREAM | TYMED.TYMED_ISTORAGE |
                 TYMED.TYMED_GDI | TYMED.TYMED_FILE | TYMED.TYMED_MFPICT | TYMED.TYMED_ENHMF
     });
 }
        private unsafe object GetDataFromOleHGLOBAL(string format, DVASPECT aspect)
        {
            var formatEtc = new Interop.FORMATETC();

            formatEtc.cfFormat = ClipboardFormats.GetFormat(format);
            formatEtc.dwAspect = aspect;
            formatEtc.lindex   = -1;
            formatEtc.tymed    = TYMED.TYMED_HGLOBAL;
            if (_wrapped.QueryGetData(&formatEtc) == 0)
            {
                Interop.STGMEDIUM medium = default;
                _ = _wrapped.GetData(&formatEtc, &medium);
                try
                {
                    if (medium.unionmember != IntPtr.Zero && medium.tymed == TYMED.TYMED_HGLOBAL)
                    {
                        if (format == DataFormats.Text)
                        {
                            return(ReadStringFromHGlobal(medium.unionmember));
                        }
                        if (format == DataFormats.FileNames)
                        {
                            return(ReadFileNamesFromHGlobal(medium.unionmember));
                        }

                        byte[] data = ReadBytesFromHGlobal(medium.unionmember);

                        if (IsSerializedObject(data))
                        {
                            using (var ms = new MemoryStream(data))
                            {
                                ms.Position = DataObject.SerializedObjectGUID.Length;
                                BinaryFormatter binaryFormatter = new BinaryFormatter();
                                return(binaryFormatter.Deserialize(ms));
                            }
                        }
                        return(data);
                    }
                }
                finally
                {
                    UnmanagedMethods.ReleaseStgMedium(ref medium);
                }
            }
            return(null);
        }
Beispiel #4
0
            private bool GetDataPresent(string format, bool autoConvert, DVASPECT aspect, int index)
            {
                if (!autoConvert)
                {
                    DataStoreEntry[] entries;
                    DataStoreEntry dse;
                    DataStoreEntry naturalDse;

                    if (!_data.ContainsKey(format))
                    {
                        return false;
                    }

                    entries = (DataStoreEntry[])_data[format];
                    dse = null;
                    naturalDse = null;

                    // Find the entry with the given aspect and index
                    for (int i = 0; i < entries.Length; i++)
                    {
                        DataStoreEntry entry;

                        entry = entries[i];
                        if (entry.Aspect == aspect)
                        {
                            if (index == -1 || entry.Index == index)
                            {
                                dse = entry;
                                break;
                            }
                        }
                        if (entry.Aspect == DVASPECT.DVASPECT_CONTENT && entry.Index == 0)
                        {
                            naturalDse = entry;
                        }
                    }

                    // If we couldn't find a specific entry, we'll use
                    // aspect == Content and index == 0.
                    if (dse == null && naturalDse != null)
                    {
                        dse = naturalDse;
                    }

                    // If we still didn't find data, return false.
                    return (dse != null);
                }
                else
                {
                    string[] formats;

                    formats = GetFormats(autoConvert);
                    for (int i = 0; i < formats.Length; i++)
                    {
                        if (IsFormatEqual(format, formats[i]))
                        {
                            return true;
                        }
                    }
                    return false;
                }
            }
Beispiel #5
0
            //------------------------------------------------------
            //
            //  Private Methods
            //
            //------------------------------------------------------

            #region Private Methods

            private Object GetData(string format, bool autoConvert, DVASPECT aspect, int index)
            {
                Object baseVar;
                Object original;
                DataStoreEntry dataStoreEntry;

                dataStoreEntry = FindDataStoreEntry(format, aspect, index);

                baseVar = GetDataFromDataStoreEntry(dataStoreEntry, format);

                original = baseVar;

                if (autoConvert
                    && (dataStoreEntry == null || dataStoreEntry.AutoConvert)
                    && (baseVar == null || baseVar is MemoryStream))
                {
                    string[] mappedFormats;

                    mappedFormats = GetMappedFormats(format);
                    if (mappedFormats != null)
                    {
                        for (int i = 0; i < mappedFormats.Length; i++)
                        {
                            if (!IsFormatEqual(format, mappedFormats[i]))
                            {
                                DataStoreEntry foundDataStoreEntry;

                                foundDataStoreEntry = FindDataStoreEntry(mappedFormats[i], aspect, index);

                                baseVar = GetDataFromDataStoreEntry(foundDataStoreEntry, mappedFormats[i]);

                                if (baseVar != null && !(baseVar is MemoryStream))
                                {
                                    if (IsDataSystemBitmapSource(baseVar) || SystemDrawingHelper.IsBitmap(baseVar))
                                    {
                                        // Ensure Bitmap(BitmapSource or System.Drawing.Bitmap) data which
                                        // match with the requested format.
                                        baseVar = EnsureBitmapDataFromFormat(format, autoConvert, baseVar);
                                    }

                                    original = null;
                                    break;
                                }
                            }
                        }
                    }
                }

                if (original != null)
                {
                    return original;
                }
                else
                {
                    return baseVar;
                }
            }
Beispiel #6
0
            private bool GetDataPresentInner(string format, DVASPECT aspect, int index)
            {
                FORMATETC formatetc;
                int hr;

                formatetc = new FORMATETC();
                formatetc.cfFormat = (short)DataFormats.GetDataFormat(format).Id;
                formatetc.dwAspect = aspect;
                formatetc.lindex = index;

                for (int i=0; i<ALLOWED_TYMEDS.Length; i++)
                {
                    formatetc.tymed |= ALLOWED_TYMEDS[i];
                }

                hr = QueryGetDataInner(ref formatetc);

                return (hr == NativeMethods.S_OK);
            }
Beispiel #7
0
            private Object GetDataFromBoundOleDataObject(string format, DVASPECT aspect, int index)
            {
                Object data;

                data = null;

                data = GetDataFromOleOther(format, aspect, index);
                if (data == null)
                {
                    data = GetDataFromOleHGLOBAL(format, aspect, index);
                }
                if (data == null)
                {
                    data = GetDataFromOleIStream(format, aspect, index);
                }

                return data;
            }
 public static FORMATETC GetFormatEtc(string dataFormat, int lindex = -1,
                                      DVASPECT dwAspect             = DVASPECT.DVASPECT_CONTENT)
 {
     return(GetFormatEtc((short)GetFormatId(dataFormat), lindex, dwAspect));
 }
Beispiel #9
0
 unsafe HRESULT IOleObject.GetMiscStatus(DVASPECT dwAspect, OLEMISC *pdwStatus)
 {
     return(HRESULT.S_OK);
 }
Beispiel #10
0
                //------------------------------------------------------
                //
                //  Constructors
                //
                //------------------------------------------------------

                #region Constructors

                public DataStoreEntry(Object data, bool autoConvert, DVASPECT aspect, int index)
                {
                    this._data = data;
                    this._autoConvert = autoConvert;
                    this._aspect = aspect;
                    this._index = index;
                }
Beispiel #11
0
 private void SetData(string format, object data, bool autoConvert, DVASPECT aspect, int index)
 {
     // If we want to support setting data into an OLE data Object,
     // the code should be here.
     throw new InvalidOperationException(SR.Get(SRID.DataObject_CannotSetDataOnAFozenOLEDataDbject));
 }
Beispiel #12
0
            private bool GetDataPresent(string format, bool autoConvert, DVASPECT aspect, int index)
            {
                SecurityHelper.DemandAllClipboardPermission();

                bool baseVar;

                baseVar = GetDataPresentInner(format, aspect, index);

                if (!baseVar && autoConvert)
                {
                    string[] mappedFormats;

                    mappedFormats = GetMappedFormats(format);
                    if (mappedFormats != null)
                    {
                        for (int i = 0; i < mappedFormats.Length; i++)
                        {
                            if (!IsFormatEqual(format, mappedFormats[i]))
                            {
                                baseVar = GetDataPresentInner(mappedFormats[i], aspect, index);
                                if (baseVar)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
                return baseVar;
            }
Beispiel #13
0
            private Object GetData(string format, bool autoConvert, DVASPECT aspect, int index)
            {
                SecurityHelper.DemandAllClipboardPermission();

                Object baseVar;
                Object original;

                baseVar = GetDataFromBoundOleDataObject(format, aspect, index);
                original = baseVar;

                if (autoConvert && (baseVar == null || baseVar is MemoryStream))
                {
                    string[] mappedFormats;

                    mappedFormats = GetMappedFormats(format);
                    if (mappedFormats != null)
                    {
                        for (int i = 0; i < mappedFormats.Length; i++)
                        {
                            if (!IsFormatEqual(format, mappedFormats[i]))
                            {
                                baseVar = GetDataFromBoundOleDataObject(mappedFormats[i], aspect, index);

                                if (baseVar != null && !(baseVar is MemoryStream))
                                {
                                    if (IsDataSystemBitmapSource(baseVar) || SystemDrawingHelper.IsBitmap(baseVar))
                                    {
                                        // Ensure Bitmap(BitmapSource or System.Drawing.Bitmap) data which
                                        // match with the requested format.
                                        baseVar = EnsureBitmapDataFromFormat(format, autoConvert, baseVar);
                                    }

                                    original = null;
                                    break;
                                }
                            }
                        }
                    }
                }

                if (original != null)
                {
                    return original;
                }
                else
                {
                    return baseVar;
                }
            }
Beispiel #14
0
 private static extern int OleDraw(IntPtr pUnk, DVASPECT dwAspect, IntPtr hdcDraw, ref Rectangle lprcBounds);
 public static FORMATETC GetFormatEtc(FormatId id, int lindex = -1,
                                      DVASPECT dwAspect       = DVASPECT.DVASPECT_CONTENT)
 {
     return(GetFormatEtc((short)id.Id, lindex, dwAspect));
 }
Beispiel #16
0
            private void SetData(string format, Object data, bool autoConvert, DVASPECT aspect, int index)
            {
                DataStoreEntry dse;
                DataStoreEntry[] datalist;

                dse = new DataStoreEntry(data, autoConvert, aspect, index);
                datalist = (DataStoreEntry[])this._data[format];

                if (datalist == null)
                {
                    datalist = (DataStoreEntry[])Array.CreateInstance(typeof(DataStoreEntry), 1);
                }
                else
                {
                    DataStoreEntry[] newlist;

                    newlist = (DataStoreEntry[])Array.CreateInstance(typeof(DataStoreEntry), datalist.Length + 1);
                    datalist.CopyTo(newlist, 1);
                    datalist = newlist;
                }

                datalist[0] = dse;
                this._data[format] = datalist;
            }
Beispiel #17
0
            private DataStoreEntry FindDataStoreEntry(string format, DVASPECT aspect, int index)
            {
                DataStoreEntry[] dataStoreEntries;
                DataStoreEntry dataStoreEntry;
                DataStoreEntry naturalDataStoreEntry;

                dataStoreEntries = (DataStoreEntry[])_data[format];
                dataStoreEntry = null;
                naturalDataStoreEntry = null;

                // Find the entry with the given aspect and index
                if (dataStoreEntries != null)
                {
                    for (int i = 0; i < dataStoreEntries.Length; i++)
                    {
                        DataStoreEntry entry;

                        entry = dataStoreEntries[i];
                        if (entry.Aspect == aspect)
                        {
                            if (index == -1 || entry.Index == index)
                            {
                                dataStoreEntry = entry;
                                break;
                            }
                        }
                        if (entry.Aspect == DVASPECT.DVASPECT_CONTENT && entry.Index == 0)
                        {
                            naturalDataStoreEntry = entry;
                        }
                    }
                }

                // If we couldn't find a specific entry, we'll use
                // aspect == Content and index == 0.
                if (dataStoreEntry == null && naturalDataStoreEntry != null)
                {
                    dataStoreEntry = naturalDataStoreEntry;
                }

                return dataStoreEntry;
            }
Beispiel #18
0
            private Object GetDataFromOleIStream(string format, DVASPECT aspect, int index)
            {
                FORMATETC formatetc;
                STGMEDIUM medium;

                formatetc = new FORMATETC();

                formatetc.cfFormat = (short)DataFormats.GetDataFormat(format).Id;
                formatetc.dwAspect = aspect;
                formatetc.lindex = index;
                formatetc.tymed = TYMED.TYMED_ISTREAM;

                object outData = null;

                if (NativeMethods.S_OK == QueryGetDataInner(ref formatetc))
                {
                    GetDataInner(ref formatetc, out medium);
                    try
                    {
                        // Check both handle and type of storage medium
                        if (medium.unionmember != IntPtr.Zero && medium.tymed == TYMED.TYMED_ISTREAM)
                        {
                            UnsafeNativeMethods.IStream pStream;

                            pStream = (UnsafeNativeMethods.IStream)Marshal.GetObjectForIUnknown(medium.unionmember);

                            NativeMethods.STATSTG sstg = new NativeMethods.STATSTG();
                            pStream.Stat(sstg, NativeMethods.STATFLAG_DEFAULT);
                            int size = (int)sstg.cbSize;

                            IntPtr hglobal = Win32GlobalAlloc(NativeMethods.GMEM_MOVEABLE
                                                               | NativeMethods.GMEM_DDESHARE
                                                               | NativeMethods.GMEM_ZEROINIT,
                                                              (IntPtr)(size));
                            try
                            {
                                IntPtr ptr = Win32GlobalLock(new HandleRef(this, hglobal));

                                try
                                {
                                    // NTRAID:WINDOWS#1449332-2006/01/16-WAYNEZEN,
                                    // Seek to the beginning of the stream before reading it.
                                    pStream.Seek(0, 0 /* STREAM_SEEK_SET */);
                                    pStream.Read(ptr, size);
                                }
                                finally
                                {
                                    Win32GlobalUnlock(new HandleRef(this, hglobal));
                                }
                                outData = GetDataFromHGLOBAL(format, hglobal);
                            }
                            finally
                            {
                                Win32GlobalFree(new HandleRef(this, hglobal));
                            }
                        }
                    }
                    finally
                    {
                        UnsafeNativeMethods.ReleaseStgMedium(ref medium);
                    }
                }

                return outData;
            }
 public static extern int OleDraw(IntPtr pUnk, DVASPECT dwAspect, IntPtr hdcDraw, ref Rectangle lprcBounds);
Beispiel #20
0
            private object GetDataFromOleHGLOBAL(string format, DVASPECT aspect, int index)
            {
                FORMATETC formatetc;
                STGMEDIUM medium;
                Object data;

                formatetc = new FORMATETC();

                formatetc.cfFormat = (short)DataFormats.GetDataFormat(format).Id;
                formatetc.dwAspect = aspect;
                formatetc.lindex = index;
                formatetc.tymed = TYMED.TYMED_HGLOBAL;

                data = null;

                if (NativeMethods.S_OK == QueryGetDataInner(ref formatetc))
                {
                    GetDataInner(ref formatetc, out medium);
                    try
                    {

                        // Check both handle and type of storage medium
                        if (medium.unionmember != IntPtr.Zero && medium.tymed == TYMED.TYMED_HGLOBAL)
                        {
                            data = GetDataFromHGLOBAL(format, medium.unionmember);
                        }
                    }
                    finally
                    {
                        UnsafeNativeMethods.ReleaseStgMedium(ref medium);
                    }
                }

                return data;
            }
Beispiel #21
0
 unsafe HRESULT IOleObject.GetExtent(DVASPECT dwDrawAspect, Size *pSizel)
 {
     return(HRESULT.S_OK);
 }
Beispiel #22
0
            private Object GetDataFromOleOther(string format, DVASPECT aspect, int index)
            {
                FORMATETC formatetc;
                STGMEDIUM medium;
                TYMED tymed;
                Object data;

                formatetc = new FORMATETC();

                tymed = (TYMED)0;

                if (IsFormatEqual(format, DataFormats.Bitmap))
                {
                    tymed = TYMED.TYMED_GDI;
                }
                else if (IsFormatEqual(format, DataFormats.EnhancedMetafile))
                {
                    tymed = TYMED.TYMED_ENHMF;
                }

                if (tymed == (TYMED)0)
                {
                    return null;
                }

                formatetc.cfFormat = (short)DataFormats.GetDataFormat(format).Id;
                formatetc.dwAspect = aspect;
                formatetc.lindex = index;
                formatetc.tymed = tymed;

                data = null;

                if (NativeMethods.S_OK == QueryGetDataInner(ref formatetc))
                {
                    GetDataInner(ref formatetc, out medium);
                    try
                    {

                        if (medium.unionmember != IntPtr.Zero)
                        {
                            if (IsFormatEqual(format, DataFormats.Bitmap))
                            //||IsFormatEqual(format, DataFormats.Dib)
                            {
                                // Get the bitmap from the handle of bitmap.
                                data = GetBitmapSourceFromHbitmap(medium.unionmember);
                            }
                            else if (IsFormatEqual(format, DataFormats.EnhancedMetafile))
                            {
                                // Get the metafile object form the enhanced metafile handle.
                                data = SystemDrawingHelper.GetMetafileFromHemf(medium.unionmember);
                            }
                        }
                    }
                    finally
                    {
                        UnsafeNativeMethods.ReleaseStgMedium(ref medium);
                    }
                }

                return data;
            }