Skip to content

Commit

Permalink
Added SourceApplication property to ClipboardChanged event arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-Kimura committed Feb 6, 2019
1 parent 445e116 commit ea1e58c
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 30 deletions.
Binary file added Assets/sharpclipboard-preview-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SharpClipboard
**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.

Here's a screenshot of the library's features:

![sc-preview](/Assets/sharpclipboard-preview-01.png)
27 changes: 21 additions & 6 deletions SharpCliboard.Tests/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion SharpCliboard.Tests/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void chkObserveFiles_CheckedChanged(object sender, EventArgs e)

private void sharpClipboard1_ClipboardChanged(object sender, SharpClipboard.ClipboardChangedEventArgs e)
{
if (e.ContentType == WK.Libraries.SharpClipboardNS.SharpClipboard.ContentTypes.Text)
if (e.ContentType == SharpClipboard.ContentTypes.Text)
{
txtCopiedTexts.Text = sharpClipboard1.ClipboardText;

Expand Down Expand Up @@ -79,6 +79,9 @@ private void sharpClipboard1_ClipboardChanged(object sender, SharpClipboard.Clip
// ---------------------------
// lstCopiedFiles.Items.AddRange(((List<string>)e.Content).ToArray()));
}

textBox1.Text = $"Name: {e.SourceApplication.Name}, Title: {e.SourceApplication.Title}, " +
$"ID: {e.SourceApplication.ID}, Path: {e.SourceApplication.Path}";
}
}
}
135 changes: 117 additions & 18 deletions SharpClipboard/SharpClipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.ComponentModel;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Runtime.InteropServices;

using WK.Libraries.SharpClipboardNS.Views;

Expand Down Expand Up @@ -57,12 +58,12 @@ public SharpClipboard(IContainer container)
#endregion

#region Fields

private bool _monitorClipboard;

private string _clipboardText;
private string _clipboardFile;
private List<string> _clipboardFiles = new List<string>();
private List<string> _clipboardFiles = new List<string>();

private Image _clipboardImage;
private Timer _timer = new Timer();
Expand Down Expand Up @@ -191,7 +192,7 @@ public Image ClipboardImage
get { return _clipboardImage; }
internal set { _clipboardImage = value; }
}

#endregion

#endregion
Expand All @@ -209,18 +210,29 @@ private void SetDefaults()
_timer.Tick += OnLoad;
}

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();

#endregion

#region Public

/// <summary>
/// Gets the current foreground window's handle.
/// </summary>
/// <returns></returns>
public IntPtr ForegroundWindowHandle()
{
return GetForegroundWindow();
}

/// <summary>
/// Starts the clipboard-monitoring process and
/// initializes the system clipboard-access handle.
/// </summary>
public void StartMonitoring()
{
if (MonitorClipboard)
_handle.Show();
_handle.Show();
}

/// <summary>
Expand All @@ -229,18 +241,17 @@ public void StartMonitoring()
/// </summary>
public void StopMonitoring()
{
if (_handle.Visible)
_handle.Close();
_handle.Close();
}

/// <summary>
/// Invokes the <see cref="ClipboardChanged"/> event with formal parameters.
/// </summary>
/// <param name="content">The current clipboard content.</param>
/// <param name="type">The current clipboard content-type.</param>
internal void Invoke(object content, ContentTypes type)
internal void Invoke(object content, ContentTypes type, SourceApplication source)
{
ClipboardChanged?.Invoke(this, new ClipboardChangedEventArgs(content, type));
ClipboardChanged?.Invoke(this, new ClipboardChangedEventArgs(content, type, source));
}

#endregion
Expand Down Expand Up @@ -292,16 +303,25 @@ public class ClipboardChangedEventArgs : EventArgs
/// </summary>
/// <param name="content">The current clipboard content.</param>
/// <param name="contentType">The current clipboard-content-type.</param>
public ClipboardChangedEventArgs(object content, ContentTypes contentType)
public ClipboardChangedEventArgs(object content, ContentTypes contentType, SourceApplication source)
{
Content = content;
ContentType = contentType;

_source = new SourceApplication(source.ID, source.Handle, source.Name,
source.Title, source.Path);
}

