简体   繁体   English

在WebBrowser .NET CF 3.5中禁用上下文菜单

[英]Disable Context Menu in WebBrowser .NET CF 3.5

I'm using a WebBrowser control in .NET CF 3.5 for a Windows CE device application, and for security reasons need to disable the Context Menu. 我在Windows CE设备应用程序的.NET CF 3.5中使用WebBrowser控件,出于安全原因,需要禁用上下文菜单。 I've tried a variety of things, none of which seem to work for mobile devices with .NET CF 3.5: 我已经尝试了多种方法,但似乎都不适用于.NET CF 3.5的移动设备:

  1. I've tried placing a pictureBox over the WebBrowser, and setting it to be transparent. 我尝试过在WebBrowser上放置一个pictureBox,并将其设置为透明。 Unfortunately, the transparency doesn't work and this ends up being a white box over my browser. 不幸的是,透明性不起作用,最终变成了浏览器上的白框。

  2. I've tried implementing a new custom transparent control to place over the web browser, similar to this . 我尝试实现一个新的自定义透明控件来放置在Web浏览器上,类似于this

  3. I've tried editing the OnContextMenu element in the html, no luck. 我试过编辑html中的OnContextMenu元素,但是不走运。

  4. I've tried overriding the CreateParams to make a transparent PictureBox over the browser, noted as one of the solutions here . 我尝试覆盖CreateParams以在浏览器上制作一个透明的PictureBox,这被称为此处的解决方案之一。

There seems to be many solutions for this online, but none of them seem to work for windows CE with .NET CF 3.5. 网上似乎有许多解决方案,但似乎没有一个适用于带有.NET CF 3.5的Windows CE。 I believe this is because the WebBrowser has a much simpler implementation than the full .NET 3.5. 我相信这是因为WebBrowser的实现比完整的.NET 3.5简单得多。 So my question is thus: is there any way to disable the context menu for the WebBrowser control? 因此,我的问题是:有没有办法禁用WebBrowser控件的上下文菜单?

I've not tried it for this specific control, so I don't know if it will work, but have you tried subclassing the browser control and intercepting and discarding the messages that cause the context menu to appear in the first place? 我没有为特定的控件尝试过它,所以我不知道它是否可以工作,但是您是否尝试过子化浏览器控件的类并拦截和丢弃导致上下文菜单首先出现的消息? That's certainly what I'd try first if I had to solve the same problem. 如果必须解决相同的问题,那当然就是我会首先尝试的方法。

It's actually not that hard to use the native HTML Control API (not IWebBrowser2 et al) by P/Invoking it from C#, if you feel that you're not enough in charge of the managed WebBrowser. 如果您觉得自己不足以管理托管WebBrowser,那么通过P /从C#调用本机HTML控件API(不是IWebBrowser2等)实际上并不难。

If you go that way, then you can either 如果走那条路,那么您可以

  • intercept the context menu notification NM_CONTEXTMENU that gets sent to the HTML control host/parent when the context menu is about to be shown, and not let it continue to the default window message handler 截取上下文菜单通知NM_CONTEXTMENU,该通知将在上下文菜单即将显示时发送到HTML控件主机/父级,而不让其继续使用默认的窗口消息处理程序

or 要么

  • completely disable the context menu by sending it a DTM_ENABLECONTEXTMENU with FALSE. 通过向其发送带有FALSE的DTM_ENABLECONTEXTMENU来完全禁用上下文菜单。

Been there, done that, both works. 到那里去了,都完成了。

Edit: 编辑:

There are 3 possible levels of complexity in this case: 在这种情况下,可能存在3种复杂程度:

  1. Unless the HTML control is created with the window style HS_CONTEXTMENU, it will by default have the context menu disabled. 除非使用窗口样式HS_CONTEXTMENU创建HTML控件,否则默认情况下将禁用上下文菜单。 So if all you ever want to do is disable it, then the native CreateWindowEx is the only needed native Win32 API function to P/Invoke. 因此,如果您只想禁用它,则本机CreateWindowEx是P / Invoke唯一需要的本机Win32 API函数。
  2. On the other hand, if you do want to enable and disable the context menu at runtime, you can P/Invoke SendMessage with DTM_ENABLECONTEXTMENU and TRUE/FALSE. 在另一方面,如果想在运行时启用和禁用上下文菜单,你可以P / Invoke的SendMessage与DTM_ENABLECONTEXTMENU和TRUE / FALSE。
  3. And finally, if you want maximum control of the menu or do something completely arbitrary when the HTML control wants to show the menu, you need to P/Invoke SetWindowLong to subclass the parent and listen to the NM_CONTEXTMENU and decide there whether to continue showing the menu or not or do something completely else. 最后,如果要最大程度地控制菜单或在HTML控件要显示菜单时执行完全任意的操作,则需要P / Invoke SetWindowLong来继承父类并听NM_CONTEXTMENU并决定是否继续显示菜单。菜单,还是不做,或者完全做其他事情。

None of these possible answers work. 这些可能的答案均无效。 So I gave up trying to hide the context menu and just added an event handler for the Navigating event and cancel the navigation if it's not the same URL as the one I originally sent it to. 因此,我放弃了尝试隐藏上下文菜单的方法,只是为Navigating事件添加了一个事件处理程序,并取消了导航(如果它与我最初发送给该URL的URL不同)。 It still shows the context menu but clicking on anything will not send it to another page 它仍然显示上下文菜单,但是单击任何内容都不会将其发送到另一个页面

void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) {
    //don't let users go anywhere else
    if (e.Url != webBrowser1.Url) {
        e.Cancel = true;
    }
}

.NET CF中的WebBrowser控件实现不包含属性IsWebBrowserContextMenuEnabled吗?

I just came across this very interesting blog entry while looking for other stuff. 我在寻找其他内容时碰到了这个非常有趣的博客条目 I sure looks like it solves the context menu issue (caveat: I'm not tested this at all). 我肯定看起来像它解决了上下文菜单问题(注意:我完全没有测试过)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM