1

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?

John Leehey
  • 22,052
  • 8
  • 61
  • 88

5 Answers5

1

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.

ctacke
  • 66,480
  • 18
  • 94
  • 155
  • woo that looks like a doozy (I don't have much windows development experience). I'll give it a shot though! – John Leehey Apr 11 '11 at 22:19
  • So I've subclassed the WebBrowser going off Microsoft's guide here: http://msdn.microsoft.com/en-us/library/ms229669.aspx I've tried both unhooking the WM_NOTIFY and WM_CONTEXTMENU messages, none of which seem to work. Once I get into the new webbrowser, nothing's changed. It seems that the message isn't being intercepted correctly, because the handler never gets called in debugging. Is it possible the signed-int values of the notifications are different? I have 0x4E for WM_NOTIFY and 0x007B for WM_CONTEXTMENU. – John Leehey Apr 14 '11 at 19:09
  • So by subclassing, I found out that there was no way to actually intercept and discard the context menu popup. My solution was to hook into the WM_CONTEXTMENU message (in a similar way as microsoft's TreeView message hooking in the link above), and pop up a message box as soon as the event passed. The MessageBox would distract from the context menu before the user was able to place any focus or click on it, providing me with an acceptable solution. Thanks! – John Leehey Apr 15 '11 at 18:33
1

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.
Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
  • Do you know any good examples for doing this online? I've googled, but I'm not completely sure what P/Invoking entails. I don't really now how I would use the native HTML Control API. Thanks. – John Leehey Apr 14 '11 at 19:11
  • @John: Do you mean examples of P/Invoking in general or doing it in this specific case? For the general case, I think http://msdn.microsoft.com/en-us/library/aa446536.aspx is an ok intro. In this specific case you would need to P/Invoke, off the top of my head, just the native Win32 API function CreateWindowEx (for creating the HTML control with a managed window of your choice as parent) in the simplest scenario. – Johann Gerell Apr 14 '11 at 20:53
  • @John: I realized that you might need more guidance on the native parts of this, so I added a section, see **Edit:** – Johann Gerell Apr 14 '11 at 21:02
  • Thanks for your response! I was fortunately able to construct a workaround without using P/Invoke. I'll definitely use this information though, I need to research using P/Invoke to disable the SIP Options menu (on an unrelated issue). Thanks again! – John Leehey Apr 15 '11 at 18:31
1

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;
    }
}
T.Rob
  • 31,522
  • 9
  • 59
  • 103
Jason
  • 11
  • 1
  • If you read my comment on the accepted answer, you'll see you're right, none of these answers worked for me, but I was able to find an acceptable alternate solution from one of the answers. Your solution still allows the context menu, which would leave the security hole I was worried about open. Thanks for the input though! – John Leehey Nov 04 '11 at 23:53
0

Does WebBrowser control implementation in .NET CF not contain property IsWebBrowserContextMenuEnabled?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
0

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

ctacke
  • 66,480
  • 18
  • 94
  • 155