2

フォームに WPF RichTextBox (ElementHost 内) を含む ac# Windows Forms プロジェクトがあり、エクスプローラー (Windows 7 x64) から画像をドラッグ アンド ドロップしたいのですが、カーソルには許可されていないシンボルしか表示されません。これは私のコードです:

    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;
    }

イベントはデザイナーを使用してサブスクライブされます。それらのどれも解雇されません:

    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 が必要です。

    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");
    }
4

1 に答える 1

2

PreviewDragEnterPreviewDragOverおよびPreviewDropイベントを使用する必要があります。

    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;
        }
    }

次に、次のメソッドは、現在の選択範囲に画像を貼り付けます。

    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必要がありません。これが行われているのを時々見かけますが、理想的ではありません。

現在の選択範囲に貼り付ける代わりに、現在のドロップ位置に画像をドロップしたい場合があります。その場合は、次から始めてください: ドラッグ アンド ドロップ中にマウスの位置を取得し、次のようにします:実行時にテキストの間に画像を WPF RichTextBox に挿入して、テキストが画像の周りに浮かぶようにする方法

于 2014-09-22T23:52:49.163 に答える