Clipboard Ring Application

Introduction

The Windows Clipboard is used by many applications, such as word formatters and word processors, as a temporary repository for data. The most obvious examples are Cut, Copy, and Paste. the Clipboard is also useful for transferring data from one application to another because the Clipboard is common across applications (processes). When data is added to the Clipboard, the data format can be specified so that other applications can recognize the format and decide whether to process it.

In .NET, the functionalies for playing with the Windows system Clipboard has been clubbed into the Clipboard class. To add data to Clipboard, you can use the SetDataObject method of the Clipboard class. You can pass any object to this method, but to add data in multiple formats, you must first add the data to a DataObject, which is generic, even though there are specific data objects for specific formats.

Background

This application was basically developed for my personal use. Many times, I would cut a block of code to paste somewhere else and, in the meantime, if I had to look into something else (like attending to someone), by the time I came back I might have forgotton that I was holding something important on the Clipboard and I used the Clipboard space for something else. As a remedial measure, I decided to develop an application that can hold all the entries that make a visit to my Clipboard, so that I can retrieve it back even at some point of time, later in the day.

Using the Code

Retrieving text data from Clipboard

The Clipboard class has a method, GetDataObject(), that retrieves the data currently in the system Clipboard. In fact, GetDataObject() returns an object that implements the IDataObject interface, which has three dominant methods: GetDataPresent, GetData, and GetFormats.

  • GetDataPresent: Determines whether the current data on theClipboard can be converted to the format you specify.
  • GetData: Retrieves the data associated with the format you specify from Windows Clipboard.
Note: The DataFormats class has predefined Clipboard data format names. These can be provided to the above two methods as parameters to verify the data format.
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
{
   string s = Clipboard.GetDataObject().GetData(DataFormats.Text).
              ToString();
}

In the preceding piece of code, the GetData method is used to retrieve data from the system Clipboard. The format of the data to be retrieved is provided as a parameter. Because the system Clipboard is shared by multiple applications, before retrieving data, you must ensure that the format of current Clipboard data is what your appliaction expects. So, you make a check for the data format of the data to be retrieved with a call to the GetDataPresent method; this method returns a boolean value that determines whether the current data on the Clipboard can be converted to the data format you specify. To specify the data format, it is advisable to use the DataFormats class that contains static fields representing system Clipboard formats. DataFormats.Text is provided for string data. An alternative to using the DataFormats class is to provide the name of the format as a string.

Setting text data to the Clipboard

The Clipboard class has a method, SetDataObject(). This method places the specified data on the Clipboard. This method has three interesting overloaded clones:

  1. SetDataObject(object data): Places data on the system Clipboard.
  2. Note: The data placed on the Clipboard by this method is non-persistent. In other words, the data is not retained on Clipboard once the application exits.
  3. SetDataObject(object data, bool copy): Places data on the system Clipboard and specifies whether the data should remain on the Clipboard after the application exits.
  4. SetDataObject(object data, bool copy, int RetryTimes, int RetryDelay): Attempts to place data on the system Clipboard the specified number of times and with the specified delay between attempts.
Clipboard.SetDataObject("C# is the best", true);

In the piece of code above, the SetDataObject method sets the string "C# is the best" on the system Clipboard. The text remains on the Clipboard until it is replaced by any application. And, the string remains there even after your application exits because the second parameter of the method is set to true, which indicates the data will be persistant.

Retrieving image from the Clipboard

This is exacly the same as retrieving text data, except that the data format given is DataFormats.Bitmap instead of DataFormats.Text:

if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Bitmap))
{
   Bitmap bmp = (Bitmap)Clipboard.GetData(DataFormats.Bitmap);
}

The GetDataPresent method verifies whether an image (bitmap) object is present on Clipboard; the GetData method retrieves the image as a bitmap object. An alternative for this in .NET 2.0 is:

if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Bitmap))
{
   Bitmap bmp = (Bitmap)Clipboard.GetImage();
}

The GetImage method returns an Image object that can be directly assigned to Image objects or controls.

Setting an Image to the Clipboard

This is also similar to setting text to the Clipboard.

Clipboard.SetDataObject((Bitmap)lstBoxCpyItms.SelectedItem, true);

The overloaded versions of SetDataObject were explained earlier in this article.

Until now, the discussion has been about the basic usage of the Clipboard class. Now, you will get into the ClipboardRing application.

Clipboard Ring Application

This is the method that retrieves text/image from the system Clipboard and makes an entry into a ListBox, where it resides until the application exits. The current data on the Clipboard is retrieved and added to the ListBox Items collection. By default, only objects with the Text format are retrieved from the Clipboard and added to the ListBox. To store images, right-click the notify icon on the system tray and select the "Process Image" option. Then, the boolean variable processImage is set to true.

Duplicate entries are restricted to the listbox. When a new string entry is made to the listbox items collection, it is stored in a strClipCurrent variable. The next time, it is compared with the current string on the Clipboard and, only if it is different, it is added to the listbox. Also, if the string is already an entry in the listbox, it is not added again. In the case of images also this checking is made, but the logic applied is a bit different, but very similar. I believe the comments in the code make things very clear. The image comparison logic has been taken from markrouse's article. Thanks to markrouse. If I hadn't found this solution, I would have discarded the image storing functionality from the application because all other options I reached to compare images were really crude and not presentable.

The basic logic behind the image comparison is that the hash of two identical objects are equal and different for unidentical objects. The comparison is done by passing an array of bytes and comparing their hash values. This is a really fast method.

private void AddToBoard()
{
   bool addImg = true;

   // Before retrieving Text from the Clipboard make sure the
   // current data on Clipboard is for type text.
   if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
   {
      string s = Clipboard.GetDataObject().
                 GetData(DataFormats.Text).ToString();

      // strClipCurrent has the last string retrieved from the
      // Clipboard.
      // This checking is to avoid the unnecessary looping of the
      // ListBox control unless a new string has come to the Clipboard.
      if (s != strClipCurrent)
      {
         // This checking is to avoid multiple entries in ListBox
         // control.
         if (!lstBoxCpyItms.Items.Contains(s))
         {
            lstBoxCpyItms.Items.Add(s);
            strClipCurrent = s;
         }
      }
   }
   // This option is enabled only when user explicitly enables it
   // This option is to manage images in the Clipboard.
   if (processImage)
   {
      if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Bitmap))
      {
         Bitmap bmp = (Bitmap)Clipboard.GetImage();
         // if bmpClipCurrent is  not null means this is not the
         // first image retrieved from the Clipboard. Therefore,
         // compare with the previous image if they are same; else,
         // add to list.
         if (bmpClipCurrent != null)
         {
            foreach (object obj in lstBoxCpyItms.Items)
            {
               if (obj.GetType().ToString() == "System.Drawing.Bitmap")
               {
                  // Comparing if the 2 bitmaps are the same.
                  // Returns true is identical else false.
                  if (CompareBitmaps(bmp, (Bitmap)obj))
                  {
                     addImg = false;
                  }
               }
            }
            // Image is different, so add it to the list.
            if (addImg)
            {
               //imageList1.Images.Add(bmp);
               lstBoxCpyItms.Items.Add(bmp);
               bmpClipCurrent = bmp;
            }
            // First image retrieved from Clipboard.
            // Therefore, add to list.
         }
         else
         {
            //imageList1.Images.Add(bmp);
            lstBoxCpyItms.Items.Add(bmp);
            bmpClipCurrent = bmp;
         }
      }
   }
}

Clipboard Ring Application

The following method acts as an eventhandler for the double-click event of the ListBox. The type of the selected listitem object is compared and handled accordingly. Here, a listbox entry is made as the first entry to the list when the application starts.

private void lstBoxCpyItms_DoubleClick(object sender, EventArgs e)
{
   // if an item in the ListBox is selected
   if (lstBoxCpyItms.SelectedItems.Count > 0)
   {
      //if the type of the object in the selected list item is string.
      if (lstBoxCpyItms.SelectedItem.GetType().ToString() ==
          "System.String")
      {
         string strSelItm = lstBoxCpyItms.SelectedItem.ToString();
         // if the selected string is not the last retrieved string
         // from the Clipboard, set the string to Clipboard.
         if (strSelItm != strClipCurrent)
         {
            strClipCurrent = strSelItm;
            Clipboard.SetDataObject(lstBoxCpyItms.SelectedItem.
                                    ToString());
            // To make the first entry - when application starts
            if (!lstBoxCpyItms.Items.Contains(strClipCurrent))
            {
               lstBoxCpyItms.Items.Add(strClipCurrent);
            }
         }
      }

      // if the selected object in the ListBox item is an image
      if (lstBoxCpyItms.SelectedItem.GetType().ToString() ==
          "System.Drawing.Bitmap")
      {
         Bitmap bmpSelItm = (Bitmap)lstBoxCpyItms.SelectedItem;
         bmpClipCurrent = bmpSelItm;
         Clipboard.SetDataObject((Bitmap)lstBoxCpyItms.SelectedItem,
                                 true);
      }
      this.Hide();
   }
   lstBoxCpyItms.SelectedIndex = -1;
}

