科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件Visual C#的剪切板编程

Visual C#的剪切板编程

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文将结合二个具体的程序例子来介绍Visual C#剪切板编程的二个重要方面:判定剪切板中的数据类型、保存剪切板中的数据

作者:阿虎 来源:Yesky 2007年11月14日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
六. 用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#开发一个抓图程序,你一定不会说很难吧!

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章