IClipboardDataControlPackage ConstructPackage(IEnumerable<uint> formats)
        {
            var package = new ClipboardDataControlPackage();
            DecoratePackageWithClipboardData(formats, package);
            userInterfaceThread.Invoke(() => DecoratePackageWithControl(package));

            return package;
        }
 void DecoratePackageWithClipboardData(
     IEnumerable<uint> formats,
     ClipboardDataControlPackage package)
 {
     foreach (var format in formats)
     {
         var dataFactory = dataFactories.FirstOrDefault(x => x.CanBuildData(format));
         if (dataFactory != null)
         {
             DecoratePackageWithFormatDataUsingFactory(package, dataFactory, format);
         }
     }
 }
 void DecoratePackageWithFormatDataUsingFactory(
     ClipboardDataControlPackage package, IClipboardDataControlFactory factory, uint format)
 {
     var unwrapper = memoryUnwrappers.FirstOrDefault(x => x.CanUnwrap(format));
     if (unwrapper != null)
     {
         var rawData = unwrapper.UnwrapStructure(format);
         if (rawData != null)
         {
             var clipboardData = factory.BuildData(format, rawData);
             if (clipboardData != null)
             {
                 package.AddData(clipboardData);
             }
         }
     }
 }
        private void ClipboardHook_DataCopied(object sender, Events.DataCopiedEventArgument e)
        {
            var dataObject = e.Data;

            var package = new ClipboardDataControlPackage();
            foreach (var factory in dataFactories)
            {
                foreach (var format in dataObject.GetFormats(true))
                {
                    if (factory.CanBuildData(format))
                    {
                        var rawData = dataObject.GetData(format);

                        var clipboardData = factory.BuildData(format, rawData);
                        package.AddData(clipboardData);
                    }
                }
            }

            //construct a control if the package contains any data.
            foreach (var factory in dataFactories)
            {
                foreach (var data in package.Contents)
                {
                    if (factory.CanBuildControl(data))
                    {
                        package.Control = factory.BuildControl(data);
                        break;
                    }
                }
            }

            if(package.Control == null)
            {
                throw new NotImplementedException("Can't handle unknown data formats yet.");
            }

            //signal an added event.
            if(ControlAdded != null)
            {
                ControlAdded(this, new ControlEventArgument(package.Control));
            }
        }
 void DecoratePackageWithControl(ClipboardDataControlPackage package)
 {
     foreach (var data in package.Contents)
     {
         var dataFactory = dataFactories.FirstOrDefault(x => x.CanBuildControl(data));
         if (dataFactory != null)
         {
             package.Control = dataFactory.BuildControl(data);
             break;
         }
     }
 }