Csharp/CSharp Tutorial/GUI Windows Forms/Clipboard

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

Clipboard Formats Available

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
  public class FormatsForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.ListBox lbFormats;
    private System.Windows.Forms.Button cmdCheckClipboard;
    public FormatsForm()
    {
      this.lbFormats = new System.Windows.Forms.ListBox();
      this.cmdCheckClipboard = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // lbFormats
      // 
      this.lbFormats.Location = new System.Drawing.Point(8, 8);
      this.lbFormats.Name = "lbFormats";
      this.lbFormats.Size = new System.Drawing.Size(272, 199);
      this.lbFormats.TabIndex = 0;
      // 
      // cmdCheckClipboard
      // 
      this.cmdCheckClipboard.Location = new System.Drawing.Point(8, 216);
      this.cmdCheckClipboard.Name = "cmdCheckClipboard";
      this.cmdCheckClipboard.Size = new System.Drawing.Size(104, 23);
      this.cmdCheckClipboard.TabIndex = 1;
      this.cmdCheckClipboard.Text = "check clipboard";
      this.cmdCheckClipboard.Click += new System.EventHandler(this.cmdCheckClipboard_Click);
      // 
      // FormatsForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.cmdCheckClipboard,
                                      this.lbFormats});
      this.Name = "FormatsForm";
      this.Text = "View Formats";
      this.Load += new System.EventHandler(this.FormatsForm_Load);
      this.ResumeLayout(false);
    }
    static void Main() 
    {
      Application.Run(new FormatsForm());
    }
    private void cmdCheckClipboard_Click(object sender, System.EventArgs e)
    {
      lbFormats.Items.Clear();
      IDataObject data = Clipboard.GetDataObject();
      string[] astrFormats = data.GetFormats(true);
      for (int i=0; i < astrFormats.Length; i++)
        lbFormats.Items.Add(astrFormats[i]);
    }
    private void FormatsForm_Load(object sender, System.EventArgs e)
    {
    
    }
  }

Clipboard Set Data Object

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
  public class MultipleFormatsForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button cmdSet;
    public MultipleFormatsForm()
    {
      this.cmdSet = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // cmdSet
      // 
      this.cmdSet.Location = new System.Drawing.Point(56, 40);
      this.cmdSet.Name = "cmdSet";
      this.cmdSet.Size = new System.Drawing.Size(160, 23);
      this.cmdSet.TabIndex = 0;
      this.cmdSet.Text = "set multiple formats";
      this.cmdSet.Click += new System.EventHandler(this.cmdSet_Click);
      // 
      // MultipleFormatsForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 117);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.cmdSet});
      this.Name = "MultipleFormatsForm";
      this.Text = "Set multiple formats";
      this.ResumeLayout(false);
    }
    static void Main() 
    {
      Application.Run(new MultipleFormatsForm());
    }
    private void cmdSet_Click(object sender, System.EventArgs e)
    {
      string strText = "Hello World";
      string strHtml = "<h1>Hello World</h1>";
      DataObject data = new DataObject();
      data.SetData(strText);
      data.SetData(DataFormats.Html, strHtml);
      data.SetData("My.Internal.Format","Some internal data");
      Clipboard.SetDataObject(data, true);
    }
  }

Clipboard Viewer

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
   
class ClipView: Form
{
     string[] astrFormats = 
     { 
     DataFormats.Bitmap, DataFormats.rumaSeparatedValue, DataFormats.Dib,
     DataFormats.Dif, DataFormats.EnhancedMetafile, DataFormats.FileDrop, 
     DataFormats.Html, DataFormats.Locale, DataFormats.MetafilePict, 
     DataFormats.OemText, DataFormats.Palette, DataFormats.PenData, 
     DataFormats.Riff, DataFormats.Rtf, DataFormats.Serializable, 
     DataFormats.StringFormat, DataFormats.SymbolicLink, DataFormats.Text, 
     DataFormats.Tiff, DataFormats.UnicodeText, DataFormats.WaveAudio 
     };
     
     Panel         panelDisplay;
     RadioButton[] aradio;
     RadioButton   radioChecked;
   
     public static void Main()
     {
          Application.Run(new ClipView());
     }
     public ClipView()
     {
          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;
   
          Panel panel = new Panel();
          panel.Parent = this;
          panel.Dock = DockStyle.Left;
          panel.Width = 200;
          
          aradio = new RadioButton[astrFormats.Length];
          EventHandler eh = new EventHandler(RadioButtonOnClick);
   
          for (int i = 0; i < astrFormats.Length; i++)
          {
               aradio[i] = new RadioButton();
               aradio[i].Parent = panel;
               aradio[i].Location = new Point(4, 12 * i);
               aradio[i].Size = new Size(300, 12);
               aradio[i].Click += eh;
               aradio[i].Tag = astrFormats[i];
          }
          AutoScaleBaseSize = new Size(4, 8);
   
          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();
   
          for (int i = 0; i < astrFormats.Length; i++)
          {
               aradio[i].Text = astrFormats[i];
               aradio[i].Enabled = data.GetDataPresent(astrFormats[i]);
   
               if (aradio[i].Enabled)
               {
                    if (!data.GetDataPresent(astrFormats[i], false))
                         aradio[i].Text += "*";
   
                    object objClip = data.GetData(astrFormats[i]);
   
                    try
                    {
                         aradio[i].Text += " (" + objClip.GetType() + ")";
                    }
                    catch
                    {
                         aradio[i].Text += " (Exception on GetType!)";
                    }
               }
          }
          panelDisplay.Invalidate();
     }
     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 || !radioChecked.Enabled)
               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;
                    y += font.GetHeight(grfx);
   
                    if (y > panel.Bottom)
                         break;
               }
          }
     }
}

