Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Enhanced Clipboard Manager in .NET using C# and Windows Forms

0.00/5 (No votes)
26 Apr 2007 1  
A better Clipboard Manager

Introduction

For most Programmers, copy and paste is a daily task.

But Windows' Clipboard makes our work of copying and pasting harder.

It won't support queuing, the saving of clipboard data, showing the time a copy occurred, the application name from where you copied data, access to remote machine's Clipboard, etc. I think it's better to design an application that will make the copying and pasting of data a little bit easier. So, I designed this application in VS.NET 2003 using C# and Windows Forms. I will explain features provided by this application followed by its design and coding.

Features present in this application

  • It allows us to queue all clipboard data.
  • It allows us to save entire clipboard data into permanent storage like file.
  • Shows the time at which you copied data into clipboard.
  • Shows an Icon of the application from where you copied data onto the clipboard.
  • It allows us to access the Clipboard of any remote machine.
  • Easy to use UI.
  • Now, Clipboard data is just a click away from us.

Now, create a new Windows Application using C# in VS.NET 2003 and name it ClipBoardWatcher. Add controls to the Main Form (Form1) as shown below:

Screenshot - image001.jpg

Here, I set the following properties of Form1:

  • Location -> 850, 0
  • Opacity -> 50%
  • Size -> 176, 738 and
  • KeyPreview -> True.

And I placed a Tooltip and NotifyIcon followed by two contextmenus. One contextmenu is assigned to Form1 (to the button displayed on the form for each clipboard item) and another to NotifyIcon.

The following are the menuitems present in the cxtmnuclip context menu (assigned to Form1's clipboard item):

  • Copy To ClipBoard (this is used to copy selected clipboard items from an application onto the ClipBoard).
  • Remove This ClipBoard Data (Removes selected clipboard item from application).
  • Remove All ClipBoard items (Remove all clipboard items from application).
  • Save Items to File (Saves contents of application (ClipBoard items) onto a file for further reference).

Following are the menuitems present in cxttaskbar context menu (assigned to NotifyIcon):

All menuitems present in this context menu are related to the UI settings of the Application:

  • Show/Hide to show or hide the application.
  • Show In Taskbar allows us to set whether the application should be shown in the taskbar or not.
  • Show on Top allows us to set whether the application should be shown on top of all windows or not.
  • Control Form Thickness allows us to increase or decrease thickness (Opacity) of main form.
  • Show ControlBox allows us to set whether application should show controlbox or not.
  • Exit is used to end execution of the application.

The main functionality of this application is it will create a button on the main form whenever you copy new data onto the clipboard. In addition, it will display an icon of application from which you copied data onto clipboard, and the time of copy by monitoring the contents of System's Clipboard for every second by using the System.Threading.Timer object.

Then I added a form named frmnewprcdetails.cs to get the IP address of a remote machine for monitoring its clipboard. Then I added a class named RemoteClipboard in Form1.cs to call the remote method whenever Clipboard data changes. In this class, I am using two methods to send and receive Clipboard data. Whenever the contents of the Clipboard changes, it will send the updated contents to a remote machine. In the remote machine, an application will set the Clipboard to the contents, it is received. Like this, we can update the remote machine's Clipboard with contents of our system easily. In order to access the remote machine's clipboard, we must have administrative (or its equivalent) permission on that machine.

Now I will explain the coding part of this application.

I added three class files to the application. I will outline the functionality of each class file.

Functionality present in ClipBoardItems.cs

In order to create buttons for each clipboard item, I implemented a control array of VB. Here, I just created a class named ClipBoardItems which inherits from System.Collections.CollectionBase. In the constructor of this class, I am passing container (Form1) for this buttons. Than I added five methods: implement add new button (clipboard item), remove selected clipboard item, remove all clipboard items from application, save all items into a file, and handle click of button.

In AddNewClipItem(), I created a new button and set its Text property to contents of clipboard data. I also set properties like width, height and Image to the icon of the application from where data is copied onto the clipboard.

Functionality present in APIFuncs.cs

In this class, I use Windows API methods to get an application's .exe path from where you copied data onto the clipboard. Actually, I get the Handle of the window from where you copied data by using GetForegroundWindow() and then that handle is passed to GetWindowProcessID() to get Process ID. Then, I pas this Process ID to GetProcessById() to get the process from where data is copied. Finally, I get the physical path of the executing process (process is created by calling GetProcessById()) by using the property MainModule of the process.

Functionality present in ActiveApplicationIcon.cs

In this class, I am use GetApplnIcon() to get the icon of the process from where data is copied. The logic behind this is, every one second System.Threading's timer object checks the contents of the system clipboard. If its contents are changed then it will get the icon of the currently focused window (the window from where data is copied) and it is set to a newly created button with text as clipboard contents, and Image as the icon of the application from where data is copied onto the clipboard. We got the icon of the application by passing the physical path of the process's MainModule to GetApplnIcon() of the ActiveApplicationIcon class.

We can use Windows API methods to get data from the clipboard whenever it is changed instead of using a timer object. If I use these Windows API methods, then most of the code in this application will not be C# code (code will have API calls only) and it will be too hard to understand that application. Performance-wise, there won't be much difference in using the timer object or Windows API. Since we are using System.Threading's timer, we won't consume many resources. If you want, you can still go with Windows API.

Finally, I added some code to enhance the UI of the application. The final output will be like this:

<Screenshot - image002.jpg

We can still enhance this application by improving UI.

By using this application, you will never lose your clipboard data. Moreover, it is just a click away from you. I am attaching the source code for further reference. I hope this code will be useful for all.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here