开发者

How to drag & drop into Windows Media Control

In relation to this post, I would like to offer the possibility to drag and drop video files int开发者_运维百科o the windows media control, so they open automatically.

I have activated the AllowDrop property to no effect.

I've read that using an image control over the wmp control allows this, but I don't know how to do it without it displaying over the video control.

Thanks.


The best, cleaner solution is to wrap the embedded media player inside a User Control and make sure the media player AllowDrop property is set to "false" and the user control AllowDrop property is set to true. Make the embedded media player to dock to fill the user control then add it to your form as you would any user control. When you select the user control in your form you'll see that the DragEnter and DragDrop events are exposed as expected. Handle them as you would do for an ordinary control (the code provided by Cody will do). You can see a complete example, in VB, in the following link (just don't forget to make sure the actual embedded media player inside the user control has its AllowDrop property set to false, or else it will "hide" the drag events from the user control wrapper):

http://www.code-magazine.com/article.aspx?quickid=0803041&page=5

But if you just want to handle drag and drop anywhere over the Form, including over the media player control, all you need to do is to handle the DragEnter and DragDrop events of the container of the embedded media player ActiveX control and make sure the AllowDrop property of the actual embedded control is set to False as not to hide the drag events from the container, and the AllowDrop of the container is set to true.


Here goes some code to clarify how you can use a container's drag events to make up for the lack of drag and drop events of the media player ActiveX control.

Just create a new form, name it MainForm, add the needed references to WMPLib to make the Media Player ActiveX control available to the application, size it so that it is wider than 320 pixels and taller than 220 pixels, and paste the code bellow into your main form code file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using WMPLib;
using AxWMPLib;

namespace YourApplicationNamespace
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            // 40 is the height of the control bar... 320 x 180 is 16:9 aspect ratio
            Panel container = new Panel()
            {
                Parent = this,
                Width = 320,
                Height = 180 + 40,
                AllowDrop = true,
                Left = (this.Width - 320) / 2,
                Top = (this.Height - 180 - 40) / 2,
            };
            AxWindowsMediaPlayer player = new AxWindowsMediaPlayer()
            {
                AllowDrop = false,
                Dock = DockStyle.Fill,
            };
            container.Controls.Add(player);
            container.DragEnter += (s, e) =>
            {
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                    e.Effect = DragDropEffects.Copy;
            };
            container.DragDrop += (s, e) =>
            {
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                    var file = files.FirstOrDefault();
                    if (!string.IsNullOrWhiteSpace(file))
                        player.URL = file;
                }
            };
        }
    }
}  

Now you can simply drag any media file over the media player control on the center of the form and it will accept it as a drop target and start playing the media file.


Ok AllowDrop property should be true into the MDI Form ,or the FORM where you placed the Video Player Control . THan you could place a ListBox or a Label what-ever you want to ,and perform these actions :

private void filesListBox_DragEnter(object sender, DragEventArgs e)  
{  
    if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)  
    {  
        e.Effect = DragDropEffects.All;  
    }  
}  

private void filesListBox_DragDrop(object sender, DragEventArgs e)  
{  
    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);  

    foreach (string file in files)  
    {  
        //add To Media PLayer
        //Play the files 
    }  
    //Or Handle the first file in string[] and play that file imediatly

} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