Skip to content

Commit

Permalink
Improved algorithm for determining blobs in the system clipboard.
Browse files Browse the repository at this point in the history
Added the property 'ClipboardObject' that gets/sets any blob copied to the system clipboard.
Updated README.
  • Loading branch information
Willy-Kimura committed Mar 16, 2019
1 parent 0946cef commit e5a5561
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
1 change: 1 addition & 0 deletions Assets/nuget-package-3.0.0-brightgreen.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SharpClipboard
[![sc-nuget](/Assets/nuget-package-2.0.7-brightgreen.svg)](https://www.nuget.org/packages/SharpClipboard/) [![sc-donate](/Assets/Donate-PayPal-blue.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=DJ8D9CE8BWA3J&source=url)
[![sc-nuget](/Assets/nuget-package-3.0.0-brightgreen.svg)](https://www.nuget.org/packages/SharpClipboard/) [![sc-donate](/Assets/Donate-PayPal-blue.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=DJ8D9CE8BWA3J&source=url)

**SharpClipboard** is a clipboard-monitoring library for .NET that listens to the system's clipboard entries,
allowing developers to tap into the rich capabilities of determining the clipboard's contents at runtime.
Expand All @@ -12,7 +12,7 @@ Here's a screenshot and below a usage-preview of the library's features:
# Installation
To install via the NuGet Package Manager Console, type:

> `Install-Package SharpClipboard -Version 2.0.7`
> `Install-Package SharpClipboard -Version 3.0.0`
You can also [download](https://github.com/Willy-Kimura/SharpClipboard/releases/download/v2.0.7/SharpClipboard.dll) the assembly and add it to Visual Studio's Toolbox; plus not forgetting its [documentation](https://github.com/Willy-Kimura/SharpClipboard/releases/download/v2.0.7/SharpClipboard.xml).

Expand Down Expand Up @@ -80,7 +80,7 @@ To use it in code, first import `WK.Libraries.SharpClipboardNS` - the code below
// If the cut/copied content is complex, use 'Other'.
else if (e.ContentType == SharpClipboard.ContentTypes.Other)
{
// Do something with 'e.Content' here...
// Do something with 'clipboard.ClipboardObject' or 'e.Content' here...
}
}
```
Expand Down
13 changes: 11 additions & 2 deletions SharpCliboard.Tests/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections.Generic;
Expand Down Expand Up @@ -84,7 +83,17 @@ private void sharpClipboard1_ClipboardChanged(object sender, SharpClipboard.Clip
}
else if (e.ContentType == SharpClipboard.ContentTypes.Other)
{
// Do something with 'e.Content' here...
// Do something with 'e.Content' or alternatively
// 'sharpClipboard1.ClipboardObject' property here...

// A great example here is when a user has copied an Outlook Mail item.
// Such an item will be of a complex object-type format which can be parsed and
// examined by using the 'Microsoft.Office.Interop.Outlook' namespace features.
// See here: https://stackoverflow.com/questions/25375367/how-to-copy-mailitem-in-outlook-c-sharp

// You can however still use the 'ClipboardText' property if you
// prefer simply displaying the copied object in text format.
txtCopiedTexts.Text = sharpClipboard1.ClipboardText.ToString();
}

// Add a TextBox, uncomment the lines below and run.
Expand Down
4 changes: 2 additions & 2 deletions SharpClipboard/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.7.0")]
[assembly: AssemblyFileVersion("2.0.7.0")]
[assembly: AssemblyVersion("3.0.0.0")]
[assembly: AssemblyFileVersion("3.0.0.0")]
7 changes: 7 additions & 0 deletions SharpClipboard/SharpClipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ public ObservableDataFormats ObservableFormats
[Browsable(false)]
public string ClipboardText { get; internal set; }

/// <summary>
/// Gets the currently cut/copied clipboard <see cref="object"/>.
/// This is necessary when handling complex content copied to the clipboard.
/// </summary>
[Browsable(false)]
public object ClipboardObject { get; internal set; }

/// <summary>
/// Gets the currently cut/copied clipboard file-path.
/// </summary>
Expand Down
28 changes: 22 additions & 6 deletions SharpClipboard/Views/ClipboardHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,28 @@ protected override void WndProc(ref Message m)
{
string[] capturedFiles = (string[])dataObj.GetData(DataFormats.FileDrop);

SharpClipboardInstance.ClipboardFiles.AddRange(capturedFiles);
SharpClipboardInstance.ClipboardFile = capturedFiles[0];

SharpClipboardInstance.Invoke(capturedFiles, SharpClipboard.ContentTypes.Files,
new SourceApplication(GetForegroundWindow(), SharpClipboardInstance.ForegroundWindowHandle(),
GetApplicationName(), GetActiveWindowTitle(), GetApplicationPath()));
// If the 'capturedFiles' string array persists as null, then it means
// that the copied content is of a complex object-type since the file-drop
// format is able to capture more-than-just-file content in the clipboard.
// Assign the content its rightful content-type.
if (capturedFiles == null)
{
SharpClipboardInstance.ClipboardObject = dataObj;
SharpClipboardInstance.ClipboardText = dataObj.GetData(DataFormats.UnicodeText).ToString();

SharpClipboardInstance.Invoke(dataObj, SharpClipboard.ContentTypes.Other,
new SourceApplication(GetForegroundWindow(), SharpClipboardInstance.ForegroundWindowHandle(),
GetApplicationName(), GetActiveWindowTitle(), GetApplicationPath()));
}
else
{
SharpClipboardInstance.ClipboardFiles.AddRange(capturedFiles);
SharpClipboardInstance.ClipboardFile = capturedFiles[0];

SharpClipboardInstance.Invoke(capturedFiles, SharpClipboard.ContentTypes.Files,
new SourceApplication(GetForegroundWindow(), SharpClipboardInstance.ForegroundWindowHandle(),
GetApplicationName(), GetActiveWindowTitle(), GetApplicationPath()));
}
}

// Determines whether text has been cut/copied.
Expand Down

0 comments on commit e5a5561

Please sign in to comment.