在Rich文本框中拖动图像 [英] Draging Images in Rich text box

查看:77
本文介绍了在Rich文本框中拖动图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Windows应用程序,其中包含富文本框.在该应用程序中,我必须放置图像.我写了代码将图像放置在富文本框内.它工作正常.但是我想将富文本框内的图像从一个地方拖到富文本框内的另一个位置.我不知道该怎么做.在Internet上,我也找不到任何解决方案.因此,请Plz帮助我.

I am developing an windows Application containing rich text box.In that i have to place images. I wrote code to place images inside rich text box.its working fine.But i want to drag an Image inside rich text box from one place to other in rich text box.I don''t no how to do it. In internet also, i did not find any solution.So, Plz help me.

推荐答案

据我所知,RichTextBox不支持此类操作.类(而且我不知道您确切地指的是什么类;请参阅我对问题的评论:您应该始终准确地指定它).因此,我唯一能看到的就是使用剪贴板.

DragDrop事件处理程序中,您应该执行以下操作:
To best of my knowledge, such operation is not supported by the RichTextBox classes (and I don''t know what class do you mean exactly; please see my comment to the question: you should specify it exactly — always). So, the only way I can see is using clipboard.

In your DragDrop event handler you should do something like this:
myRichTextBox.DragDrop += (sender, eventArgs) => {
    RichTextBox richTextBox = (RichTextBox)sender;
    Image image = eventArgs.??? // depends on UI library
    //in WPF the declaration above would be BitmapSource image...
    Clipboard.SetImage(image);
    richTextBox.Paste();
} //myRichTextBox.DragDrop



同样,这取决于您使用的UI库(始终对其进行标记!),但是您应该得到图片. :-)

抱歉,上面的代码不是您需要的确切代码. 这是因为您没有指定RichTextBox和UI库的确切类.请基于此想法,请访问MSDN帮助并找到要使用的确切类型和方法.使用不同的.NET UI库,它们有些相似但有所不同.



我最近找到了在System.Windows.Forms.RichTextBox中插入图像的解决方案.它基于剪贴板.这不是很好的支持,因此我也提供了一个有趣的替代方法,我测试并发现它很棒.请查看我最近的答案:

如何使用以下方法打开Word文件富文本框中的图片和项目符号 [



Again, it depends on what UI library you use (tag it — always!), but you should get the picture. :-)

Sorry the code above is not exact code you will need. This is because you did not specify exact class of RichTextBox and UI library. Please, based on this idea, go to MSDN help and find exact types and methods to use. With different .NET UI libraries, they are somewhat similar but different.



I recently found the solution for inserting images in System.Windows.Forms.RichTextBox; it is based on Clipboard. This is not a very good support, so I also offered an interesting alternative I tested and found great. Please see my recent answer:

how to open word file with images and bullets in a rich text box[^].

—SA


Try this code in constructor:

richTextBox.AddHandler(RichTextBox.DragOverEvent, new DragEventHandler(RichTextBox_DragOver), true);

richTextBox.AddHandler(RichTextBox.DropEvent, new DragEventHandler(RichTextBox_Drop), true);


还要在RichTextBox_DragOver


Also set e.handled = false inside RichTextBox_DragOver


这篇关于在Rich文本框中拖动图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