程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> Visual C#的剪切板編程(3)

Visual C#的剪切板編程(3)

編輯:關於C語言

六.用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. 下一頁:
Copyright © 程式師世界 All Rights Reserved