GetFormat() public static method

public static GetFormat ( int id ) : System.Text.Format
id int
return System.Text.Format
示例#1
0
 private void PasteHandler(object sender, Forms.KeyEventArgs e)
 {
     if (e.Control && e.KeyCode == Keys.V)
     {
         richTextEditorBox.Paste(DataFormats.GetFormat(DataFormats.Text));
         e.Handled = true;
     }
 }
            public virtual string[] GetFormats(bool autoConvert)
            {
                Debug.Assert(innerData != null, "You must have an innerData on all DataObjects");

                IEnumFORMATETC enumFORMATETC = null;

                // Since we are only adding elements to the HashSet, the order will be preserved.
                HashSet <string> distinctFormats = new HashSet <string>();

                try
                {
                    enumFORMATETC = innerData.EnumFormatEtc(DATADIR.DATADIR_GET);
                }
                catch
                {
                }

                if (enumFORMATETC != null)
                {
                    enumFORMATETC.Reset();

                    FORMATETC[] formatetc = new FORMATETC[] { new FORMATETC() };
                    int[]       retrieved = new int[] { 1 };

                    while (retrieved[0] > 0)
                    {
                        retrieved[0] = 0;
                        try
                        {
                            enumFORMATETC.Next(1, formatetc, retrieved);
                        }
                        catch
                        {
                        }

                        if (retrieved[0] > 0)
                        {
                            string name = DataFormats.GetFormat(formatetc[0].cfFormat).Name;
                            if (autoConvert)
                            {
                                string[] mappedFormats = GetMappedFormats(name);
                                for (int i = 0; i < mappedFormats.Length; i++)
                                {
                                    distinctFormats.Add(mappedFormats[i]);
                                }
                            }
                            else
                            {
                                distinctFormats.Add(name);
                            }
                        }
                    }
                }

                return(distinctFormats.ToArray());
            }
