简体   繁体   English

使用C#进行Windows 7剪贴板复制粘贴操作

[英]Windows 7 clipboard copy paste operation using C#

I'm working on a Windows application where I need to use clipboard data. 我正在开发一个需要使用剪贴板数据的Windows应用程序。 I am trying to copy text from clipboard by the code below. 我试图通过下面的代码从剪贴板复制文本。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace MultiValuedClipBoard
{
    class Class1
    {

        public String SwapClipboardHtmlText(String replacementHtmlText)
        {
            String returnHtmlText = "hello";
            if (Clipboard.ContainsText(TextDataFormat.Html))
            {
                returnHtmlText = Clipboard.GetText(TextDataFormat.Html);
                Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);
            }
            return returnHtmlText;
        }
    }
}

Calling the above function by: 通过以下方式调用上述功能:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace MultiValuedClipBoard
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 aas = new Class1();
            string a = aas.SwapClipboardHtmlText("chetan");
            Console.WriteLine(a);
            Console.ReadLine();
        }
    }
}

When running this code it gives the output "Hello" which is the default value, not clipboard data. 运行此代码时,它会输出“Hello”,这是默认值,而不是剪贴板数据。

Your code will not work because of two reasons: 您的代码无法正常工作,原因有两个:

[1] When you say: [1]当你说:

if (Clipboard.ContainsText(TextDataFormat.Html))

Here you are basically assuming that the clipboard already contains a text and that too in HTML format, but depending on the values you are setting in the clipboard it doesn't look like you are intending to use the pre-existing clipboard value anywhere in your program. 在这里,您基本上假设剪贴板已包含文本,并且也是HTML格式,但根据您在剪贴板中设置的值,您似乎不打算在您的任何位置使用预先存在的剪贴板值程序。 So, this if condition should not be there. 所以,这个if条件不应该存在。

[2] Secondly, you are further trying to set the string "chetan" to the clipboard which is definitely not in HTML format. [2]其次,您还在尝试将字符串“chetan”设置为剪贴板,而剪贴板绝对不是HTML格式。 So, 所以,

            Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);

becomes

            Clipboard.SetText(replacementHtmlText, TextDataFormat.Text);

Hence, effectively, your new code becomes something like this: 因此,实际上,您的新代码会变成这样:

        String returnHtmlText = "hello";
        //if (Clipboard.ContainsText(TextDataFormat.Html))
        //{
            returnHtmlText = Clipboard.GetText(TextDataFormat.Text);
            Clipboard.SetText(replacementHtmlText, TextDataFormat.Text);
        //}
        return returnHtmlText;

Clearly Clipboard.ContainsText(TextDataFormat.Html) evaluates to false . 显然, Clipboard.ContainsText(TextDataFormat.Html)计算结果为false Which means that the clipboard in fact does not contain text in the format you specify. 这意味着剪贴板实际上不包含您指定格式的文本。

I changed your program to prove the point: 我改变了你的程序来证明这一点:

[STAThread]
static void Main(string[] args)
{
    Clipboard.SetText("boo yah!", TextDataFormat.Html);
    Class1 aas = new Class1();
    string a = aas.SwapClipboardHtmlText("chetan");
    Console.WriteLine(a);
    Console.WriteLine(Clipboard.GetText(TextDataFormat.Html));
    Console.ReadLine();
}

Output: 输出:

boo yah!
chetan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM