Jason
Jason

Reputation:

Delphi - TWebBrowser issues

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.

Upvotes: 3

Views: 2516

Answers (2)

user1897277
user1897277

Reputation: 495

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.

Upvotes: 0

Jason
Jason

Reputation:

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);

Upvotes: 6

Related Questions