Yahoo! Messenger-Style Alert

I always wanted to create a Yahoo! Messenger-like alert popup. So, I thought this was the right time to work on it and this is the right application for me to try it. The easiest solution I came to was to use a timer component. I know that it needs optimization, but this solution works fine. I am happy for the time being.

Following is a quick snap of its implementation.

In the form load event two variables, xPos and yPos, are set. xPos will have the desktop area width and yPos will have the desktop area's height.

The GetWorkingArea method of the Screen(System.Windows.Forms.Screen) class retrives the working area (desktop area in case of your form—the working area is dependent on the container of the control) excluding the taskbar and any other docked toolbars.

xPos = Screen.GetWorkingArea(this).Width;
yPos = Screen.GetWorkingArea(this).Height;

Basically, I have used two timer components for this functionality: tmr1 and tmr2. Each has an tick interval of 1 millisecond.

tmr1 is enabled when the notify icon in the system tray is clicked. Each time it ticks, the Y location of the form is decreased so that the form raises from the bottom boundary of the screen.

private void tmr1_Tick(object sender, EventArgs e)
{
   int curPos = this.Location.Y;
   if (curPos > yPos - this.Height)
   {
      this.Location = new Point(xPos - this.Width, curPos - 20);
   }
   else
   {
      tmr1.Stop();
      tmr1.Enabled = false;
   }
}

tmr2 is enabled when the close button is clicked. Each time it ticks, the Y location of the form is increased to take the form below the visible screen space and ultimately take it out of the user's visibily. At this point, this timer is disabled.

private void tmr2_Tick(object sender, EventArgs e)
{
   int curPos = this.Location.Y;

   if (curPos < (yPos + 30))
   {
      this.Location = new Point(xPos - this.Width, curPos + 20);
   }
   else
   {
      tmr2.Stop();
      tmr2.Enabled = false;
   }
}

The logic is quite simple and you can easily understand it taking a look at it. You can change the timer interval and timing to get variations of the animation effect.

Handling Images in Listview

In the attached application, the Clipboard ring is implemented in two forms. The first one, Form1.cs, is a listbox implementation; the second one, ClipboardWithImage.cs, is a listview implementation.

In the listview implementation, an image thumbnail is shown towards the left of each image entry. The implementation of this is quite simple. An ImageList control is set to the SmallImageList property of the ListView control. The dimension of images displayed in the ListView is set by setting the ImageSize property of ImageList control. The ImageSize property is of type size, so its Height and Width properties can be set individually. Anyway, that's not an issue because it can be set in the design view.

[ClipboardImageApp.jpg]

Each time a new picture entry is to be made into the Listview, the image is added to the ImageList control and the corresponding index of the image in the ImageList is set to the ListView items imageindex parameter.

Points to Remember

  1. Please install .NET Framework 2.0 to run the application. The current attached code has been generated by using Visual Studio 2005 and was complied for .NET Framework 2.0. The implementation in Framework 1.1 is very similar except for one functionality mentioned above (the GetImage method).
  2. To test images, you can use MS Paint. Both Copy and Paste options can be tested using the same.


About the Author

Mohammed Habeeb

Mohammed Habeeb works as a software developer for an IT company in Dubai. He holds a bachelors in Computer Science Engineering from MES College, Calicut University. He is also a Microsoft Certified Application Developer (MCAD) in .NET Framework. He has a strong inclination towards Microsoft technologies especially the .NET Platform. He has been an active member of Cochin and Bangalore Microsoft user groups. He has a strong passion for science and technology. His interests span through travelling, driving, photography, stamps and coin collection. You can find more about him @ http://www.habeebonline.com

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

  • Specialization and efficiency are always in need. Whether it's replacing an aging roof, getting a haircut, or tuning up a car, most seek the assistance of trusted experts. The same is true in the business world, where an increasing number of companies are seeking the help of others to administer their IT systems and services. This special edition of Unleashing IT highlights a new breed of IT caretaker -- Cisco Powered service providers -- and the business advantages and operational efficiencies they …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds