Csharp/C Sharp/GUI Windows Form/Clipboard

Материал из .Net Framework эксперт
Версия от 11:32, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Clipboard Viewer (All Formats)

 

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[i] != astrFormatsSave[i]) {
                    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[i];
            if (!data.GetDataPresent(astrFormats[i], false))
                radio.Text += "*";
            try {
                object objClip = data.GetData(astrFormats[i]);
                radio.Text += " (" + objClip.GetType() + ")";
            } catch {
                radio.Text += " (Exception on GetData or GetType!)";
            }
            radio.Tag = astrFormats[i];
            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[]))   // FileDrop
          {
            string str = string.Join("\r\n", (string[])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[] abyBuffer = new byte[16];
            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;
            }
        }
    }
}


Clip Text

using System;
using System.Drawing;
using System.Windows.Forms;
   
class ClipText: Form
{
     string   strText = "Sample text";
     MenuItem miCut, miCopy, miPaste;
   
     public static void Main()
     {
          Application.Run(new ClipText());
     }
     public ClipText()
     {
          ResizeRedraw = true;
   
          Menu = new MainMenu();
          MenuItem mi = new MenuItem("&Edit");
          mi.Popup += new EventHandler(MenuEditOnPopup);
          Menu.MenuItems.Add(mi);
          miCut = new MenuItem("Cu&t");
          miCut.Click += new EventHandler(MenuEditCutOnClick);
          miCut.Shortcut = Shortcut.CtrlX;
          Menu.MenuItems[0].MenuItems.Add(miCut);
   
          miCopy = new MenuItem("&Copy");
          miCopy.Click += new EventHandler(MenuEditCopyOnClick);
          miCopy.Shortcut = Shortcut.CtrlC;
          Menu.MenuItems[0].MenuItems.Add(miCopy);
   
          miPaste = new MenuItem("&Paste");
          miPaste.Click += new EventHandler(MenuEditPasteOnClick);
          miPaste.Shortcut = Shortcut.CtrlV;
          Menu.MenuItems[0].MenuItems.Add(miPaste);
     }
     void MenuEditOnPopup(object obj, EventArgs ea)
     {
          miCut.Enabled = miCopy.Enabled = strText.Length > 0;
          miPaste.Enabled = Clipboard.GetDataObject().GetDataPresent(typeof(string));
     }
     void MenuEditCutOnClick(object obj, EventArgs ea)
     {
          MenuEditCopyOnClick(obj, ea);
          strText = "";
          Invalidate();
     }
     void MenuEditCopyOnClick(object obj, EventArgs ea)
     {
          Clipboard.SetDataObject(strText, true);
     }
     void MenuEditPasteOnClick(object obj, EventArgs ea)
     {
          IDataObject data = Clipboard.GetDataObject();
   
          if (data.GetDataPresent(typeof(string)))
               strText = (string) data.GetData(typeof(string));
   
          Invalidate();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;
          StringFormat strfmt = new StringFormat();
          strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;
   
          grfx.DrawString(strText, Font, new SolidBrush(ForeColor),
                          ClientRectangle, strfmt);
     }
}


Copying from the Clipboard.

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Windows.Forms;
public class MainClass {

    public static void Main(string[] args) {
        IDataObject iData = Clipboard.GetDataObject();
        if (iData.GetDataPresent(DataFormats.Text)) {
            Console.WriteLine((String)iData.GetData(DataFormats.Text));
        }
        if (iData.GetDataPresent(DataFormats.Bitmap)) {
            Image img = (Bitmap)iData.GetData(DataFormats.Bitmap);
        }
    }
}


Copying to the Clipboard.

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ruponentModel;
  using System.Data;
  using System.Windows.Forms;
  
  public class MainClass
  {
    public static void Main(string[] args)
    {
        Clipboard.SetDataObject("data");
    }
  }