示例#3
0
            public virtual string[] GetFormats(bool autoConvert)
            {
                Debug.Assert(innerData != null, "You must have an innerData on all DataObjects");

                IEnumFORMATETC enumFORMATETC = null;
                ArrayList      formats       = new ArrayList();

                try
                {
                    enumFORMATETC = innerData.EnumFormatEtc(DATADIR.DATADIR_GET);
                }
                catch
                {
                }

                if (enumFORMATETC != null)
                {
                    enumFORMATETC.Reset();

                    FORMATETC[] formatetc = new FORMATETC[] { new FORMATETC() };
                    int[]       retrieved = new int[] { 1 };

                    while (retrieved[0] > 0)
                    {
                        retrieved[0] = 0;
                        try
                        {
                            enumFORMATETC.Next(1, formatetc, retrieved);
                        }
                        catch
                        {
                        }

                        if (retrieved[0] > 0)
                        {
                            string name = DataFormats.GetFormat(formatetc[0].cfFormat).Name;
                            if (autoConvert)
                            {
                                string[] mappedFormats = GetMappedFormats(name);
                                for (int i = 0; i < mappedFormats.Length; i++)
                                {
                                    formats.Add(mappedFormats[i]);
                                }
                            }
                            else
                            {
                                formats.Add(name);
                            }
                        }
                    }
                }

                string[] temp = new string[formats.Count];
                formats.CopyTo(temp, 0);
                return(GetDistinctStrings(temp));
            }
            public FormatEnumerator(IDataObject parent, string[] formats)
            {
                Debug.WriteLineIf(CompModSwitches.DataObject.TraceVerbose, "FormatEnumerator: Constructed: " + parent.ToString() + ", string[]" + formats.Length.ToString(CultureInfo.InvariantCulture));

                this.parent = parent;
                this.formats.Clear();

                if (formats != null)
                {
                    if (parent is DataObject dataObject && dataObject.RestrictedFormats)
                    {
                        if (!Clipboard.IsFormatValid(formats))
                        {
                            throw new Security.SecurityException(SR.ClipboardSecurityException);
                        }
                    }

                    for (int i = 0; i < formats.Length; i++)
                    {
                        string    format = formats[i];
                        FORMATETC temp   = new FORMATETC
                        {
                            cfFormat = unchecked ((short)(ushort)(DataFormats.GetFormat(format).Id)),
                            dwAspect = DVASPECT.DVASPECT_CONTENT,
                            ptd      = IntPtr.Zero,
                            lindex   = -1
                        };

                        if (format.Equals(DataFormats.Bitmap))
                        {
                            temp.tymed = TYMED.TYMED_GDI;
                        }
                        else if (format.Equals(DataFormats.Text) ||
                                 format.Equals(DataFormats.UnicodeText) ||
                                 format.Equals(DataFormats.StringFormat) ||
                                 format.Equals(DataFormats.Rtf) ||
                                 format.Equals(DataFormats.CommaSeparatedValue) ||
                                 format.Equals(DataFormats.FileDrop) ||
                                 format.Equals(CF_DEPRECATED_FILENAME) ||
                                 format.Equals(CF_DEPRECATED_FILENAMEW))
                        {
                            temp.tymed = TYMED.TYMED_HGLOBAL;
                        }
                        else
                        {
                            temp.tymed = TYMED.TYMED_HGLOBAL;
                        }

                        if (temp.tymed != 0)
                        {
                            this.formats.Add(temp);
                        }
                    }
                }
            }
        static XplatUI()
        {
            // Compose name with current domain id because on Win32 we register class name
            // and name must be unique to process. If we load MWF into multiple appdomains
            // and try to register same class name we fail.
            default_class_name = "SWFClass" + System.Threading.Thread.GetDomainID().ToString();

            if (RunningOnUnix)
            {
                //if (Environment.GetEnvironmentVariable ("not_supported_MONO_MWF_USE_NEW_X11_BACKEND") != null) {
                //        driver=XplatUIX11_new.GetInstance ();
                //} else
                if (Environment.GetEnvironmentVariable("MONO_MWF_MAC_FORCE_X11") != null)
                {
                    driver = XplatUIX11.GetInstance();
                }
                else
                {
                    IntPtr buf = Marshal.AllocHGlobal(8192);
                    // This is a hacktastic way of getting sysname from uname ()
                    if (uname(buf) != 0)
                    {
                        // WTF: We cannot run uname
                        driver = XplatUIX11.GetInstance();
                    }
                    else
                    {
                        string os = Marshal.PtrToStringAnsi(buf);
                        if (os == "Darwin")
                        {
                            driver = XplatUICarbon.GetInstance();
                        }
                        else
                        {
                            driver = XplatUIX11.GetInstance();
                        }
                    }
                    Marshal.FreeHGlobal(buf);
                }
            }
            else
            {
                driver = XplatUIWin32.GetInstance();
            }

            driver.InitializeDriver();

            // Initialize things that need to be done after the driver is ready
            DataFormats.GetFormat(0);

            // Signal that the Application loop can be run.
            // This allows UIA to initialize a11y support for MWF
            // before the main loop begins.
            Application.FirePreRun();
        }
示例#6
0
        internal static void SetDataObjectImpl(object data, bool copy)
        {
            IntPtr clipboard_handle;

            XplatUI.ObjectToClipboard converter;
            int native_format;

            DataFormats.Format item_format;

            converter = new XplatUI.ObjectToClipboard(ConvertToClipboardData);

            clipboard_handle = XplatUI.ClipboardOpen(false);
            XplatUI.ClipboardStore(clipboard_handle, null, 0, null);    // Empty clipboard

            native_format = -1;

            if (data is IDataObject)
            {
                string[] formats;

                IDataObject data_object = data as IDataObject;
                formats = data_object.GetFormats();
                for (int i = 0; i < formats.Length; i++)
                {
                    item_format = DataFormats.GetFormat(formats[i]);
                    if ((item_format != null) && (item_format.Name != DataFormats.StringFormat))
                    {
                        native_format = item_format.Id;
                    }

                    object obj = data_object.GetData(formats [i]);

                    // this is used only by custom formats
                    if (IsDataSerializable(obj))
                    {
                        item_format.is_serializable = true;
                    }

                    XplatUI.ClipboardStore(clipboard_handle, obj, native_format, converter);
                }
            }
            else
            {
                item_format = DataFormats.Format.Find(data.GetType().FullName);
                if ((item_format != null) && (item_format.Name != DataFormats.StringFormat))
                {
                    native_format = item_format.Id;
                }

                XplatUI.ClipboardStore(clipboard_handle, data, native_format, converter);
            }
            XplatUI.ClipboardClose(clipboard_handle);
        }
