简体   繁体   English

如何将文字与剪贴板中的图像(如Microsoft Word)一起粘贴到C#中?

[英]How to Paste text with images from clipboard like Microsoft Word do C#?

i was wondering how can i paste the content of my clipboard like Microsoft word do, i mean for example if i want to copy something like: 我想知道如何像Microsoft word一样粘贴剪贴板中的内容,例如,如果我要复制以下内容:

Some text 
*some image*
More text

and paste it just like it was when i copied with the text and the image between the text, how can i do that? 并粘贴它,就像我复制文本和文本之间的图像时一样,我该怎么做?

I tried with Rich Textbox pasting with HTML Format but still got nothing... So far i can only paste the text without format or the HTML text with the tags also without format... 我尝试使用HTML格式的Rich Textbox粘贴,但仍然一无所获...到目前为止,我只能粘贴没有格式的文本或带有标签也没有格式的HTML文本...

By the way, is there some way to override the Ctrl + V paste method of a textbox? 顺便说一句,有什么方法可以覆盖文本框的Ctrl + V粘贴方法?

Thank you 谢谢

EDIT: I'm working on WinForms 编辑:我正在使用WinForms

As to the first question, I don't believe you can do that with the RichTextBox. 关于第一个问题,我不相信您可以使用RichTextBox来实现。 You can paste an image by itself, or you can paste just the text if you copied text and images from a website. 您可以单独粘贴图像,或者如果从网站复制了文本和图像,则可以仅粘贴文本。 But it won't paste both, unfortunately. 不幸的是,它不会同时粘贴两者。

Here's a previous SO post where some suggestions were thrown out. 这是之前的SO帖子 ,其中提出了一些建议。


As for the other question, if you want to override CTRL + V functionality, you'll have to subscribe to the KeyDown and KeyPress events: 至于另一个问题,如果要覆盖CTRL + V功能,则必须订阅KeyDownKeyPress事件:

 private bool IsPressedCtrlV = false;

 private void textBox1_KeyDown(object sender, KeyEventArgs e)
 {
     // You have access to modifiers in here, so you can detect the Control key
     IsPressedCtrlV = (e.Modifiers == Keys.Control && e.KeyCode == Keys.V);
 }

 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
 {
     if (IsPressedCtrlV)
     {
         // When the key "press" is complete, handle ctrl-v however you want
         e.Handled = true;
         MessageBox.Show("No pasting allowed!");
     }
 }

If you can see the image displayed in RichTextBox via copy paste, then you can do the same from code as follow : 如果您可以通过复制粘贴看到RichTextBox中显示的图像,则可以通过以下代码执行以下操作:

myRichTextBox.Rtf = Clipboard.GetText(TextDataFormat.Rtf);

simple step, set RichTextBox's Rtf property to value you get from Clipboard in Rtf format. 简单的步骤,将RichTextBox的Rtf属性设置为您从剪贴板以Rtf格式获得的值。

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

相关问题 C#如何将剪贴板中的格式化文本粘贴到RichTextBox - C# How can I paste formatted text from clipboard to the RichTextBox 如何在C#中一次从剪贴板粘贴一行? - How do I Paste one line from the clipboard at a time in C#? 如何将图像从剪贴板粘贴到Word - How to paste image from clipboard to Word 从ToolStripTextBox(C#)中的剪贴板检测粘贴 - Detecting paste from clipboard in ToolStripTextBox (C#) 使用C#Word API从Word文件读取图像而不使用剪贴板 - Reading images from Word files using C# Word API without using Clipboard 如何在不使用剪贴板的情况下在C#中保存Excel中的图像? - How do I save images from Excel without using Clipboard, in C#? 如何在C#中使用Microsoft.Office.Interop.Word从Word文件(.Docx)中按页获取文本 - How to get text by page from word file (.Docx) using Microsoft.Office.Interop.Word in C# 如何使用Microsoft.Office.Interop.Word在C#中从Word文件中查找突出显示的文本? - How to find highlighted text from Word file in C# using Microsoft.Office.Interop.Word? 关闭后,Microsoft Word调用C#剪贴板更改事件 - Microsoft word call C# Clipboard change event after closing 从剪贴板在C#Windows文本框中以相同格式复制粘贴 - Copy paste from clipboard in c# windows text box in the same format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM