Clipboard Viewer (All Formats)

image_pdfimage_print
   
 


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

class ClipViewAll : Form {
    Panel panelDisplay, panelButtons;
    RadioButton radioChecked;
    string[] astrFormatsSave = new string[0];

    public static void Main() {
        Application.Run(new ClipViewAll());
    }
    public ClipViewAll() {
        panelDisplay = new Panel();
        panelDisplay.Parent = this;
        panelDisplay.Dock = DockStyle.Fill;
        panelDisplay.Paint += new PaintEventHandler(PanelOnPaint);
        panelDisplay.BorderStyle = BorderStyle.Fixed3D;

        Splitter split = new Splitter();
        split.Parent = this;
        split.Dock = DockStyle.Left;

        panelButtons = new Panel();
        panelButtons.Parent = this;
        panelButtons.Dock = DockStyle.Left;
        panelButtons.AutoScroll = true;
        panelButtons.Width = Width / 2;

        Timer timer = new Timer();
        timer.Interval = 1000;
        timer.Tick += new EventHandler(TimerOnTick);
        timer.Enabled = true;
    }
    void TimerOnTick(object obj, EventArgs ea) {
        IDataObject data = Clipboard.GetDataObject();

        string[] astrFormats = data.GetFormats();
        bool bUpdate = false;

        if (astrFormats.Length != astrFormatsSave.Length)
            bUpdate = true;
        else {
            for (int i = 0; i < astrFormats.Length; i++)
                if (astrFormats&#91;i&#93; != astrFormatsSave&#91;i&#93;) {
                    bUpdate = true;
                    break;
                }
        }

        panelDisplay.Invalidate();

        if (!bUpdate)
            return;

        astrFormatsSave = astrFormats;
        panelButtons.Controls.Clear();
        Graphics grfx = CreateGraphics();
        EventHandler eh = new EventHandler(RadioButtonOnClick);
        int cxText = AutoScaleBaseSize.Width;
        int cyText = AutoScaleBaseSize.Height;

        for (int i = 0; i < astrFormats.Length; i++) {
            RadioButton radio = new RadioButton();
            radio.Parent = panelButtons;
            radio.Text = astrFormats&#91;i&#93;;

            if (!data.GetDataPresent(astrFormats&#91;i&#93;, false))
                radio.Text += "*";

            try {
                object objClip = data.GetData(astrFormats&#91;i&#93;);
                radio.Text += " (" + objClip.GetType() + ")";
            } catch {
                radio.Text += " (Exception on GetData or GetType!)";
            }
            radio.Tag = astrFormats&#91;i&#93;;
            radio.Location = new Point(cxText, i * 3 * cyText / 2);
            radio.Size = new Size((radio.Text.Length + 20) * cxText,
                                  3 * cyText / 2);
            radio.Click += eh;
        }
        grfx.Dispose();
        radioChecked = null;
    }
    void RadioButtonOnClick(object obj, EventArgs ea) {
        radioChecked = (RadioButton)obj;
        panelDisplay.Invalidate();
    }
    void PanelOnPaint(object obj, PaintEventArgs pea) {
        Panel panel = (Panel)obj;
        Graphics grfx = pea.Graphics;
        Brush brush = new SolidBrush(panel.ForeColor);

        if (radioChecked == null)
            return;

        IDataObject data = Clipboard.GetDataObject();
        object objClip = data.GetData((string)radioChecked.Tag);

        if (objClip == null)
            return;

        else if (objClip.GetType() == typeof(string)) {
            grfx.DrawString((string)objClip, Font, brush,
                            panel.ClientRectangle);
        } else if (objClip.GetType() == typeof(string&#91;&#93;))   // FileDrop
          {
            string str = string.Join("
", (string&#91;&#93;)objClip);

            grfx.DrawString(str, Font, brush, panel.ClientRectangle);
        } else if (objClip.GetType() == typeof(Bitmap) ||
                   objClip.GetType() == typeof(Metafile) ||
                   objClip.GetType() == typeof(Image)) {
            grfx.DrawImage((Image)objClip, 0, 0);
        } else if (objClip.GetType() == typeof(MemoryStream)) {
            Stream stream = (Stream)objClip;
            byte&#91;&#93; abyBuffer = new byte&#91;16&#93;;
            long lAddress = 0;
            int iCount;
            Font font = new Font(FontFamily.GenericMonospace,
                                   Font.SizeInPoints);
            float y = 0;

            while ((iCount = stream.Read(abyBuffer, 0, 16)) > 0) {
                lAddress += 16;
            }
        }
    }
}