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