一个朋友刚刚帮我解决了这个问题。
禁用上下文菜单相当容易
axWindowsMediaPlayer1.enableContextMenu = false;
禁用双击需要一个消息过滤器——网上已经有解决方案。
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。但就这个问题而言,我认为它已经回答了。