简体   繁体   中英

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. I've tried a variety of things, none of which seem to work for mobile devices with .NET CF 3.5:

  1. I've tried placing a pictureBox over the WebBrowser, and setting it to be transparent. 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 .

  3. I've tried editing the OnContextMenu element in the html, no luck.

  4. I've tried overriding the CreateParams to make a transparent PictureBox over the browser, noted as one of the solutions here .

There seems to be many solutions for this online, but none of them seem to work for windows CE with .NET CF 3.5. I believe this is because the WebBrowser has a much simpler implementation than the full .NET 3.5. So my question is thus: is there any way to disable the context menu for the WebBrowser control?

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.

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

or

  • completely disable the context menu by sending it a DTM_ENABLECONTEXTMENU with FALSE.

Been there, done that, both works.

Edit:

There are 3 possible levels of complexity in this case:

  1. Unless the HTML control is created with the window style HS_CONTEXTMENU, it will by default have the context menu disabled. 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.
  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.
  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.

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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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