Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?
Save up to 50% on our desktop apps during our Year End Sale!Save up to 50% on our desktop apps during our Year End Sale, including DisplayFusion, ClipboardFusion, FileSeek, LogFusion, TrayStatus, and VoiceBot!Save up to 50% on our desktop apps during our Year End Sale!

Classic Style History Menu

Description
This macro recreates the classic style of the history menu from ClipboardFusion 4.x.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
Mar 23, 2017
Date Last Modified
Mar 23, 2017

Macro Code

using System;
using System.Collections.Specialized;
using System.Drawing;
using System.Windows.Forms;

// The 'text' parameter will contain the text from the:
//   - Current Clipboard when run by HotKey
//   - History Item when run from the History Menu
// The returned string will be:
//   - Placed directly on the Clipboard
//   - Ignored by ClipboardFusion if it is 'null'
public static class ClipboardFusionHelper
{
	public static string ProcessText(string text)
	{
        //create a new ContextMenuStrip to show the items
		using(ContextMenuStrip menu = new ContextMenuStrip())
		{
            //dont show the padding on the left of the menu
			menu.ShowCheckMargin = false;
			menu.ShowImageMargin = false;
			
			//set the font so it isn't huge
			menu.Font = new Font(menu.Font.FontFamily, 8, FontStyle.Regular);
			
			//loop through the first 25 history items
			for(int i = 0; (i < BFS.ClipboardFusion.GetHistoryItemCount()) && (i < 25); i++)
			{
                //get the history item data
                text = BFS.ClipboardFusion.GetHistoryText(i);
                Bitmap image = BFS.ClipboardFusion.GetHistoryImage(i);
                string[] fileList = BFS.ClipboardFusion.GetHistoryFileList(i);
                
                //strip the newline characters out of the text for the title, and limit it to 50 characters
                string title = string.IsNullOrEmpty(text) ? "[Empty]" : text;
                title = title.Replace(Environment.NewLine, "\n").Replace("\r", "\n").Replace("\n", "");
                if(title.Length > 50)
                    title = title.Substring(0, 50);
                
                //make the context menu item
                ToolStripMenuItem item = new ToolStripMenuItem(title);
                
                //if the item contains an image, set the background image to the item
                if(image != null)
                {
                    item.BackgroundImage = image;
                    item.BackgroundImageLayout = ImageLayout.Stretch;
                }
                
                //make the ClipboardData object
                ClipboardData data = new ClipboardData(text, image, fileList);	
                
                //set the click event, set the ClipboardData to the tag, and add the item to the menu
                item.Click += ItemClick;
                item.Tag = data;
                menu.Items.Add(item);
			}
            
            //add an item that you can click to close the menu
            menu.Items.Add("Close");
            menu.Items[menu.Items.Count - 1].ForeColor = Color.Red;
            
			//show the menu
            menu.Show(BFS.Input.GetMousePositionX(), BFS.Input.GetMousePositionY());

            //wait for the menu to close
			while(menu.Visible)
				Application.DoEvents();
				
            //dispose the objects
            foreach(ToolStripMenuItem item in menu.Items)
            {
                ClipboardData data = item.Tag as ClipboardData;
                if(data != null)
                    data.Dispose();
                    
                item.Dispose();
            }
		}
		
		return null;
	}
	
	//this function will be called when you click on a regular menu item
	private static void ItemClick(object sender, EventArgs e)
	{
        try
        {
            //get the menu item object
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            if(item == null)
                throw new Exception("ToolStripMenuItem is NULL");
             
            //get the ClipboardData object
            ClipboardData data = item.Tag as ClipboardData;
            if(data == null)
                throw new Exception("ClipboardData is NULL");
            
            //depending on what data we have, set the clipboard
            if(data.Image != null)
            {
                Clipboard.SetImage(data.Image);
                BFS.Clipboard.Paste();
            }
            else if(data.FileList.Length > 0)
            {
                StringCollection collection = new StringCollection();
                collection.AddRange(data.FileList);
                Clipboard.SetFileDropList(collection);
                BFS.Clipboard.Paste();
            }
            else
            {            
                BFS.Clipboard.PasteText(data.Text);
            }
        }
        catch(Exception ex)
        {
            //if something happened, show an error
            BFS.Dialog.ShowMessageError(ex.Message);
        }
	}
	
	//this is an object that will store the Clipboard History Item data
	private class ClipboardData : IDisposable
	{
        public string Text;
        public Bitmap Image;
        public string[] FileList;
        
        public ClipboardData(string text, Bitmap image, string[] fileList)
        {
            this.Text = text;
            this.Image = image;
            this.FileList = fileList;
        }
        
        public void Dispose()
        {
            if(this.Image != null)
                this.Image.Dispose();
        }
	}
}