void ch_ClipboardGrabbed(System.Windows.Forms.IDataObject dataObject)
        {
            try
            {
                IntPtr id;
                User32.GetWindowThreadProcessId(activeWindow, out id);
                activeProcess = Process.GetProcessById(id.ToInt32());

                if (ExclusionslistController.Accept(activeWindow))
                {

                    var formats = dataObject.GetFormats();
                    DataEnabledListBoxItem n = null;
                    var ItemsBox = mainWindow.ItemsBox;
                    if (formats.Contains(DataFormats.Text))
                    {
                        var text = Clipboard.GetText();

                        if (Settings.Default.OmitEmptyStrings && String.IsNullOrEmpty(text))
                        {
                            return;
                        }

                        if (Settings.Default.OmitWhitespacesOnlyString && String.IsNullOrEmpty(text.Trim()))
                        {
                            return;
                        }

                        if (CheckDuplicates(ItemsBox, (obj) => (text as string) == (obj as string)))
                            return;

                        n = new TextDataLBI(Clipboard.GetDataObject());
                    }
                    else if (formats.Contains(DataFormats.FileDrop))
                    {
                        var files = dataObject.GetData(DataFormats.FileDrop) as string[];

                        if (CheckDuplicates(ItemsBox, (obj) =>
                        {
                            string[] objasfiles = obj as string[];

                            if (objasfiles != null && objasfiles.Length == files.Length)
                            {

                                // we need this since order may nto be preserved
                                // e.g. when user selects file from l-to-r and
                                // and then r-to-l
                                for (int i = 0; i < objasfiles.Length; i++)
                                {
                                    if (!files.Contains(objasfiles[i]))
                                        return false;
                                }

                                return true;
                            }
                            return false;
                        }))
                            return;

                        n = new FileDropsLBI(files);
                    }
                    else if (System.Windows.Clipboard.ContainsImage())
                    {

                        System.Windows.Forms.IDataObject clipboardData = System.Windows.Forms.Clipboard.GetDataObject();
                        System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)clipboardData.GetData(
                           System.Windows.Forms.DataFormats.Bitmap);

                        if (CheckDuplicates(ItemsBox, (obj) =>
                        {
                            var taghash = obj as ImageHash;
                            if (taghash == null)
                            {
                                return false;
                            }

                            else return ImageHash.Compare(taghash, new ImageHash(bitmap)) == ImageHash.CompareResult.ciCompareOk;
                        }))
                            return;

                        n = new ImageLBI(bitmap);
                    }

                    if (n != null)
                    {
                        ItemsBox.Items.Insert(0, n);
                        n.ItemClicked += new ItemCopiedEventHandler(n_ItemClicked);
                    }

                    /* Console.WriteLine("\r\n\r\n");

                     for (int i = 0; i < ItemsBox.Items.Count; i++)
                     {
                         Console.WriteLine("{0}:{1}", i, (ItemsBox.Items[i] as ListBoxItem).Tag);
                     }*/
                }
            }
            catch (COMException)
            {
                MessageBox.Show("An error occured while performing clipboard operation (read)");
            }
        }
Beispiel #2
0
        void ch_ClipboardGrabbed(System.Windows.Forms.IDataObject dataObject)
        {
            TimeSpan variable =  DateTime.Now - lastClipTime;
            lastClipTime = DateTime.Now;

            if (skipNextPaste || variable.TotalMilliseconds < 500)
            {
                skipNextPaste = false;
                return;
            }

            var formats = dataObject.GetFormats();

            if (formats.Contains(DataFormats.Text))
            {
                var text = Clipboard.GetText();

                if (String.IsNullOrWhiteSpace(text))
                {
                    return;
                }

                var data = Clipboard.GetDataObject();

                ClipHub.Code.Models.ClipboardEntry newClip = new ClipHub.Code.Models.ClipboardEntry();
                newClip.clipboardContents = text;
                newClip.dateClipped = DateTime.Now;

                String title = GetActiveWindowTitle();
                newClip.applicationClippedFrom = title;

                clipRepository.Insert(newClip);
            }
            else if (formats.Contains(DataFormats.FileDrop))
            {
                //var files = dataObject.GetData(DataFormats.FileDrop) as string[];

                //if (CheckDuplicates(ItemsBox, (obj) =>
                //{
                //    string[] objasfiles = obj as string[];

                //    if (objasfiles != null && objasfiles.Length == files.Length)
                //    {

                //        // we need this since order may nto be preserved
                //        // e.g. when user selects file from l-to-r and
                //        // and then r-to-l
                //        for (int i = 0; i < objasfiles.Length; i++)
                //        {
                //            if (!files.Contains(objasfiles[i]))
                //                return false;
                //        }

                //        return true;
                //    }
                //    return false;
                //}))
                //    return;

                //n = new FileDropsLBI(files);
            }
            else if (System.Windows.Clipboard.ContainsImage())
            {

                //System.Windows.Forms.IDataObject clipboardData = System.Windows.Forms.Clipboard.GetDataObject();
                //System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)clipboardData.GetData(
                //   System.Windows.Forms.DataFormats.Bitmap);

                //if (CheckDuplicates(ItemsBox, (obj) =>
                //{
                //    var taghash = obj as ImageHash;
                //    if (taghash == null)
                //    {
                //        return false;
                //    }

                //    else return ImageHash.Compare(taghash, new ImageHash(bitmap)) == ImageHash.CompareResult.ciCompareOk;
                //}))
                //    return;

                //n = new ImageLBI(bitmap);
            }
        }
	/*******************************/
	public static bool IsDataFormatSupported(System.Windows.Forms.IDataObject data, System.Windows.Forms.DataFormats.Format format) 
	{
		bool result = false;
		if ((data != null) && (format != null))
		{
			System.String[] formats = data.GetFormats(true);
			int count = formats.GetLength(0);
			for (int index = 0; index < count; index++) 
			{
				if (formats[index].Equals(format.Name))
				{
					result = true;
					break;
				}
			}
		}
		return result;
	}
Beispiel #4
0
		public static BitmapSource GetBestPossibletAlphaBitmapFromDataObject(System.Windows.IDataObject ob) {
			var formats = ob.GetFormats();
			BitmapSource bmp = null;

			foreach (string f in formats) {
				if (f != "PNG" && f != "image/png") { // only PNG (GIMP) and image/png (haven't seen this in the wild but could be useful)
					continue;
				}
				var it = ob.GetData(f);
				var ms = it as MemoryStream;
				if (ms != null) {
					BitmapImage result = new BitmapImage();
					result.BeginInit();
					// According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."
					// Force the bitmap to load right now so we can dispose the stream.
					result.CacheOption = BitmapCacheOption.OnLoad;
					result.StreamSource = ms;
					result.EndInit();
					result.Freeze();
					bmp = result;
					break;
				}
			}

			if (bmp == null) {
				foreach (string f in formats) {
					if (!f.ToLower().Contains("bitmap")) {
						continue;
					}
					var obj = ob.GetData(f) as BitmapSource;
					if (obj != null) {
						bmp = obj;
						break;
					}
				}
			}

			if (bmp == null) {
				bmp = WindowsClipboard.GetImage();
			}
			bmp = TryFixAlphaChannel(bmp);
			return bmp;
		}