雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

C#編程實現(xiàn)查看剪切板內容的方法

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊:

本文實例講述了C#編程實現(xiàn)查看剪切板內容的方法。分享給大家供大家參考,具體如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication49
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
      radioButton1.Checked = true;
      pictureBox1.Visible = false;
      textBox1.Visible = true;
    }
    //“查看”按鈕
    private void button1_Click(object sender, EventArgs e)
    {
      IDataObject data;//為傳送數(shù)據(jù)提供與格式無關的接口
      string format = FormatString();
      if (format == "Bitmap")
      {
        textBox1.Visible = false;
        pictureBox1.Visible = true;
        data = Clipboard.GetDataObject();//檢索位于當前系統(tǒng)剪切板的數(shù)據(jù)
        if (data.GetDataPresent(format))//確定此實例中存儲的數(shù)據(jù)是否與指定的格式關聯(lián),返回布爾
        {
          pictureBox1.Image = (Bitmap)data.GetData(format);//檢索與指定的格式關聯(lián)的數(shù)據(jù)
          pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        }
        else
        {
          MessageBox.Show("格式不正確", "提示");
        }
      }
      else
      {
        textBox1.Visible = true;
        pictureBox1.Visible = false;
        data = Clipboard.GetDataObject();
        if (data.GetDataPresent(format))
        {
          textBox1.Text = (string)data.GetData(format);
        }
        else
        {
          MessageBox.Show("格式不正確", "提示");
        }
      }
    }
    private string FormatString()
    {
      string format = "";
      if (radioButton1.Checked) format = DataFormats.Text;//得到IDATAOBJECT里面數(shù)據(jù)的格式
      if (radioButton2.Checked) format = DataFormats.Rtf;
      if (radioButton3.Checked) format = DataFormats.Bitmap;
      if (radioButton4.Checked) format = DataFormats.Html;
      return format;
    }
  }
}

運行程序后,如果之前復制過BITMAP圖,則TEXTBOX消失,PICTRUEBOX出現(xiàn),并且顯示該圖;反之如果選擇的是后三項,則TEXTBOX出現(xiàn),并且顯示復制過的值。效果圖如下:

這里有一個問題,如果把文字與圖片一起復制的話,就不能顯示了。感興趣的朋友可以加以完善。

希望本文所述對大家C#程序設計有所幫助。

上一篇:C#數(shù)據(jù)結構之雙向鏈表(DbLinkList)實例詳解

欄    目:C#教程

下一篇:輕松學習C#的方法

本文標題:C#編程實現(xiàn)查看剪切板內容的方法

本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6803.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有