Since TWebBrowser is inherited methods from TControl, you may think immediately to use the method 'SetFocus' to make it get the focus. Unfortunately, it does not work. So we have to find out some way else to accomplish this task. I have found three ways to make it focused.
1. The first way need to write a function to get it.
procedure TForm1.WebBrowserSetFocus; begin if WebBrowser1.Document nil then with WebBrowser1.Application as IOleobject do DoVerb(OLEIVERB_UIACTIVATE, nil, WebBrowser1, 0, Handle, GetClientRect); end;
2. Another easy way to get it.
if WebBrowser1.Document nil then IHTMLWindow2(IHTMLDocument2(WebBrowser1.Document).ParentWindow).focus
3. The best way I found to get it.
if WebBrowser1.Document nil then IHTMLWindow4(WebBrowser1.Document).focus
Ok, when you need to determind if a webbrowser has the focus, use the following code:
if IHTMLWindow4(WebBrowser1.Document).hasfocus then