示例#7
0
        /// <summary>
        /// Sets managed data to a clipboard DataObject.
        /// </summary>
        /// <param name="dataObject">The DataObject to set the data on.</param>
        /// <param name="format">The clipboard format.</param>
        /// <param name="data">The data object.</param>
        /// <remarks>
        /// Because the underlying data store is not storing managed objects, but
        /// unmanaged ones, this function provides intelligent conversion, allowing
        /// you to set unmanaged data into the COM implemented IDataObject.</remarks>
        public static void SetDataEx(this IDataObject dataObject, string format, object data)
        {
            DataFormats.Format dataFormat = DataFormats.GetFormat(format);

            // Initialize the format structure
            FORMATETC formatETC = new FORMATETC();

            formatETC.cfFormat = (short)dataFormat.Id;
            formatETC.dwAspect = DVASPECT.DVASPECT_CONTENT;
            formatETC.lindex   = -1;
            formatETC.ptd      = IntPtr.Zero;

            // Try to discover the TYMED from the format and data
            TYMED tymed = GetCompatibleTymed(format, data);

            // If a TYMED was found, we can use the system DataObject
            // to convert our value for us.
            if (tymed != TYMED.TYMED_NULL)
            {
                formatETC.tymed = tymed;

                // Set data on an empty DataObject instance
                DataObject conv = new DataObject();
                conv.SetData(format, true, data);

                // Now retrieve the data, using the COM interface.
                // This will perform a managed to unmanaged conversion for us.
                STGMEDIUM medium;
                ((ComIDataObject)conv).GetData(ref formatETC, out medium);
                try
                {
                    // Now set the data on our data object
                    ((ComIDataObject)dataObject).SetData(ref formatETC, ref medium, true);
                }
                catch
                {
                    // On exceptions, release the medium
                    ReleaseStgMedium(ref medium);
                    throw;
                }
            }
            else
            {
                // Since we couldn't determine a TYMED, this data
                // is likely custom managed data, and won't be used
                // by unmanaged code, so we'll use our custom marshaling
                // implemented by our COM IDataObject extensions.

                ComDataObjectExtensions.SetManagedData((ComIDataObject)dataObject, format, data);
            }
        }
示例#8
0
        public void AddSource(int type, object source)
        {
            // Try to detect plain text, based on the old behaviour of XplatUIX11, which usually assigns
            // -1 as the type when a string is stored in the Clipboard
            if (source is string && (type == DataFormats.GetFormat(DataFormats.Text).Id || type == -1))
            {
                plain_text_source = source as string;
            }
            else if (source is Image)
            {
                image_source = source as Image;
            }

            source_data [type] = source;
        }
 internal static bool IsFormatValid(FORMATETC[] formats)
 {
     if ((formats == null) || (formats.Length > 4))
     {
         return(false);
     }
     for (int i = 0; i < formats.Length; i++)
     {
         short cfFormat = formats[i].cfFormat;
         if (((cfFormat != 1) && (cfFormat != 13)) && ((cfFormat != DataFormats.GetFormat("System.String").Id) && (cfFormat != DataFormats.GetFormat("Csv").Id)))
         {
             return(false);
         }
     }
     return(true);
 }
示例#10
0
        internal static IDataObject GetDataObject(bool primary_selection)
        {
            DataObject clipboard;
            IntPtr     clipboard_handle;

            int[] native_formats;
            DataFormats.Format item_format;
            object             managed_clipboard_item;

            XplatUI.ClipboardToObject converter;

            converter = new XplatUI.ClipboardToObject(ConvertFromClipboardData);

            clipboard_handle = XplatUI.ClipboardOpen(primary_selection);
            native_formats   = XplatUI.ClipboardAvailableFormats(clipboard_handle);
            if (native_formats == null)
            {
                return(null);   // Clipboard empty
            }

            // Build the IDataObject
            clipboard = new DataObject();
            for (int i = 0; i < native_formats.Length; i++)
            {
                // We might get a format we don't understand or know
                item_format = DataFormats.GetFormat(native_formats[i]);

                if (item_format != null)
                {
                    managed_clipboard_item = XplatUI.ClipboardRetrieve(clipboard_handle, native_formats[i], converter);

                    if (managed_clipboard_item != null)
                    {
                        clipboard.SetData(item_format.Name, managed_clipboard_item);
                        // We don't handle 'bitmap' since it involves handles, so we'll equate it to dib
                        if (item_format.Name == DataFormats.Dib)
                        {
                            clipboard.SetData(DataFormats.Bitmap, managed_clipboard_item);
                        }
                    }
                }
            }

            XplatUI.ClipboardClose(clipboard_handle);

            return(clipboard);
        }
示例#11
0
            private unsafe object GetDataFromOleIStream(string format)
            {
                FORMATETC formatetc = new FORMATETC();
                STGMEDIUM medium    = new STGMEDIUM();

                formatetc.cfFormat = unchecked ((short)(ushort)(DataFormats.GetFormat(format).Id));
                formatetc.dwAspect = DVASPECT.DVASPECT_CONTENT;
                formatetc.lindex   = -1;
                formatetc.tymed    = TYMED.TYMED_ISTREAM;

                medium.tymed = TYMED.TYMED_ISTREAM;

                // Limit the # of exceptions we may throw below.
                if ((int)HRESULT.S_OK != QueryGetDataUnsafe(ref formatetc))
                {
                    return(null);
                }

                try
                {
                    innerData.GetData(ref formatetc, out medium);
                }
                catch
                {
                    return(null);
                }

                if (medium.unionmember != IntPtr.Zero)
                {
                    Ole32.IStream pStream = (Ole32.IStream)Marshal.GetObjectForIUnknown(medium.unionmember);
                    Marshal.Release(medium.unionmember);
                    pStream.Stat(out Ole32.STATSTG sstg, Ole32.STATFLAG.STATFLAG_DEFAULT);

                    IntPtr hglobal = Kernel32.GlobalAlloc(
                        Kernel32.GMEM.MOVEABLE | Kernel32.GMEM.DDESHARE | Kernel32.GMEM.ZEROINIT,
                        (uint)sstg.cbSize);
                    IntPtr ptr = Kernel32.GlobalLock(hglobal);
                    pStream.Read((byte *)ptr, (uint)sstg.cbSize, null);
                    Kernel32.GlobalUnlock(hglobal);

                    return(GetDataFromHGLOBAL(format, hglobal));
                }

                return(null);
            }
示例#12
0
        static XplatUI()
        {
#if XAMARINMAC
            driver = XplatUICocoa.GetInstance();
#else
            driver = XplatUIWin32.GetInstance();
#endif

            driver.InitializeDriver();

            // Initialize things that need to be done after the driver is ready
            DataFormats.GetFormat(0);

            // Signal that the Application loop can be run.
            // This allows UIA to initialize a11y support for MWF
            // before the main loop begins.
            Application.FirePreRun();
        }
            public FormatEnumerator(IDataObject parent, string[] formats)
            {
                Debug.WriteLineIf(CompModSwitches.DataObject.TraceVerbose, $"FormatEnumerator: Constructed: {parent}, string[{(formats?.Length ?? 0)}]");

                _parent = parent;

                if (formats != null)
                {
                    for (int i = 0; i < formats.Length; i++)
                    {
                        string    format = formats[i];
                        FORMATETC temp   = new FORMATETC
                        {
                            cfFormat = unchecked ((short)(ushort)(DataFormats.GetFormat(format).Id)),
                            dwAspect = DVASPECT.DVASPECT_CONTENT,
                            ptd      = IntPtr.Zero,
                            lindex   = -1
                        };

                        if (format.Equals(DataFormats.Bitmap))
                        {
                            temp.tymed = TYMED.TYMED_GDI;
                        }
                        else if (format.Equals(DataFormats.Text) ||
                                 format.Equals(DataFormats.UnicodeText) ||
                                 format.Equals(DataFormats.StringFormat) ||
                                 format.Equals(DataFormats.Rtf) ||
                                 format.Equals(DataFormats.CommaSeparatedValue) ||
                                 format.Equals(DataFormats.FileDrop) ||
                                 format.Equals(CF_DEPRECATED_FILENAME) ||
                                 format.Equals(CF_DEPRECATED_FILENAMEW))
                        {
                            temp.tymed = TYMED.TYMED_HGLOBAL;
                        }
                        else
                        {
                            temp.tymed = TYMED.TYMED_HGLOBAL;
                        }

                        _formats.Add(temp);
                    }
                }
            }