Simple Clipboard

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
  public class ClipboardTestForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button cmdString2Clipboard;
    private System.Windows.Forms.Button cmdImage2Clipboard;
    private System.Windows.Forms.Button cmdGetAsString;
    private System.Windows.Forms.Button cmdGetImage;
    public ClipboardTestForm()
    {
      this.cmdString2Clipboard = new System.Windows.Forms.Button();
      this.cmdImage2Clipboard = new System.Windows.Forms.Button();
      this.cmdGetAsString = new System.Windows.Forms.Button();
      this.cmdGetImage = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // cmdString2Clipboard
      // 
      this.cmdString2Clipboard.Location = new System.Drawing.Point(8, 16);
      this.cmdString2Clipboard.Name = "cmdString2Clipboard";
      this.cmdString2Clipboard.Size = new System.Drawing.Size(136, 23);
      this.cmdString2Clipboard.TabIndex = 0;
      this.cmdString2Clipboard.Text = "put string in clipboard";
      this.cmdString2Clipboard.Click += new System.EventHandler(this.cmdString2Clipboard_Click);
      // 
      // cmdImage2Clipboard
      // 
      this.cmdImage2Clipboard.Location = new System.Drawing.Point(8, 48);
      this.cmdImage2Clipboard.Name = "cmdImage2Clipboard";
      this.cmdImage2Clipboard.Size = new System.Drawing.Size(136, 23);
      this.cmdImage2Clipboard.TabIndex = 1;
      this.cmdImage2Clipboard.Text = "put image in clipboard";
      this.cmdImage2Clipboard.Click += new System.EventHandler(this.cmdImage2Clipboard_Click);
      // 
      // cmdGetAsString
      // 
      this.cmdGetAsString.Location = new System.Drawing.Point(176, 16);
      this.cmdGetAsString.Name = "cmdGetAsString";
      this.cmdGetAsString.Size = new System.Drawing.Size(136, 23);
      this.cmdGetAsString.TabIndex = 2;
      this.cmdGetAsString.Text = "get as string";
      this.cmdGetAsString.Click += new System.EventHandler(this.cmdGetAsString_Click);
      // 
      // cmdGetImage
      // 
      this.cmdGetImage.Location = new System.Drawing.Point(176, 48);
      this.cmdGetImage.Name = "cmdGetImage";
      this.cmdGetImage.Size = new System.Drawing.Size(136, 23);
      this.cmdGetImage.TabIndex = 3;
      this.cmdGetImage.Text = "get image";
      this.cmdGetImage.Click += new System.EventHandler(this.cmdGetImage_Click);
      // 
      // ClipboardTestForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(320, 149);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.cmdGetImage,
                                      this.cmdGetAsString,
                                      this.cmdImage2Clipboard,
                                      this.cmdString2Clipboard});
      this.Name = "ClipboardTestForm";
      this.Text = "Clipboard Test";
      this.ResumeLayout(false);
    }
    static void Main() 
    {
      Application.Run(new ClipboardTestForm());
    }
    private void cmdString2Clipboard_Click(object sender, System.EventArgs e)
    {
      string strText = "Hello World";
      Clipboard.SetDataObject(strText, true);
    }
    private void cmdImage2Clipboard_Click(object sender, System.EventArgs e)
    {
      Bitmap bmp2Clipboard = new Bitmap("pinz.jpg");
      Clipboard.SetDataObject(bmp2Clipboard, true);
    }
    private void cmdGetAsString_Click(object sender, System.EventArgs e)
    {
      IDataObject data = Clipboard.GetDataObject();
      if (data.GetDataPresent(typeof(string)))
      {
        string strData = (string)data.GetData(typeof(string));
        MessageBox.Show(strData);
      }
      else
      {
        MessageBox.Show("Data not retrievable as string");
      }
    }
    private void cmdGetImage_Click(object sender, System.EventArgs e)
    {
      IDataObject data = Clipboard.GetDataObject();
      if (data.GetDataPresent(typeof(Bitmap)))
      {
        Bitmap bmp = (Bitmap)data.GetData(typeof(Bitmap));
        bmp.Save(@"c:\cliptest.bmp");
        MessageBox.Show("Saved to c:\\cliptest.bmp");
      }
      else
      {
        MessageBox.Show("Data not retrievable as bitmap");
      }
    }
  }