简体   繁体   English

如何使用Windows剪贴板和C#复制/粘贴特殊字符

[英]How to copy/paste special characters with Windows Clipboard and C#

I found a similar question in this post , but my problem is different, so worthy of a new post. 我在这篇文章中发现了类似的问题,但我的问题有所不同,因此值得发表新文章。

Basically, I have a WPF/C# application that generates a text output. 基本上,我有一个WPF / C#应用程序,可以生成文本输出。 The text output includes several instances of a special '℗' character. 文本输出包括一个特殊的“℗”字符的多个实例。 The user can click a button to be sent the information in an e-mail and this part works fine. 用户可以单击一个按钮以通过电子邮件发送信息,此部分可以正常工作。 The user can click another button in the application to copy the same text output into the clipboard and this is where I have a problem. 用户可以在应用程序中单击另一个按钮,以将相同的文本输出复制到剪贴板,这是我遇到的问题。 I am using the following code to copy/paste the text. 我正在使用以下代码复制/粘贴文本。

public void SetClipboardText(string text)
{
    try { Clipboard.SetData(DataFormats.Text, text); }
    catch (COMException)
    {
        MessageBox.Show("The clipboard was inaccessible, please try again later");
    }
}

public string GetClipboardText()
{
    if (Clipboard.ContainsData("Text"))
    {
        try { return Clipboard.GetData(DataFormats.Text) as string; }
        catch (COMException)
        {
            MessageBox.Show("The clipboard was inaccessible, please try again later");
        }
    }
    return string.Empty;
}

When the copied text is pasted into any external text-editing application, the '℗' character is replaced with a '?' 当复制的文本粘贴到任何外部文本编辑应用程序中时,“℗”字符将替换为“?” character. 字符。 After searching the internet, I found another article relating to a similar problem regarding encoding HTML before copying to the clipboard. 在互联网上搜索后,我发现了另一篇文章,该文章涉及在复制到剪贴板之前对HTML进行编码的类似问题。 However, after adding the following line of code as the first line in the SetClipboardText method, the pasted '℗' character is now replaced with 'â„—' in the output. 但是,将以下代码行添加为SetClipboardText方法的第一行之后,现在在输出中将粘贴的“℗”字符替换为“â”。

text = Encoding.GetEncoding(0).GetString(Encoding.UTF8.GetBytes(text));

This tells me that this is an encoding issue. 这告诉我这是一个编码问题。 So my question really is, 'which character encoding can I use to successfully copy AND paste a '℗' character?'. 所以我的问题确实是,“我可以使用哪种字符编码成功复制并粘贴'℗'字符?”。 As you can see from all the '℗' characters in this post, it is indeed possible to copy and paste them using the clipboard... but how? 从这篇文章中所有的'℗'字符中可以看出,确实可以使用剪贴板进行复制和粘贴...但是如何呢?

Additional information: 附加信息:

The '℗' character comes from the Arial font included in all Windows PCs and can also be found in the 'normal text' Font in Word. ℗字符来自所有Windows PC随附的Arial字体,也可以在Word的“普通文本”字体中找到。 After typing (int)'℗' in the Visual Studio QuickWatch Window, I get the relating integer value of 8471 for the character. 在Visual Studio快速监视窗口中键入(int)'℗'后,我得到了该字符的相关整数值8471。

Solution Update: 解决方案更新:

Thanks to @Ilya Ivanov, I have changed the copy/paste code to the following, which now copies and pastes special characters successfully. 感谢@Ilya Ivanov,我将复制/粘贴代码更改为以下代码,该代码现在可以成功复制和粘贴特殊字符。

public void SetClipboardText(string text)
{
    try { Clipboard.SetText(text); }
    catch (COMException)
    {
        MessageBox.Show("The clipboard was inaccessible, please try again later");
    }
}

public string GetClipboardText()
{
    if (Clipboard.ContainsText())
    {
        try { return Clipboard.GetText(); }
        catch (COMException)
        {
            MessageBox.Show("The clipboard was inaccessible, please try again later");
        }
    }
    return string.Empty;
}

Try to use Clipboard.SetText("℗", TextDataFormat.UnicodeText); 尝试使用Clipboard.SetText("℗", TextDataFormat.UnicodeText); instead of Clipboard.SetData . 而不是Clipboard.SetData It works with special symbols like 它适用于特殊符号,例如

I've executed this code in LinqPad and the pasted text from clipboard into text editor - worked fine. 我已经在LinqPad执行了此代码, LinqPad剪贴板中的文本粘贴到了文本编辑器中-工作正常。

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

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