コード例 #1
0
        public ClipboardThumbnailItem(BitmapSource icon, BitmapSource preview, string title, ClipboardSnapshot snapshot)
        {
            ShowActivated = false;
            Focusable = false;
            Icon = icon;
            WindowStyle = WindowStyle.None;
            ResizeMode = ResizeMode.NoResize;
            Title = title;
            Width = 0;
            Height = 0;
            Owner = GlobalMessageListener.Instance;
            Top = -short.MaxValue;
            Left = -short.MaxValue;

            var bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = new Uri("pack://application:,,,/Shapeshifter;component/Images/Overlay.png");
            bitmap.EndInit();

            TaskbarItemInfo = new TaskbarItemInfo()
            {
                Overlay = bitmap
            };

            _snapshot = snapshot;

            _preview = BitmapFromBitmapSource(preview);

            _taskbarList = (ITaskbarList4)new TaskbarList();
            _taskbarList.HrInit();
        }
コード例 #2
0
        protected override ClipboardItem CreateNew(ClipboardSnapshot snapshot)
#endif
        {

#if !NETFX_CORE
            if (snapshot.HasFormat(KnownClipboardFormats.CF_UNICODETEXT)) //general text data
#else
            if(snapshot.HasFormat(StandardDataFormats.Text))
#endif
            {

#if !NETFX_CORE
                IntPtr textPointer = snapshot.FetchDataPointer(KnownClipboardFormats.CF_UNICODETEXT);

                try
                {

                    string value = Marshal.PtrToStringUni(textPointer);
#else
                var value = (string)snapshot.FetchData(StandardDataFormats.Text);
#endif

                    var result = new ClipboardText();
                    result.Text = value;

                    return result;

#if !NETFX_CORE

                }
                finally
                {

                    if (textPointer != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(textPointer);
                    }

                }

#endif

            }
            else
            {
                throw new InvalidOperationException("The text format was not known");
            }
        }
コード例 #3
0
        protected override ClipboardItem CreateNew(ClipboardSnapshot snapshot)
#endif
        {
            var preferredFormat = snapshot.FormatPriorities.First();

            var result = new ClipboardCustomData();

#if !NETFX_CORE
            result.Name = snapshot.GetClipboardFormatName(preferredFormat);
            result.Icon = ClipboardSource.IconFromWindowHandle(GetClipboardOwner(), 64);
#else
            result.Name = preferredFormat;
            //TODO: shapeshifter icon or something else which symbolizes custom data
#endif

            return result;
           
        }
コード例 #4
0
        public async Task<ClipboardItem> CreateNewClipboardItem()
#endif
        {
            if (_snapshot == null)
            {
#if !NETFX_CORE
                _snapshot = ClipboardSnapshot.CreateSnapshot(clipboardOwner);
#else
                _snapshot = ClipboardSnapshot.CreateSnapshot();
#endif
            }

#if !NETFX_CORE
            ClipboardItem result = CreateNew(_snapshot);
#else
            ClipboardItem result = await CreateNew(_snapshot);
#endif

            var comparableClipboardItem = result as IClipboardDataComparable;
            if(comparableClipboardItem != null)
            {
                foreach(var possibleDuplicate in possibleDuplicates)
                {
                    if(comparableClipboardItem.IsIdentical(possibleDuplicate))
                    {
                        result = possibleDuplicate;
                        break;
                    }
                }
            }

            result.Snapshot = _snapshot;

#if !NETFX_CORE
            result.Source = ClipboardSource.SourceFromWindowHandle(_snapshot.OutsideClipboardOwnerHandle);
#endif

            return result;
        }
コード例 #5
0
 protected abstract ClipboardItem CreateNew(ClipboardSnapshot snapshot);
コード例 #6
0
 protected abstract Task<ClipboardItem> CreateNew(ClipboardSnapshot snapshot);
コード例 #7
0
 protected ClipboardItemFactory(ClipboardSnapshot snapshot)
 {
     _snapshot = snapshot;
 }
コード例 #8
0
 protected async override Task<ClipboardItem> CreateNew(ClipboardSnapshot snapshot)
コード例 #9
0
 public ClipboardTextFactory(ClipboardSnapshot snapshot = null)
     : base(snapshot)
 {
 }
コード例 #10
0
 public ClipboardCustomDataFactory(ClipboardSnapshot snapshot = null) : base(snapshot)
 {
 }
コード例 #11
0
        protected override ClipboardItem CreateNew(ClipboardSnapshot snapshot)