Image Clip

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
class ImageClip : Form {
    MenuItem miCut, miCopy, miPaste, miDel;
    Image image;
    public static void Main() {
        Application.Run(new ImageClip());
    }
    public ImageClip() {
        Text = "Image Clip";
        MenuItem mi = new MenuItem("&Edit");
        mi.Popup += new EventHandler(MenuEditOnPopup);
        Menu.MenuItems.Add(mi);
        int index = Menu.MenuItems.Count - 1;
        miCut = new MenuItem("Cu&t");
        miCut.Click += new EventHandler(MenuEditCutOnClick);
        miCut.Shortcut = Shortcut.CtrlX;
        Menu.MenuItems[index].MenuItems.Add(miCut);
        miCopy = new MenuItem("&Copy");
        miCopy.Click += new EventHandler(MenuEditCopyOnClick);
        miCopy.Shortcut = Shortcut.CtrlC;
        Menu.MenuItems[index].MenuItems.Add(miCopy);
        miPaste = new MenuItem("&Paste");
        miPaste.Click += new EventHandler(MenuEditPasteOnClick);
        miPaste.Shortcut = Shortcut.CtrlV;
        Menu.MenuItems[index].MenuItems.Add(miPaste);
        miDel = new MenuItem("De&lete");
        miDel.Click += new EventHandler(MenuEditDelOnClick);
        miDel.Shortcut = Shortcut.Del;
        Menu.MenuItems[index].MenuItems.Add(miDel);
    }
    void MenuEditOnPopup(object obj, EventArgs ea) {
        miCut.Enabled = miCopy.Enabled = miDel.Enabled = image != null;
        IDataObject data = Clipboard.GetDataObject();
        miPaste.Enabled = data.GetDataPresent(typeof(Bitmap)) || data.GetDataPresent(typeof(Metafile));
    }
    void MenuEditCutOnClick(object obj, EventArgs ea) {
        MenuEditCopyOnClick(obj, ea);
        MenuEditDelOnClick(obj, ea);
    }
    void MenuEditCopyOnClick(object obj, EventArgs ea) {
        Clipboard.SetDataObject(image, true);
    }
    void MenuEditPasteOnClick(object obj, EventArgs ea) {
        IDataObject data = Clipboard.GetDataObject();
        if (data.GetDataPresent(typeof(Metafile)))
            image = (Image)data.GetData(typeof(Metafile));
        else if (data.GetDataPresent(typeof(Bitmap)))
            image = (Image)data.GetData(typeof(Bitmap));
        Invalidate();
    }
    void MenuEditDelOnClick(object obj, EventArgs ea) {
        image = null;
        Invalidate();
    }
}


Rich-Text Paste

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
   
class RichTextPaste: Form
{
     string   strPastedText = "";
     MenuItem miPastePlain, miPasteRTF, miPasteHTML, miPasteCSV;
   
     public static void Main()
     {
          Application.Run(new RichTextPaste());
     }
     public RichTextPaste()
     {
          ResizeRedraw = true;
   
          Menu = new MainMenu();
   
          MenuItem mi = new MenuItem("&Edit");
          mi.Popup += new EventHandler(MenuEditOnPopup);
          Menu.MenuItems.Add(mi);
   
          miPastePlain = new MenuItem("Paste &Plain Text");
          miPastePlain.Click += new EventHandler(MenuEditPastePlainOnClick);
          Menu.MenuItems[0].MenuItems.Add(miPastePlain);
   
          miPasteRTF = new MenuItem("Paste &Rich Text Format");
          miPasteRTF.Click += new EventHandler(MenuEditPasteRTFOnClick);
          Menu.MenuItems[0].MenuItems.Add(miPasteRTF);
   
          miPasteHTML = new MenuItem("Paste &HTML");
          miPasteHTML.Click += new EventHandler(MenuEditPasteHTMLOnClick);
          Menu.MenuItems[0].MenuItems.Add(miPasteHTML);
   
          miPasteCSV = new MenuItem("Paste &Comma-Separated Values");
          miPasteCSV.Click += new EventHandler(MenuEditPasteCSVOnClick);
          Menu.MenuItems[0].MenuItems.Add(miPasteCSV);
     }
     void MenuEditOnPopup(object obj, EventArgs ea)
     {
          miPastePlain.Enabled = Clipboard.GetDataObject().GetDataPresent(typeof(string));
          miPasteRTF.Enabled = Clipboard.GetDataObject().GetDataPresent(DataFormats.Rtf);
          miPasteHTML.Enabled = Clipboard.GetDataObject().GetDataPresent(DataFormats.Html);
          miPasteCSV.Enabled = Clipboard.GetDataObject().GetDataPresent(DataFormats.rumaSeparatedValue);
     }
     void MenuEditPastePlainOnClick(object obj, EventArgs ea)
     {
          IDataObject data = Clipboard.GetDataObject();
   
          if (data.GetDataPresent(typeof(string)))
          {
               strPastedText = (string) data.GetData(typeof(string));
               Invalidate();
          }
     }
     void MenuEditPasteRTFOnClick(object obj, EventArgs ea)
     {
          IDataObject data = Clipboard.GetDataObject();
   
          if (data.GetDataPresent(DataFormats.Rtf))
          {
               strPastedText = (string) data.GetData(DataFormats.Rtf);
               Invalidate();
          }
     }
     void MenuEditPasteHTMLOnClick(object obj, EventArgs ea)
     {
          IDataObject data = Clipboard.GetDataObject();
   
          if (data.GetDataPresent(DataFormats.Html))
          {
               strPastedText = (string) data.GetData(DataFormats.Html);
               Invalidate();
          }
     }
     void MenuEditPasteCSVOnClick(object obj, EventArgs ea)
     {
          IDataObject data = Clipboard.GetDataObject();
   
          if (data.GetDataPresent(DataFormats.rumaSeparatedValue))
          {
               MemoryStream memstr = (MemoryStream) data.GetData("Csv");
               StreamReader streamreader = new StreamReader(memstr);
               strPastedText = streamreader.ReadToEnd();
               Invalidate();
          }
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          pea.Graphics.DrawString(strPastedText, Font, new SolidBrush(ForeColor),
                          ClientRectangle);
     }
}