示例#14
0
            private bool GetDataPresentInner(string format)
            {
                Debug.Assert(_innerData is not null, "You must have an innerData on all DataObjects");
                FORMATETC formatetc = new FORMATETC
                {
                    cfFormat = unchecked ((short)(ushort)(DataFormats.GetFormat(format).Id)),
                    dwAspect = DVASPECT.DVASPECT_CONTENT,
                    lindex   = -1
                };

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

                int hr = QueryGetDataUnsafe(ref formatetc);

                return(hr == (int)HRESULT.S_OK);
            }
示例#15
0
        private void GetDataIntoOleStructs(ref FORMATETC formatetc,
                                           ref STGMEDIUM medium)
        {
            if (GetTymedUseable(formatetc.tymed) && GetTymedUseable(medium.tymed))
            {
                string format = DataFormats.GetFormat(formatetc.cfFormat).Name;

                if (GetDataPresent(format))
                {
                    object data = GetData(format);

                    if ((formatetc.tymed & TYMED.TYMED_HGLOBAL) != 0)
                    {
                        HRESULT hr = SaveDataToHandle(data, format, ref medium);
                        if (hr.Failed())
                        {
                            Marshal.ThrowExceptionForHR((int)hr);
                        }
                    }
                    else if ((formatetc.tymed & TYMED.TYMED_GDI) != 0)
                    {
                        if (format.Equals(DataFormats.Bitmap) && data is Bitmap bm &&
                            bm != null)
                        {
                            // save bitmap
                            medium.unionmember = (IntPtr)GetCompatibleBitmap(bm);
                        }
                    }
                    else
                    {
                        Marshal.ThrowExceptionForHR((int)HRESULT.DV_E_TYMED);
                    }
                }
                else
                {
                    Marshal.ThrowExceptionForHR((int)HRESULT.DV_E_FORMATETC);
                }
            }
            else
            {
                Marshal.ThrowExceptionForHR((int)HRESULT.DV_E_TYMED);
            }
        }
示例#16
0
            /// <summary>
            ///  Uses HGLOBALs and retrieves the specified format from the bound IComDataObject.
            /// </summary>
            private object?GetDataFromOleHGLOBAL(string format, out bool done)
            {
                done = false;
                Debug.Assert(_innerData is not null, "You must have an innerData on all DataObjects");

                FORMATETC formatetc = new FORMATETC();
                STGMEDIUM medium    = new STGMEDIUM();

                formatetc.cfFormat = unchecked ((short)(ushort)(DataFormats.GetFormat(format).Id));
                formatetc.dwAspect = DVASPECT.DVASPECT_CONTENT;
                formatetc.lindex   = -1;
                formatetc.tymed    = TYMED.TYMED_HGLOBAL;

                object?data = null;

                if ((int)HRESULT.S_OK == QueryGetDataUnsafe(ref formatetc))
                {
                    try
                    {
                        _innerData.GetData(ref formatetc, out medium);

                        if (medium.tymed == TYMED.TYMED_HGLOBAL && medium.unionmember != IntPtr.Zero)
                        {
                            data = GetDataFromHGLOBAL(format, medium.unionmember);
                        }
                    }
                    catch (RestrictedTypeDeserializationException)
                    {
                        done = true;
                    }
                    catch
                    {
                    }
                    finally
                    {
                        Ole32.ReleaseStgMedium(ref medium);
                    }
                }

                return(data);
            }
示例#17
0
        internal static void SetDataObjectImpl(object data, bool copy)
        {
            var converter = new XplatUI.ObjectToClipboard(ConvertToClipboardData);

            var clipboard_handle = XplatUI.ClipboardOpen(false);

            XplatUI.ClipboardStore(clipboard_handle, null, 0, null, copy);              // Empty clipboard

            if (data is IDataObject idata)
            {
                XplatUI.ClipboardStore(clipboard_handle, idata, DataFormats.GetFormat(IDataObjectFormat).Id, converter, copy);
            }
            else
            {
                var format = DataFormats.Format.Find(data.GetType().FullName);
                var id     = (format != null) && (format.Name != DataFormats.StringFormat) ? format.Id : -1;
                XplatUI.ClipboardStore(clipboard_handle, data, id, converter, copy);
            }

            XplatUI.ClipboardClose(clipboard_handle);
        }
示例#18
0
        internal static bool IsFormatValid(FORMATETC[] formats)
        {
            Debug.Assert(formats != null, "Null returned from GetFormats");
            if (formats.Length <= 4)
            {
                for (int i = 0; i < formats.Length; i++)
                {
                    short format = formats[i].cfFormat;
                    if (format != NativeMethods.CF_TEXT &&
                        format != NativeMethods.CF_UNICODETEXT &&
                        format != DataFormats.GetFormat("System.String").Id&&
                        format != DataFormats.GetFormat("Csv").Id)
                    {
                        return(false);
                    }
                }
                return(true);
            }

            return(false);
        }
示例#19
0
        //RichTextBox 插入图片
        public void ShowInsertImageDlg()
        {
            OpenFileDialog OpenFileDialog1 = new OpenFileDialog();

            OpenFileDialog1.Title       = "Select Image";
            OpenFileDialog1.Filter      = "BMP File|*.BMP|JPEG File|*.JPG|GIF File|*.GIF|PNG File|*.PNG|ICO File|*.ICO|Image File|*.BMP;*.DIB;*.RLE;*.JPG;*.JPEG;*.JPE;*.JFIF;*.GIF;*.EMF;*.WMF;*.TIF;*.PNG;*.ICO|All File|*.*";
            OpenFileDialog1.FilterIndex = 6;
            if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string strImagePath = OpenFileDialog1.FileName;
                Image  img;
                img = Image.FromFile(strImagePath);
                Clipboard.SetDataObject(img);
                DataFormats.Format df;
                df = DataFormats.GetFormat(DataFormats.Bitmap);
                if (this.CanPaste(df))
                {
                    this.Paste(df);
                }
            }
        }
示例#20
0
        internal static IDataObject GetDataObject(bool primary_selection)
        {
            IDataObject data = null;

            var clipboard_handle = XplatUI.ClipboardOpen(primary_selection);

            var formats = XplatUI.ClipboardAvailableFormats(clipboard_handle);

            if (formats != null)
            {
                var format    = DataFormats.GetFormat(IDataObjectFormat).Id;
                var converter = new XplatUI.ClipboardToObject(ConvertFromClipboardData);
                if (XplatUI.ClipboardRetrieve(clipboard_handle, format, converter) is IDataObject idata)
                {
                    data = idata;
                }
            }

            XplatUI.ClipboardClose(clipboard_handle);

            return(data ?? new DataObject());
        }
示例#21
0
        internal IStream GetStream(System.Runtime.InteropServices.ComTypes.IDataObject cdata, int index)
        {
            var format = new FORMATETC
            {
                cfFormat = (short)DataFormats.GetFormat(CFSTR_FILECONTENTS).Id,
                dwAspect = DVASPECT.DVASPECT_CONTENT,
                tymed    = TYMED.TYMED_ISTREAM,
                lindex   = index
            };

            cdata.GetData(ref format, out STGMEDIUM medium);

            if (medium.tymed == format.tymed)
            {
                if (Marshal.GetObjectForIUnknown(medium.unionmember) is IStream stream)
                {
                    return(stream);
                }
            }

            return(null);
        }
示例#22
0
        int IComDataObject.QueryGetData(ref FORMATETC formatetc)
        {
            Debug.WriteLineIf(CompModSwitches.DataObject.TraceVerbose, "QueryGetData");
            if (innerData is OleConverter)
            {
                return(((OleConverter)innerData).OleDataObject.QueryGetData(ref formatetc));
            }

            if (formatetc.dwAspect == DVASPECT.DVASPECT_CONTENT)
            {
                if (GetTymedUseable(formatetc.tymed))
                {
                    if (formatetc.cfFormat == 0)
                    {
                        Debug.WriteLineIf(CompModSwitches.DataObject.TraceVerbose, "QueryGetData::returning S_FALSE because cfFormat == 0");
                        return((int)HRESULT.S_FALSE);
                    }
                    else if (!GetDataPresent(DataFormats.GetFormat(formatetc.cfFormat).Name))
                    {
                        return((int)HRESULT.DV_E_FORMATETC);
                    }
                }
                else
                {
                    return((int)HRESULT.DV_E_TYMED);
                }
            }
            else
            {
                return((int)HRESULT.DV_E_DVASPECT);
            }
#if DEBUG
            int format = unchecked ((ushort)formatetc.cfFormat);
            Debug.WriteLineIf(CompModSwitches.DataObject.TraceVerbose, "QueryGetData::cfFormat " + format.ToString(CultureInfo.InvariantCulture) + " found");
#endif
            return((int)HRESULT.S_OK);
        }
示例#23
0
            /// <summary>
            ///  Uses IStream and retrieves the specified format from the bound IComDataObject.
            /// </summary>
            private unsafe object?GetDataFromOleIStream(string format)
            {
                FORMATETC formatetc = new FORMATETC();
                STGMEDIUM medium    = new STGMEDIUM();

                formatetc.cfFormat = unchecked ((short)(ushort)(DataFormats.GetFormat(format).Id));
                formatetc.dwAspect = DVASPECT.DVASPECT_CONTENT;
                formatetc.lindex   = -1;
                formatetc.tymed    = TYMED.TYMED_ISTREAM;

                // Limit the # of exceptions we may throw below.
                if ((int)HRESULT.S_OK != QueryGetDataUnsafe(ref formatetc))
                {
                    return(null);
                }

                try
                {
                    _innerData.GetData(ref formatetc, out medium);
                }
                catch
                {
                    return(null);
                }

                Ole32.IStream?pStream = null;
                IntPtr        hglobal = IntPtr.Zero;

                try
                {
                    if (medium.tymed == TYMED.TYMED_ISTREAM && medium.unionmember != IntPtr.Zero)
                    {
                        pStream = (Ole32.IStream)Marshal.GetObjectForIUnknown(medium.unionmember);
                        pStream.Stat(out Ole32.STATSTG sstg, Ole32.STATFLAG.DEFAULT);

                        hglobal = Kernel32.GlobalAlloc(
                            Kernel32.GMEM.MOVEABLE | Kernel32.GMEM.DDESHARE | Kernel32.GMEM.ZEROINIT,
                            (uint)sstg.cbSize);
                        // not throwing here because the other out of memory condition on GlobalAlloc
                        // happens inside innerData.GetData and gets turned into a null return value
                        if (hglobal == IntPtr.Zero)
                        {
                            return(null);
                        }
                        IntPtr ptr = Kernel32.GlobalLock(hglobal);
                        pStream.Read((byte *)ptr, (uint)sstg.cbSize, null);
                        Kernel32.GlobalUnlock(hglobal);

                        return(GetDataFromHGLOBAL(format, hglobal));
                    }

                    return(null);
                }
                finally
                {
                    if (hglobal != IntPtr.Zero)
                    {
                        Kernel32.GlobalFree(hglobal);
                    }

                    if (pStream is not null)
                    {
                        Marshal.ReleaseComObject(pStream);
                    }

                    Ole32.ReleaseStgMedium(ref medium);
                }
            }
