| if ( d.GetDataPresent ( DataFormats.Bitmap ) ) { Console.WriteLine ( "当前剪切板中的数据类型是位图!" ) ; } else if ( d.GetDataPresent ( DataFormats.Text ) ) { Console.WriteLine ( "当前剪切板中的数据类型是文本!" ) ; } else if ( d.GetDataPresent ( DataFormats.Html ) ) { Console.WriteLine ( "当前剪切板中的数据类型是超文本!" ) ; } else { Console.WriteLine ( "当前剪切板中的数据类型是其他类型数据!" ) ; } |
| using System ; using System.Windows.Forms ; public class MyClipboard { public static void Main ( string [ ] args ) { IDataObject d = Clipboard.GetDataObject ( ) ; if ( d.GetDataPresent ( DataFormats.Bitmap ) ) { Console.WriteLine ( "当前剪切板中的数据类型是位图!" ) ; } else if ( d.GetDataPresent ( DataFormats.Text ) ) { Console.WriteLine ( "当前剪切板中的数据类型是文本!" ) ; } else if ( d.GetDataPresent ( DataFormats.Html ) ) { Console.WriteLine ( "当前剪切板中的数据类型是超文本!" ) ; } else { Console.WriteLine ( "当前剪切板中的数据类型是其他类型数据!" ) ; } } } |
| object GetData ( Type ) ; |
| Bitmap b = ( Bitmap ) d.GetData ( DataFormats.Bitmap ) ;//位图 String c = ( String ) d.GetData ( DataFormats.Text ) ;//文本 |
![]() 图01:数据类型为位图的时候,程序运行界面 |
![]() 图02:数据类型为文本时程序运行界面 |
| Bitmap b = ( Bitmap ) d.GetData ( DataFormats.Bitmap ) ; b.Save ( @"c:\my.bmp" ) ; MessageBox.Show ( "当前剪切板内容是位图,已经保存到"MY.BMP"文件中!" ) ; |
| using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data; public class Form1 : Form { private RichTextBox richTextBox1 ; private Button button1 ; private System.ComponentModel.Container components = null ; public Form1() { //初始化窗体中的各个组件 InitializeComponent ( ) ; } //清除程序中使用过的资源 protected override void Dispose ( bool disposing ) { if ( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose ( disposing ); } private void InitializeComponent ( ) { this.richTextBox1 = new RichTextBox ( ) ; this.button1 = new Button ( ) ; this.SuspendLayout ( ) ; this.richTextBox1.Location = new System.Drawing.Point ( 40 , 16 ) ; this.richTextBox1.Name = "richTextBox1" ; this.richTextBox1.Size = new System.Drawing.Size ( 336 , 264 ) ; this.richTextBox1.TabIndex = 0 ; this.richTextBox1.Text = "" ; this.button1.Location = new System.Drawing.Point ( 128 , 304 ) ; this.button1.Name = "button1" ; this.button1.Size = new System.Drawing.Size ( 128 , 24 ) ; this.button1.TabIndex = 1 ; this.button1.Text = "获得剪切板中的数据" ; this.button1.Click += new System.EventHandler ( this.button1_Click ) ; this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ; this.ClientSize = new System.Drawing.Size ( 408 , 357 ) ; this.Controls.Add ( button1 ); this.Controls.Add ( richTextBox1 ); this.Name = "Form1"; this.Text = "用Visual C#来保存剪切板中的数据!"; this.ResumeLayout(false); } static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } private void button1_Click ( object sender , System.EventArgs e ) { //定义一个IDataObject接口 IDataObject d = Clipboard.GetDataObject ( ) ; //如果剪切板中数据是位图,则另存为C盘的my.bmp文件 if ( d.GetDataPresent ( DataFormats.Bitmap ) ) { //出箱 Bitmap b = ( Bitmap ) d.GetData ( DataFormats.Bitmap ) ; b.Save ( @"c:\my.bmp" ) ; MessageBox.Show ( "当前剪切板内容是位图,已经保存到"MY.BMP"文件中!" ) ; } //如果是文本,则用窗体中的RichText组件显示文本内容。 else if ( d.GetDataPresent ( DataFormats.Text ) ) { //出箱 String c = ( String ) d.GetData ( DataFormats.Text ) ; richTextBox1.Text = c ; } else { MessageBox.Show ( "剪切板中是其他类型的数据!" ) ; } } } |