• 0

[C# .NET 2.0] WebBrowser Control Focus Issues


Question

I'm using VS 2005 and I'd really like to use the System.Windows.Forms.WebBrowser control. The problem is that I'm having some focus issues it would appear when using a Java applet in the web page that is loaded in the control.

When using the Sun Java Virtual Machine, right clicking on the applet will cause the applet to no longer receive keyboard input. But if I use the MS Java Virtual Machine, then this does not occur. But if I use the Internet Explorer application (not the embedded IE) with Sun Java, the problem does not occur. Also if I used the COM version of the control in VS 2005 instead of System.Windows.Forms.WebBrowser and Sun Java, the problem will still not occur.

So my only conclusion is that there is a problem somewhere in the implementation of the System.Windows.Forms.WebBrowser control. I really would rather use System.Windows.Forms.WebBrowser because of some of its features. Implementing the ability to disable the context menu for instance is quite a bit more difficult if I use the COM version (AxWebBrowser). So I was hoping there was a way to fix this by making a new control that inherits from System.Windows.Forms.WebBrowser and then overrides some method or something.

I have made some progress on that already. After looking at the code for System.Windows.Forms.WebBrowser (with .NET Reflector), I was able to fix the problem half-way. What I mean is, I overrode WndProc and called the default WndProc instead of the WndProc of System.Windows.Forms.WebBrowser when the WM_MOUSEACTIVATE message was received. I was able to right click on the applet and focus was not lost using Sun Java.

However, there is still one remaining problem. Whenever I minimize the main form that the web browser control is on and then bring it back up, right clicking on the applet fails to give it keyboard focus. (Left clicking does bring focus back to the applet.) This also occurs if I bring up another window in front of my app window and click on my app window again. So basically, when my app window loses focus and regains it, I'm unable to give the applet focus by right clicking it. Once again this is something that does not occur in AxWebBrowser (COM).

So that is my problem. Does anyone have any suggestions as to what I might be able to do next? I have attached a project that demonstrates the problem. Just compile and run the app. Make sure you have Sun Java installed as the web page that the app goes to specifically asks for Sun in this case. (There's another page that just uses whatever your default JVM happens to be, but that's beside the point.) When the applet is finished loading, click on the button that says "Existing User" and this is where you can easily see the keyboard focus issues. There's no need to actually login to see the problem in action.

Thanks in advance.

WindowsApplication2.zip

4 answers to this question

Recommended Posts

  • 0

Cannot duplicate. Works on my machine just fine.

Well the attached project prevents focus being lost when right clicking on the applet. However, are you certain you tried minimizing the program, bringing it back up, and then right clicking the applet? Right clicking on the applet in Internet Explorer gives me keyboard focus, but in my program it does not.

(Also, another really stupid question. Are you certain you are using the Sun JVM? It doesn't seem like it can be reproduced using Microsoft's. Using the Spy++ tool if you try to find a window and put your cursor over the applet should say that the classname is SunAwtCanvas)

Sorry for the seemingly dumb questions, but since my original post was kinda long, some of the details might have been missed.

Edited by Damaged Soul
  • 0

Well the attached project prevents focus being lost when right clicking on the applet. However, are you certain you tried minimizing the program, bringing it back up, and then right clicking the applet? Right clicking on the applet in Internet Explorer gives me keyboard focus, but in my program it does not.

(Also, another really stupid question. Are you certain you are using the Sun JVM? It doesn't seem like it can be reproduced using Microsoft's. Using the Spy++ tool if you try to find a window and put your cursor over the applet should say that the classname is SunAwtCanvas)

Sorry for the seemingly dumb questions, but since my original post was kinda long, some of the details might have been missed.

Ok. I see what you're saying. I misread. I thought you were saying you couldn't ever get focus back after you minimized and r-clicked. Yes, I am using Sun's JVM. I don't use MS' JVM at all since it is so outdated.

You're going to have to figure out a way to send a WM_SETFOCUS message to the appropriate window on a WM_MOUSEACTIVATE, as right-clicks don't ever send WM_SETFOCUS in the current applet. I don't know why this doesn't work the same as the Microsoft JVM. Perhaps it is something inherent in the applet. In any case, I'd suspect that what you see with the Sun JVM is appropriate behavior. Maybe you should petition the developers to modify the mouse listener to set focus on right-clicks.

  • 0

You're going to have to figure out a way to send a WM_SETFOCUS message to the appropriate window on a WM_MOUSEACTIVATE, as right-clicks don't ever send WM_SETFOCUS in the current applet. I don't know why this doesn't work the same as the Microsoft JVM. Perhaps it is something inherent in the applet. In any case, I'd suspect that what you see with the Sun JVM is appropriate behavior. Maybe you should petition the developers to modify the mouse listener to set focus on right-clicks.

Thanks a lot for your suggestion. I finally figured it out and it seems to work perfectly now. I had tried using WM_SETFOCUS before without success, but apparently I was sending the message to the wrong window. In case you or anyone else is interested, it was the child window of "Internet Explorer_Server" which in this case was "Java Plug-in Control Window."

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • OK, back to normal now! Ozzy Osbourne - Flying High Again (Official Music Video)  
    • The actual download size is ~130–180 MB, not 100 MB.
    • Big change of pace for me! Gunnar & the Grizzly Boys - Standard American (Official)  
    • draw.io Desktop 30.2.4 by Razvan Serea draw.io desktop is a downloadable security-first diagramming application that runs on Windows, MacOS and Linux. Creating diagrams in the desktop app doesn’t need an internet connection. This is useful when you are disconnected or when you must create diagrams in a highly secure environment, where data protection is of the utmost importance. When you use the draw.io desktop app, your diagrams will be stored on your local device. Because this is a stand-alone application, also designed to run offline, there are no interfaces to cloud storage platforms available. Of course, you can still store your diagrams in folders that are synchronised to your cloud storage if you wish. Easy-to-use diagram editor The draw.io apps work just like the office and drawing tools you are used to using. Drag and drop shapes from the shape libraries and drag to draw connectors between them. Drag connectors to add waypoints and set a precise shape and position, or let them reroute automatically. Double click and start typing to add a label to anything. Create tables and swimlane flows with a familiar tool. Style shapes and connectors with customisable palettes, sketch options, fonts and text formatting tools. Search for shapes, including in open-source icon libraries. Use our vast libraries of shapes and templates, organised into logical categories, to create a range of diagrams and infographics. Generate diagrams from text descriptions using our smart templates. Diagram faster with keyboard shortcuts. draw.io Desktop 30.2.4 changelog: Uses electron 42.4.1 Updates to draw.io core 30.2.4. Download: draw.io 64-bit | Standalone ~100.0 MB (Open Source) Download: draw.io 32-bit | ARM64 | ARM64 Standalone Links: draw.io Home Page | Project page @GitHub | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      Eurosoft10 earned a badge
      Week One Done
    • One Month Later
      Eurosoft10 earned a badge
      One Month Later
    • One Year In
      Skeet Campbell earned a badge
      One Year In
    • One Month Later
      Sharbel earned a badge
      One Month Later
    • First Post
      BizSAR earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      591
    2. 2
      +Edouard
      191
    3. 3
      PsYcHoKiLLa
      81
    4. 4
      Michael Scrip
      77
    5. 5
      Steven P.
      73
  • Tell a friend

    Love Neowin? Tell a friend!