#endif
        {

            try
            {

                if (snapshot.HasFormat(KnownClipboardFormats.CF_DIBV5))
                //device independent bitmap (with transparency - yay!)
                {
                    IntPtr pointer = IntPtr.Zero;

                    try
                    {

                        pointer = snapshot.FetchDataPointer(KnownClipboardFormats.CF_DIBV5);

                        var infoHeader =
                            (BITMAPV5HEADER)Marshal.PtrToStructure(pointer, typeof(BITMAPV5HEADER));

                        using (
                            var bitmap = new Bitmap((int)infoHeader.bV5Width, (int)infoHeader.bV5Height,
                                                    (int)(infoHeader.bV5SizeImage / infoHeader.bV5Height),
                                                    PixelFormat.Format32bppArgb,
                                                    new IntPtr(pointer.ToInt64() + infoHeader.bV5Size)))
                        {

                            var bitmapSource = new RenderTargetBitmap((int)infoHeader.bV5Width,
                                                                      (int)infoHeader.bV5Height,
                                                                      96, 96, PixelFormats.Pbgra32);
                            var visual = new DrawingVisual();
                            var drawingContext = visual.RenderOpen();

                            drawingContext.DrawImage(CreateBitmapSourceFromBitmap(bitmap),
                                                     new Rect(0, 0, (int)infoHeader.bV5Width,
                                                              (int)infoHeader.bV5Height));

                            drawingContext.Close();

                            bitmapSource.Render(visual);

                            var result = new ClipboardImage();
                            result.Image = bitmapSource;

                            return result;

                        }

                    }
                    finally
                    {
                        if (pointer != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(pointer);
                        }
                    }

                }
            }
            catch (ArgumentException)
            {
                //this dibv5 format was faulty. let's roll back to normal dib.
            }

            //TODO: do we need the below or don't we? will the above work for all formats?
            if (snapshot.HasFormat(KnownClipboardFormats.CF_DIB)) //device independent bitmap
            {
                byte[] buffer = snapshot.FetchData(KnownClipboardFormats.CF_DIB);

                var infoHeader =
                    ApiHelper.ByteArrayToStructure<BITMAPINFOHEADER>(buffer);

                int fileHeaderSize = Marshal.SizeOf(typeof(BITMAPFILEHEADER));
                int infoHeaderSize = infoHeader.biSize;
                int fileSize = fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage;

                var fileHeader = new BITMAPFILEHEADER();
                fileHeader.bfType = BITMAPFILEHEADER.BM;
                fileHeader.bfSize = fileSize;
                fileHeader.bfReserved1 = 0;
                fileHeader.bfReserved2 = 0;
                fileHeader.bfOffBits = fileHeaderSize + infoHeaderSize + infoHeader.biClrUsed * 4;

                byte[] fileHeaderBytes =
                    ApiHelper.StructureToByteArray(fileHeader);

                var bitmapStream = new MemoryStream();
                bitmapStream.Write(fileHeaderBytes, 0, fileHeaderSize);
                bitmapStream.Write(buffer, 0, buffer.Length);
                bitmapStream.Seek(0, SeekOrigin.Begin);

                BitmapFrame bitmap = BitmapFrame.Create(bitmapStream);

                var result = new ClipboardImage();
                result.Image = bitmap;

                return result;
            }

            if(snapshot.HasFormat(KnownClipboardFormats.CF_ENHMETAFILE))
            {
                byte[] buffer = snapshot.FetchData(KnownClipboardFormats.CF_ENHMETAFILE);
                using (var memoryStream = new MemoryStream(buffer))
                {
                    using(var metafile = new Metafile(memoryStream))
                    {
                        using (var bitmap = new Bitmap(metafile.Width, metafile.Height))
                        {
                            using (var graphics = Graphics.FromImage(bitmap))
                            {
                                graphics.DrawImage(metafile, 0, 0);

                                var result = new ClipboardImage();
                                result.Image = CreateBitmapSourceFromBitmap(bitmap);

                                return result;
                            }
                        }
                    }
                }
            }

            var formatResult = string.Empty;
            foreach (var format in snapshot.FormatPriorities)
            {
                formatResult += format + ", ";
            }
            if (!string.IsNullOrEmpty(formatResult))
            {
                formatResult = formatResult.Substring(0, formatResult.Length - 2);
            }

            throw new InvalidOperationException("None of the image formats from " + ClipboardSource.ApplicationNameFromWindowHandle(snapshot.OutsideClipboardOwnerHandle) + " were known (" + formatResult + ")");

        }
コード例 #12
0
 public ClipboardImageFactory(ClipboardSnapshot snapshot = null)
     : base(snapshot)
 {
 }
コード例 #13
0
        protected override ClipboardItem CreateNew(ClipboardSnapshot snapshot)
#endif
        {

            //does it have one or multiple files?
#if !NETFX_CORE
            if (snapshot.HasFormat(KnownClipboardFormats.CF_HDROP))
#else
            if(snapshot.HasFormat(StandardDataFormats.StorageItems))
#endif
            {
#if !NETFX_CORE

                IntPtr pointer = snapshot.FetchDataPointer(KnownClipboardFormats.CF_HDROP);
                try
                {

                    var paths = new List<string>();

                    //get amount of files. see http://msdn.microsoft.com/en-us/library/windows/desktop/bb776408(v=vs.85).aspx
                    int fileCount = DragQueryFile(pointer, -1, null, 0);

#else

                var data = (IEnumerable<IStorageItem>) snapshot.FetchData(StandardDataFormats.StorageItems);
                var paths = data.ToList();
                int fileCount = paths.Count();
 
#endif

                    if (fileCount > 0)
                    {
#if !NETFX_CORE
                        for (int i = 0; i < fileCount; i++)
                        {
                            int fileNameLength = DragQueryFile(pointer, i, null, 0) + 1;

                            var fileNameBuilder = new StringBuilder(fileNameLength);
                            DragQueryFile(pointer, i, fileNameBuilder, fileNameBuilder.Capacity);

                            string fileName = fileNameBuilder.ToString();
                            paths.Add(fileName);
                        }
#endif

                        if (fileCount > 1)
                        {
                            var result = new ClipboardFileCollection();
                            result.Paths = paths;

                            return result;
                        }
                        else
                        {
                            var result = new ClipboardFile();
                            result.Path = paths.First();

                            result.Icon = IconHelper.GetIcon(result.Path, true, 64);

                            return result;
                        }
                    }

                    return null;

#if !NETFX_CORE

                }
                finally
                {
                    if (pointer != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(pointer);
                    }
                }
#endif

            }
            else
            {
                throw new InvalidOperationException("Can't create file collection clipboard item when there is no recognizable file data present");
            }
        }