• 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

    • Too soon, I'm still not over this death!
    • Normally, I admit when a title is clickbait (unfortunately, it's become somewhat necessary in AI-dominated news sections today), but in this case, all supported versions is implied and doesn't need to be spelled out in the title. Of course, I'm covering a Patch Tuesday update but that is only available to supported Windows SKUs. All our coverage relates to supported Windows software and SKUs only unless we expressly state that it's "unsupported", "unofficial", or "third-party". I'm sorry, but supported/official SKUs don't need to be spelled out as such in every Neowin headline.
    • ALL versions or ALL SUPPORTED versions? Neowin does it again.
    • But the reality is it will work for people's needs, and they don't care about the technology that makes it. Clearly not everyone's needs, but that low end space where personal laptops were only used to type emails, watch content and browse websites, but they didn't want to do that on a small screen device. Heck, writing that out I can now see the connection and reason it'll do so well. Apple is about experience. If the experience is bad, they don't release it. Low end Windows laptop manufacturers up until this point have not taken that into consideration ever before, so slow laggy usage with brittle slimey plastic shells were common. I hope that the low end space at least creates better physical products that last a bit longer, and if Microsoft get their act together, they could also have a solid OS on such low end hardware that would actually make the experience work for what the hardware was intended for. The fact that the CPU is a "cellphone", sorry mobile phone processor is irrelevant. It's about the experience, and so far, that sounds quite solid.
  • Recent Achievements

    • Week One Done
      Jordan Smith earned a badge
      Week One Done
    • Reacting Well
      BizSAR earned a badge
      Reacting Well
    • First Post
      AndreaB earned a badge
      First Post
    • Week One Done
      Huge Trailer earned a badge
      Week One Done
    • Week One Done
      Classifyskilleducation earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      595
    2. 2
      +Edouard
      186
    3. 3
      PsYcHoKiLLa
      77
    4. 4
      Michael Scrip
      73
    5. 5
      Steven P.
      65
  • Tell a friend

    Love Neowin? Tell a friend!