2011-09-16 37 views
0

关于this post,我想提供将视频文件拖放到Windows媒体控件的可能性,以便它们自动打开。如何拖放到Windows媒体控制

我已经激活AllowDrop属性不起作用。

我读过在wmp控件上使用图像控件允许这样做,但我不知道如何在没有它显示视频控件的情况下如何操作。

谢谢。

回答

1

最好的,清洁的解决方案是包装用户控件内嵌的媒体播放器,并确保媒体播放器AllowDrop属性设置为“false”,并且用户控件AllowDrop属性设置为true。使嵌入式媒体播放器停靠以填充用户控件,然后将其添加到表单中,就像您使用任何用户控件一样。当您在表单中选择用户控件时,您会看到DragEnter和DragDrop事件按预期显示。像处理普通控制一样处理它们(由Cody提供的代码将执行)。你可以在VB中看到一个完整的例子,在下面的链接中(不要忘记确保用户控件中的实际嵌入式媒体播放器的AllowDrop属性设置为false,否则它将“隐藏”拖动事件从用户控件包装):

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

但如果你只是想处理拖放任何地方拖放到形式,包括在媒体播放器控制,所有你需要做的是处理dragenter和的DragDrop嵌入式媒体播放器ActiveX控件的容器事件,并确保实际嵌入式控件的AllowDrop属性设置为False,因为不会隐藏容器中的拖动事件,并且容器的AllowDrop设置为true。


这里去一些代码,以澄清你如何使用容器的拖动事件来弥补不足拖放媒体播放器ActiveX控件的事件。

只需创建一个新的形式,将其命名为MainForm,添加所需的引用WMPLib使Media Player ActiveX控件可用的应用程序,它的大小,使其超过320个像素和高度大于220个像素,并粘贴宽代码波纹管到你的主窗体代码文件:

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

现在,你可以简单地拖动窗体的中心在媒体播放器控制任何媒体文件,它会接受它作为一个放置目标,并开始播放媒体文件。

-1

好的AllowDrop属性应该为MDI窗体或放置视频播放器控件的FORM。 比你能放置一个列表框或者你想什么,永远的标签,并执行以下操作:

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

这将不允许在Media Player ActiveX控件上拖放文件,因为它不会公开DragDrop,AllowDrop,DragEnter和任何“拖动相关”事件/属性。凯尔默问道,在控制上使用透明图像是一种解决方法,但它非常不雅观。我正在研究更好的解决方案,并在完成后发布。 – Loudenvier