3

Two quick questions

  1. How do I set focus to a TWebBrowser? This is so the mouse wheel scrolls the display without having to click within the TWebBrwoser display area first. It has a setfocus method that does nothing (or seems to do nothing).

  2. Within a TWebBrowser, right click a displayed link and select properties. The OK and Cancel buttons are disabled and you cannot close the dialog. You need to end task your app to kill it.

Any ideas?

Thanks, Jason.

2
  • In SO it's best to ask questions separately. The point of SO is to create a repository of questions with high-quality answers. If you get two separate answers to your questions, which will you Accept? Commented May 28, 2009 at 2:44
  • Makes sense. I will create separate questions in the future. Jason. Commented May 28, 2009 at 5:57

2 Answers 2

6

Answer for Question 1 after much web hunting....

 with WebBrowser1 do if Document <> nil then with Application as IOleobject do DoVerb(OLEIVERB_UIACTIVATE, nil, WebBrowser1, 0, Handle, GetClientRect); 
Sign up to request clarification or add additional context in comments.

Comments

0

This is covered in the following article by Peter Johnson, How to make a TWebBrowser become the active control when clicked.

To summarise heavily, add this OnCommandStateChange event:

procedure TWebBrowserFrame.CommandStateChange(Sender: TObject; Command: Integer; Enable: WordBool); var Doc: IHTMLDocument2; // document object Sel: IHTMLSelectionObject; // current selection begin // Check we have a valid web browser triggering this event if not Assigned(Sender) or not (Sender is TWebBrowser) then Exit; // Check we have required command if TOleEnum(Command) <> CSC_UPDATECOMMANDS then Exit; // Get ref to document object and check not nil Doc := Browser.Document as IHTMLDocument2; if not Assigned(Doc) then Exit; // Get ref to current selection Sel := Doc.selection as IHTMLSelectionObject; // If selection is of correct type then we have a mouse click if Assigned(Sel) and (Sel.type_ = 'Text') then begin // Make the web browser the form's active control (Sender as TWebBrowser).SetFocus; Doc.parentWindow.focus; end; end; 

There is a lot more detail in the article, please do make sure you read it all.

1 Comment

I edited in a reference to the article from where you obtained this code. Please do not post code without attribution.