Get the URL of a hyperlink when the mouse moves over a twebbrowser document

Source: Internet
Author: User
Tags xml parser

The twebbrowser Delphi Component provides access to the web browser functionality from your Delphi applications.

In most situations you use the twebbrowser to display HTML documents ents to the user-thus creating your own version of the (Internet Explorer) Web browser. note that the twebbrowser can also display Word documents, for example.

A very nice feature of a browser is to display link information, for example, in the status bar, when the mouse hovers over a link in a document.

The twebbrowser does not expose an event like "onmousemove". Even if such an event wocould exist It wocould be fired for the twebbrowser component-not for the document being displayed inside the twebbrowser.

In order to provide such information (and much more, as you will see in a moment) in your Delphi application using the twebbrowser component, a technique called"Events sinking"Must be implemeted.

Webbrowser event sink

To navigate to a Web page using the twebbrowser component you callNavigateMethod. Sorted sored links

Webrenderer-Java sdkjava HTML and multimedia component HTML 4.01, CSS, XSL, XML, sslwww.webrenderer.com

Convert data to xmlcrediblexml-award winning software new V2.1 supports. Net & javawww.crediblexml.com/

Delphi XML componentdownload native Delphi XML Parser and writer (source available) www. simdesign. nl/xml.html

TheDocumentProperty of the twebbrowser returnsIhtmldocument2Value (for Web users). This interface is used to retrieve information about a document, to examine and modify the HTML elements and text within the document, and to process related events.

To get the "href" attribute (Link) of an "A" tag inside a document, while the mouse hovers over a document, you need to react on the "onmousemove" event of the ihtmldocument2.

Here are the steps to sink events for the currently loaded document:

  1. Sink the webbrowser control's events inDocumentcompleteEvent raised by the twebbrowser. This event is fired when the document is fully loaded into the web browser.
  2. Inside documentcomplete, retrieve the webbrowser's Document Object and sink the html#entevents interface.
  3. Handle the event you are interested in.
  4. Clear the sink in the inBeforenavigate2-That is when the new document is loaded in the Web browser.
HTML document onmousemove

Since we are interested in the href attribute of an element-in order to show the URL of a link the mouse is over, we will sink the "onmousemove" event.

The procedure to get the tag (and its attributes) "below" The mouse can be defined:

VaR
Htmldoc: ihtmldocument2;

...
ProcedureTform1.document _ onmouseover;
VaR
Element: ihtmlelement;
Begin
IfHtmldoc =Nil ThenExit;

Element: = htmldoc. parentwindow. event. srcelement;

Elementinfo. Clear;

IfLowercase (element. tagname) = 'A' then
Begin
Showmessage ('link, href: '+ element. getattribute ('href', 0)]);
End
Else IfLowercase (element. tagname) = 'img'Then
Begin
Showmessage ('image, Src: '+ element. getattribute ('src', 0)]);
End
Else
Begin
Elementinfo. lines. Add (format ('tag: % s', [element. tagname]);
End;
End;(* Document_onmouseover *)

As explained above, we attach to the onmousemove event of a document in the ondocumentcomplete event of a twebbrowser:

ProcedureTform1.webbrowser1documentcomplete (asender: tobject;
Const Pdisp: idispatch;
VaR URL: olevariant );
Begin
IfAssigned (webbrowser1.document)Then
Begin
Htmldoc: = webbrowser1.documentAsIhtmldocument2;

Htmldoc. onmouseover: = (teventobject. Create (document_onmouseover)AsIdispatch );
End;
End;(* Webbrowser1documentcomplete *)

And this is where the problems arise! As you might guess the "onmousemove" event is ** not * a usual event-as are those we are used to work with in Delphi.

The "onmousemove" expects a pointer to a variable of type variant of Type vt_dispatch that means es the idispatch interface of an object with a default method that is invoked when the event occurs.

In order to attach a Delphi procedure to "onmousemove" you need to create a wrapper that implements idispatch and raises your event in its invoke method.

Here's the teventobject interface:

Teventobject =Class(Tinterfacedobject, idispatch)
Private
Fonevent: tobjectprocedure;
Protected
FunctionGettypeinfocount (OutCount: integer): hresult; stdcall;
FunctionGettypeinfo (index, localeid: integer;OutTypeinfo): hresult; stdcall;
FunctionGetidsofnames (ConstIID: tguid; names: pointer; namecount, localeid: integer; DISPIDs: pointer): hresult; stdcall;
FunctionInvoke (dispid: integer;ConstIID: tguid; localeid: integer; flags: word;VaRParams; varresult, raise info, argerr: pointer): hresult; stdcall;
Public
ConstructorCreate (ConstOnevent: tobjectprocedure );
PropertyOnevent: tobjectprocedureReadFoneventWriteFonevent;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.