| | | Visual C#的剪切板编程 | | 2002-01-17·
·阿虎··yesky
| 上一页 1 2 3 4 5 六. 用Visual C#读取当前剪切板中数据内容并保存的程序源代码:
通过上面的介绍,可以得到实现上述功能的源程序代码,如下:
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 ( "剪切板中是其他类型的数据!" ) ; } } } | 七. 总结:
本文介绍了在用Visual C#进行剪切板编程的二个重要方面的内容,即:判定剪切板中的数据和保存剪切板中的数据。其实针对剪切板的编程用途是比较广的,譬如有了上面的知识做铺垫,我想如果要你用Visual C#开发一个抓图程序,你一定不会说很难吧!
上一页 1 2 3 4 5 | | | 感谢
访问天极网,如果您觉得该文章涉及版权问题,请看这里!
|
|