Webbrowser steals focus

前端 未结 7 1818
天命终不由人
天命终不由人 2021-01-06 07:17

I\'m using webbrowser control in my winforms app (c#). And when it is doing automation things, I\'m losing focus control from the window I was working with. Webbrowsers\' fo

7条回答
  •  逝去的感伤
    2021-01-06 07:41

    I guess WebBrowser acquires the focus after a page is loaded by calling Navigate (or the Click method of an HtmlElement, which causes navigation). The focus could be given back to the control on the window (the TextBox) in the DocumentComplete event handler of the WebBrowser, but this is very difficult:

    1. When would you determine which control owned the focus originally? Before calling Navigate? This is not enough, because the user can move to another control after calling Navigate, but before handling DocumentComplete.

    2. AFAIK setting the focus to a TextBox will select its whole content, so you will have to put the cursor back to its original position. But when would you store the original position? Same problem.

    3. There can be more than one DocumentComplete event after a single Navigate (or Click).

    A possible solution would be to create a separate application for your hidden WebBrowser. This second application would be invisible, and could communicate with the original GUI application using some InterProcess Communication (IPC) technique. Because the WebBrowser in this case would run in a different process, you would have a better chance not to lose lose the focus and bother the user.

提交回复
热议问题