#endregion


#region Fields

private SourceApplication _source;

#endregion

#region Properties

/// <summary>
/// Gets the currently copied clipboard content.
/// </summary>
Expand All @@ -311,7 +331,17 @@ public ClipboardChangedEventArgs(object content, ContentTypes contentType)
/// Gets the currently copied clipboard content-type.
/// </summary>
public ContentTypes ContentType { get; }


/// <summary>
/// Gets the application from where the
/// clipboard's content were copied.
/// </summary>
public SourceApplication SourceApplication
{
get { return _source; }

}

#endregion
}

Expand Down Expand Up @@ -348,7 +378,7 @@ private void OnLoad(object sender, EventArgs e)
public class WKDesigner : ComponentDesigner
{
private DesignerActionListCollection actionLists;

// Use pull model to populate smart tag menu.
public override DesignerActionListCollection ActionLists
{
Expand Down Expand Up @@ -384,7 +414,7 @@ public WKComponentActionList(IComponent component) : base(component)

// Cache a reference to DesignerActionUIService so
// that the DesignerActionList can be refreshed.
this.designerActionUISvc = GetService(typeof(DesignerActionUIService))
this.designerActionUISvc = GetService(typeof(DesignerActionUIService))
as DesignerActionUIService;

// Automatically display Smart Tags for quick access
Expand Down Expand Up @@ -451,7 +481,7 @@ public override DesignerActionItemCollection GetSortedActionItems()
new DesignerActionPropertyItem("MonitorClipboard",
"Monitor Clipboard", "Behaviour",
GetPropertyDescriptor(this.Component, "MonitorClipboard").Description)

};

return items;
Expand All @@ -475,7 +505,7 @@ public bool MonitorClipboard
#endregion
}

#region Observable Formats
#region Property Classes

/// <summary>
/// Provides a list of supported observable data formats
Expand All @@ -488,7 +518,7 @@ public bool MonitorClipboard
public class ObservableDataFormats
{
/// <summary>
/// Creates a new <see cref="ObservableDataFormats"/> options class.
/// Creates a new <see cref="ObservableDataFormats"/> options class-instance.
/// </summary>
public ObservableDataFormats()
{
Expand Down Expand Up @@ -561,6 +591,75 @@ public override string ToString()

#endregion
}

/// <summary>
/// Stores details of the application from
/// where the clipboard's content were copied.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public class SourceApplication
{
/// <summary>
/// Creates a new <see cref="SourceApplication"/> class-instance.
/// </summary>
/// <param name="id">The application's ID.</param>
/// <param name="handle">The application's handle.</param>
/// <param name="name">The application's name.</param>
/// <param name="title">The application's title.</param>
/// <param name="path">The application's path.</param>
internal SourceApplication(int id, IntPtr handle, string name,
string title, string path)
{
ID = id;
Name = name;
Path = path;
Title = title;
Handle = handle;
}

#region Properties

/// <summary>
/// Gets the application's process-ID.
/// </summary>
public int ID { get; }

/// <summary>
/// Gets the appliation's window-handle.
/// </summary>
public IntPtr Handle { get; }

/// <summary>
/// Gets the application's name.
/// </summary>
public string Name { get; }

/// <summary>
/// Gets the application's title-text.
/// </summary>
public string Title { get; }

/// <summary>
/// Gets the application's absolute path.
/// </summary>
public string Path { get; }

#endregion

#region Overrides

/// <summary>
/// Returns a <see cref="string"/> containing the list
/// of application details provided.
/// </summary>
public override string ToString()
{
return $"ID: {ID}; Handle: {Handle}, Name: {Name}; " +
$"Title: {Title}; Path: {Path}";
}

#endregion
}

#endregion
}
Loading

0 comments on commit ea1e58c

Please sign in to comment.