Building a Clipboard Ring Utility Application Using C#
page 4 of 7
by Mohammed Habeeb
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 36122/ 41

Clipboard Ring Application

This is the method which retrieves text/image from the system Clipboard and makes an entry into a ListBox, where it resides till the application exits. The current data on the clipboard is retrieved and added to the ListBox Items collection. By default only objects with Text format is retrieved from Clipboard and added to the ListBox. To store images, right click on the notify icon on 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 variable strClipCurrent. Next time, it is compared with the current string on 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 code make things very clear.

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

Listing 6 - Retrieve text/image from the system Clipboard to ListBox (Clipboard Ring Application)

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;
            }
        }
    }
}

The following method acts as an eventhandler for the double click event of the ListBox. 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.

Listing 7 - Retrieve text/image from List Box to System Clipboard (Clipboard Ring Application)

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;
}

View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 11:44:04 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search