示例#24
0
            /// <summary>
            ///  Retrieves the specified format data from the bound IComDataObject, from
            ///  other sources that IStream and HGLOBAL... this is really just a place
            ///  to put the "special" formats like BITMAP, ENHMF, etc.
            /// </summary>
            private object?GetDataFromOleOther(string format)
            {
                Debug.Assert(_innerData is not null, "You must have an innerData on all DataObjects");

                FORMATETC formatetc = new FORMATETC();
                STGMEDIUM medium    = new STGMEDIUM();

                TYMED tymed = (TYMED)0;

                if (format.Equals(DataFormats.Bitmap))
                {
                    tymed = TYMED.TYMED_GDI;
                }

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

                formatetc.cfFormat = unchecked ((short)(ushort)(DataFormats.GetFormat(format).Id));
                formatetc.dwAspect = DVASPECT.DVASPECT_CONTENT;
                formatetc.lindex   = -1;
                formatetc.tymed    = tymed;

                object?data = null;

                if ((int)HRESULT.S_OK == QueryGetDataUnsafe(ref formatetc))
                {
                    try
                    {
                        _innerData.GetData(ref formatetc, out medium);
                    }
                    catch
                    {
                    }
                }

                try
                {
                    if (medium.tymed == TYMED.TYMED_GDI && medium.unionmember != IntPtr.Zero)
                    {
                        if (format.Equals(DataFormats.Bitmap))
                        {
                            // ASURT 140870 -- GDI+ doesn't own this HBITMAP, but we can't
                            // delete it while the object is still around.  So we have to do the really expensive
                            // thing of cloning the image so we can release the HBITMAP.

                            // This bitmap is created by the com object which originally copied the bitmap to the
                            // clipboard. We call Add here, since DeleteObject calls Remove.
                            Image clipboardImage = Image.FromHbitmap(medium.unionmember);
                            if (clipboardImage is not null)
                            {
                                Image firstImage = clipboardImage;
                                clipboardImage = (Image)clipboardImage.Clone();
                                firstImage.Dispose();
                            }

                            data = clipboardImage;
                        }
                    }
                }
                finally
                {
                    Ole32.ReleaseStgMedium(ref medium);
                }

                return(data);
            }
示例#25
0
        public static void Main()
        {
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.Bitmap));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.CommaSeparatedValue));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.Dib));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.Dif));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.EnhancedMetafile));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.FileDrop));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.Html));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.Locale));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.MetafilePict));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.OemText));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.Palette));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.PenData));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.Riff));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.Rtf));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.Serializable));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.StringFormat));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.SymbolicLink));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.Text));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.Tiff));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.UnicodeText));
            PrintFormatInfo(DataFormats.GetFormat(DataFormats.WaveAudio));

            // Add our own format
            PrintFormatInfo(DataFormats.GetFormat("Birthday"));

            // Test some basic stuff
            DataObject dobj;
            Control    c;
            string     rtf;

            c   = new Control();
            rtf = "\\r\\t\\f  string";

            // Load the data object
            dobj = new DataObject(DataFormats.Text, "I am text");
            dobj.SetData(c.GetType(), c);
            dobj.SetData(DataFormats.Rtf, rtf);

            PrintFormats("GetFormats(): ", dobj.GetFormats());                                                           // Count should be 5
            PrintFormats("GetFormats(true): ", dobj.GetFormats(true));                                                   // Count should be 5
            PrintFormats("GetFormats(false): ", dobj.GetFormats(false));                                                 // Count should be 3

            Console.WriteLine("GetDataPresent(typeof(string)): {0}", dobj.GetDataPresent(typeof(string)));               // We expect true
            Console.WriteLine("GetDataPresent(DataFormats.Text): {0}", dobj.GetDataPresent(DataFormats.Text));           // We expect true
            Console.WriteLine("GetDataPresent(DataFormats.WaveAudio): {0}", dobj.GetDataPresent(DataFormats.WaveAudio)); // We expect false

            Console.WriteLine("GetData(DataFormats.Rtf): {0}", dobj.GetData(DataFormats.Rtf));                           // We expect "\r\t\f  string"

            clipboard clip = new clipboard();

            PrintClipboardContents();

            IDataObject data;

            data = Clipboard.GetDataObject();

            if (data != null && data.GetDataPresent(DataFormats.Bitmap))
            {
                image = (Image)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);
            }

            Bitmap i = new Bitmap("test.bmp");
            string s = "bummerä";

            Clipboard.SetDataObject(s);

            Application.Run(clip);
        }