[英]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功能,则必须订阅
KeyDown
和KeyPress
事件:
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.