How to make TWebBrowser get focus in Delphi

   
 

  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 

How to make TWebBrowser get focus in Delphi

上一篇:winform上控件的拖拽小结


下一篇:实验-----通过loganalyzer展示数据库中的日志