如何阻止AxWindowsMediaPlayer接受C#中的任何用户命令

时间:2012-01-06 12:56:58

标签: c# events activex axwindowsmediaplayer

在我的democode中,嵌入式Windows媒体播放器开始加载并播放视频。播放器不显示任何控件,所有其他选项都是默认选项。到目前为止,这是有效的。

不起作用的是并非所有用户交互都被停止。例如,可以通过双击将模式更改为全屏,也可以通过右键单击获得完整的上下文。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        axWindowsMediaPlayer1.uiMode = "none";
    }

    private void button1_Click(object sender, EventArgs e)
    {
        axWindowsMediaPlayer1.URL = @"C:\stackoverflow.mp4";
    }
}

如何将播放器与用户隔离并仅通过代码控制播放器?

3 个答案:

答案 0 :(得分:2)

一位朋友帮我解决了这个问题。

禁用上下文菜单相当容易

axWindowsMediaPlayer1.enableContextMenu = false;

禁用双击需要消息过滤器 - there is already a solution on the web

Application.AddMessageFilter((IMessageFilter)CustomFilter(this/*Form*/, axWMP));

我已经改写了我的例子,现在我正在使用这段代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        axWindowsMediaPlayer1.uiMode = "none";
        axWindowsMediaPlayer1.enableContextMenu = false;
        Application.AddMessageFilter(new IgnoreMouseClickMessageFilter(this, axWindowsMediaPlayer1));
    }

    private void button1_Click(object sender, EventArgs e)
    {
        axWindowsMediaPlayer1.URL = @"C:\stackoverflow.mp4";
    }
}

class IgnoreMouseClickMessageFilter : IMessageFilter
    {
        private Control parent { get; set; }
        private Control target { get; set; }

        public IgnoreMouseClickMessageFilter(Control parent, Control target)
        {
            this.parent = parent;
            this.target = target;
        }

        public bool PreFilterMessage(ref Message messageBeforeFiltering)
        {
            const Boolean FilterTheMessageOut = true;
            const Boolean LetTheMessageThrough = false;

            if (IsNull(parent)) return LetTheMessageThrough;
            if (IsNull(target)) return LetTheMessageThrough;
            if (WasNotClickedOnTarget(parent, target)) return LetTheMessageThrough;
            if (MessageContainsAnyMousebutton(messageBeforeFiltering)) return FilterTheMessageOut;
            return LetTheMessageThrough;
        }

        private bool MessageContainsAnyMousebutton(Message message)
        {
            if (message.Msg == 0x202) return true; /* WM_LBUTTONUP*/
            if (message.Msg == 0x203) return true; /* WM_LBUTTONDBLCLK*/
            if (message.Msg == 0x204) return true; /* WM_RBUTTONDOWN */
            if (message.Msg == 0x205) return true; /* WM_RBUTTONUP */
            return false;
        }

        private bool WasNotClickedOnTarget(Control parent, Control target)
        {
            Control clickedOn = parent.GetChildAtPoint(Cursor.Position);
            if (IsNull(clickedOn)) return true;
            if (AreEqual(clickedOn, target)) return false;
            return true;
        }

        private bool AreEqual(Control controlA, Control controlB)
        {
            if (controlA == controlB) return true;
            return false;
        }

        private bool IsNull(Control control)
        {
            if (control == null) return true;
            return false;
        }
    }

特别感谢我的未命名朋友和Microsoft Developer Network Frorums的“remarkpk11”。

代码有一些小问题 - 我不喜欢消息首先隐藏在我身上,我也很想摆脱两个全局依赖的Cursor和Application。但就这个问题而言,我认为它得到了回答。

答案 1 :(得分:2)

尝试axWindowsMediaPlayer1.Ctlenabled = False

编辑:对不起,这是为了vb ..

答案 2 :(得分:1)

axWindowsMediaPlayer1.Ctlcontrols.stop();