首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C#窗口窗体:将图片放到WPF RichTextBox中(在ElementHost中)

C#窗口窗体:将图片放到WPF RichTextBox中(在ElementHost中)
EN

Stack Overflow用户
提问于 2014-09-21 19:54:17
回答 1查看 1.3K关注 0票数 2

我有一个c# Windows项目,其窗体上有一个WPF RichTextBox (在ElementHost中),并且希望从资源管理器(Windows7 x64)拖放一张图片到其中,但是光标只显示不允许的符号。这是我的密码:

代码语言:javascript
运行
复制
    private void Form1_Load(object sender, EventArgs e)
    {
        this.AllowDrop = true;
        elementHost1.AllowDrop = true;
    }

    public UserControl1()
    {
        InitializeComponent();
        Background = System.Windows.Media.Brushes.Transparent;
        this.AllowDrop = true;
        richTextBox1.AllowDrop = true;
    }

事件是使用设计器订阅的。他们中没有人被解雇:

代码语言:javascript
运行
复制
    private void richTextBox1_DragEnter(object sender, DragEventArgs e)
    {
        MessageBox.Show("Test");
    }

    private void richTextBox1_DragLeave(object sender, DragEventArgs e)
    {
        MessageBox.Show("Test");
    }

    private void richTextBox1_DragOver(object sender, DragEventArgs e)
    {
        MessageBox.Show("Test");
    }

    private void richTextBox1_Drop(object sender, DragEventArgs e)
    {
        MessageBox.Show("Test");
    }

如果我使用Windows,RichTextBox是工作的,但我需要一个WPF RichTextBox:

代码语言:javascript
运行
复制
    private void Form1_Load(object sender, EventArgs e)
    {
        richTextBox1.AllowDrop = true;
        richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop);
    }

    private void richTextBox1_DragDrop(object sender, EventArgs e)
    {
        MessageBox.Show("Test");
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-22 23:52:49

您需要使用PreviewDragEnterPreviewDragOverPreviewDrop事件:

代码语言:javascript
运行
复制
    public Window1()
    {
        InitializeComponent();

        // mainRTB is the name of my RichTextBox.

        mainRTB.PreviewDragEnter += new DragEventHandler(mainRTB_PreviewDragEnter);

        mainRTB.PreviewDragOver += new DragEventHandler(mainRTB_PreviewDragEnter);

        mainRTB.PreviewDrop += new DragEventHandler(mainRTB_PreviewDrop);

        mainRTB.AllowDrop = true;
    }

    static bool IsImageFile(string fileName)
    {
        return true;  // REPLACE THIS STUB WITH A REAL METHOD.
    }

    void mainRTB_PreviewDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            // Note that you can have more than one file.
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            if (files != null && files.Length > 0)
            {
                // Filter out non-image files
                if (mainRTB.Document.PasteImageFiles(mainRTB.Selection, files.Where(IsImageFile)))
                    e.Handled = true;
            }
        }
    }

    void mainRTB_PreviewDragEnter(object sender, DragEventArgs e)
    {
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
        // Filter out non-image files
        if (files != null && files.Length > 0 && files.Where(IsImageFile).Any())
        {
            // Consider using DragEventArgs.GetPosition() to reposition the caret.
            e.Handled = true;
        }
    }

然后,以下方法将图像粘贴到当前选择范围内:

代码语言:javascript
运行
复制
    public static bool PasteImageFiles(this FlowDocument doc, TextRange selection, IEnumerable<string> files)
    {
        // Assuming you have one file that you care about, pass it off to whatever
        // handling code you have defined.
        FlowDocument tempDoc = new FlowDocument();
        Paragraph par = new Paragraph();
        tempDoc.Blocks.Add(par);

        foreach (var file in files)
        {
            try
            {
                BitmapImage bitmap = new BitmapImage(new Uri(file));
                Image image = new Image();
                image.Source = bitmap;
                image.Stretch = Stretch.None;

                InlineUIContainer container = new InlineUIContainer(image);
                par.Inlines.Add(container);
            }
            catch (Exception)
            {
                Debug.WriteLine("\"file\" was not an image");
            }
        }

        if (par.Inlines.Count < 1)
            return false;

        try
        {
            var imageRange = new TextRange(par.Inlines.FirstInline.ContentStart, par.Inlines.LastInline.ContentEnd);
            using (var ms = new MemoryStream())
            {
                string format = DataFormats.XamlPackage;

                imageRange.Save(ms, format, true);
                ms.Seek(0, SeekOrigin.Begin);
                selection.Load(ms, format);

                return true;
            }
        }
        catch (Exception)
        {
            Debug.WriteLine("Not an image");
            return false;
        }
    }
}

顺便说一句,这种方法避免使用剪贴板将图像粘贴到RichTextBox中--人们有时会看到这一点,但这并不理想。

与其粘贴当前所选内容,不如将图像拖放到当前拖放位置。如果是这样,请从以下内容开始:Get the mouse position during drag and drop和this:How can I insert an image into a WPF RichTextBox at runtime in between text so the text floats around the image

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25963244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档