您现在的位置是: 软件 > 开发者网络 > 微软开发专栏 > Visual Studio.net专栏 > C#专区 > 正文


-Win xp中的多种网络
-试验试验试验试验
-用Freehand实现位图矢量化
-网络电话面面观

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  

【责任编辑:方舟】
【发表评论】【关闭窗口】
■ 相关内容
 C#的多线程机制初探
 让窗体飘动起来--C#中Timer组件用法
 微软Visual Studio .NET
 用Visual C#动态生成组件
 .Net平台Windows Forms程序设计简介
 用Visual C#做托盘程序
 用C#实现基于TCP协议的网络通讯
 在C#应用程序中控制输入法
感谢 访问天极网,如果您觉得该文章涉及版权问题,请看这里!