Example #1
0
 public void SetData(FormatId id, object data, int lindex = -1)
 {
     if (!store.TryGetValue(id, out var dic) || dic == null)
     {
         store[id] = new Dictionary <int, object>();
     }
     store[id][lindex] = data;
 }
        public Stream GetStream(FormatId id, int lindex = -1)
        {
            var f = id.FormatEtc;

            f.lindex = lindex;
            DataObject.GetData(ref f, out var s);
            return(s.GetManagedStream());
        }
Example #3
0
 public T GetData <T>(FormatId id, int lindex = -1)
 {
     if (store.TryGetValue(id, out var dict) && dict.TryGetValue(lindex, out var obj))
     {
         return((T)obj);
     }
     throw new KeyNotFoundException();
 }
Example #4
0
        private Stream GetUnmanagedStream(FormatId id, int lindex = -1)
        {
            var f = id.FormatEtc;

            f.lindex = lindex;
            DataObject.GetData(ref f, out var s);
            return(s.GetUnmanagedStream(true));
        }
 public bool GetStream(FormatId id, out Stream result, int lindex = -1)
 {
     if (GetDataPresent(id))
     {
         result = GetStream(id, lindex);
         return(true);
     }
     result = null;
     return(false);
 }
 public bool GetString(FormatId id, NativeStringType native, out string result)
 {
     if (GetDataPresent(id))
     {
         result = GetString(id, native);
         return(true);
     }
     result = null;
     return(false);
 }
        public string GetString(FormatId id, NativeStringType type)
        {
            var       f = DataObjectUtils.GetFormatEtc(id);
            STGMEDIUM s = default;

            try
            {
                DataObject.GetData(ref f, out s);
                return(s.GetString(type));
            }
            finally
            {
                s.Dispose();
            }
        }
        public TResult ReadHGlobal <TResult>(FormatId id) where TResult : unmanaged
        {
            STGMEDIUM s = default;
            var       f = id.FormatEtc;

            try
            {
                DataObject.GetData(ref f, out s);
                return(s.ReadHGlobal <TResult>());
            }
            finally
            {
                s.Dispose();
            }
        }
        public DataObjectFormat(FORMATETC f, int?cannonical = null, bool notDataObject = false)
        {
            FormatId = new FormatId(f.cfFormat);
            if (notDataObject)
            {
                NotDataObject = true;
                return;
            }

            DvAspect  = f.dwAspect;
            PtdNull   = f.ptd;
            LIndex    = f.lindex;
            Tymed     = f.tymed;
            Canonical = cannonical; // man.GetCanonicalFormatEtc(f.cfFormat).cfFormat;
        }
Example #10
0
 public string GetString(FormatId id)
 {
     if (id == FormatId.CF_TEXT || id == FormatId.Rtf || id == FormatId.CF_OEMTEXT ||
         id == FormatId.CommaSeparatedValue)
     {
         return(GetString(id, NativeStringType.Ansi));
     }
     if (id == FormatId.Html || id == FormatId.Xaml)
     {
         return(GetString(id, NativeStringType.Utf8));
     }
     if (id == FormatId.CF_UNICODETEXT || id == FormatId.ApplicationTrust)
     {
         return(GetString(id, NativeStringType.Unicode));
     }
     throw new ArgumentException(nameof(id));
 }
Example #11
0
        public bool TryGetData <T>(FormatId id, out T data, int lindex = -1)
        {
            if (store.TryGetValue(id, out var dict))
            {
                if (dict.TryGetValue(lindex, out var obj))
                {
                    if (obj is T d)
                    {
                        data = d;
                        return(true);
                    }
                }
            }

            data = default;
            return(false);
        }
Example #12
0
        void IComDataObject.GetDataHere(ref FORMATETC format, ref STGMEDIUM medium)
        {
            var id = new FormatId(format.cfFormat);

            if (!TryGetData(id, out object val, format.lindex))
            {
                //Marshal.ThrowExceptionForHR(unchecked((int)0x80040064));
                HRESULT.E_FAIL.ThrowIfFailed();
            }

            var ptr = medium.unionmember;

            if (id == FormatId.CF_HDROP && val is string[] files)
            {
                SaveHdropToHandle(ref ptr, files);
                medium.unionmember = ptr;
                return;
            }

            if (id == FormatId.CF_BITMAP && val is Bitmap bmp)
            {
                medium.unionmember = GetCompatibleBitmap(bmp).DangerousGetHandle();
                return;
            }

            (val switch
            {
                string s when(id == FormatId.CF_TEXT || id == FormatId.Rtf || id == FormatId.CF_OEMTEXT ||
                              id == FormatId.CommaSeparatedValue || id == FormatId.CFSTR_INETURLA)
                => SaveStringToHandle(ref ptr, s, NativeStringType.Ansi),
                string s when id == FormatId.Html || id == FormatId.Xaml
                => SaveStringToHandle(ref ptr, s, NativeStringType.Utf8),
                string s when(id == FormatId.CF_UNICODETEXT || id == FormatId.ApplicationTrust ||
                              id == FormatId.CFSTR_INETURLW)
                => SaveStringToHandle(ref ptr, s, NativeStringType.Unicode),
                Memory <byte> m => SaveSpanToHandle(ref ptr, m.Span),
                byte[] b => SaveSpanToHandle(ref ptr, b.AsSpan()),
                Stream s => SaveStreamToHandle(ref ptr, s),
                _ => throw new ApplicationException()
            }).ThrowIfFailed();
Example #13
0
        public Image GetBitmap(FormatId id)
        {
            if (id == FormatId.CF_BITMAP)
            {
                return(GetBitmap());
            }
            var       f = id.FormatEtc;
            STGMEDIUM s = default;

            try
            {
                DataObject.GetData(ref f, out s);
                using var st  = s.GetUnmanagedStream(true);
                using var bmp = Image.FromStream(st);
                var ret = (Image)bmp.Clone();
                return(ret);
            }
            finally
            {
                s.Dispose();
            }
        }
Example #14
0
 public object GetData(FormatId id)
 {
     if (id == FormatId.CF_TEXT || id == FormatId.Rtf || id == FormatId.CF_OEMTEXT ||
         id == FormatId.CommaSeparatedValue || id == FormatId.Html || id == FormatId.Xaml ||
         id == FormatId.CF_UNICODETEXT || id == FormatId.ApplicationTrust)
     {
         return(GetString(id));
     }
     if (id == FormatId.CF_HDROP)
     {
         return(GetFileDropList());
     }
     if (id == FormatId.CFSTR_FILENAMEW)
     {
         return new[] { GetString(FormatId.CFSTR_FILENAMEW) }
     }
     ;
     if (id == FormatId.CFSTR_FILENAMEA)
     {
         return new[] { GetString(FormatId.CFSTR_FILENAMEA) }
     }
     ;
     if (id == FormatId.CF_BITMAP)
     {
         return(GetBitmap());
     }
     if (id == FormatId.CF_ENHMETAFILE)
     {
         return(GetEnhancedMetafile());
     }
     if (id == FormatId.CF_METAFILEPICT)
     {
         return(GetMetafile());
     }
     return(GetStream(id));
 }
Example #15
0
 public bool GetDataPresent(FormatId id, int lindex = -1)
 {
     return(store.ContainsKey(id) && (store[id]?.ContainsKey(lindex) ?? false));
 }
Example #16
0
 public bool GetDataPresent(FormatId format)
 {
     return(GetDataPresent(format.Id));
 }
Example #17
0
 public T GetData <T>(int lindex = -1)
 {
     return(GetData <T>(FormatId.FromName(typeof(T).FullName), lindex));
 }
Example #18
0
 public object GetData(string format, bool autoConvert)
 {
     return(GetData(FormatId.FromName(format)));
 }
Example #19
0
 public void SetData <T>(T data, int lindex = -1)
 {
     SetData(FormatId.FromName(typeof(T).FullName), data);
 }
Example #20
0
 bool IDataObject.GetDataPresent(string format, bool autoConvert) =>
 GetDataPresent(FormatId.FromName(format));
Example #21
0
 void IDataObject.SetData(string format, bool autoConvert, object data) => SetData(FormatId.FromName(format), data);
Example #22
0
 object IDataObject.GetData(Type format) => GetData <object>(FormatId.FromName(format.FullName));
Example #23
0
 object IDataObject.GetData(string format) => GetData <object>(FormatId.FromName(format));
Example #24
0
 object IDataObject.GetData(string format, bool autoConvert) => GetData <object>(FormatId.FromName("format"));
 public static FORMATETC GetFormatEtc(FormatId id, int lindex = -1,
                                      DVASPECT dwAspect       = DVASPECT.DVASPECT_CONTENT)
 {
     return(GetFormatEtc((short)id.Id, lindex, dwAspect));
 }
Example #26
0
 public virtual FORMATETC GetCanonicalFormatEtc(FormatId format)
 {
     return(GetCanonicalFormatEtc(format.Id));
 }