Link to home
Create AccountLog in
Avatar of Grant1842
Grant1842Flag for United States of America

asked on

(Advanced Expert Question Here) Get position of clicked text in twebbrowser and go to it in SynEdit(RichEdit)

I have a synedit(richedit) and a twebbrowser on a form. I have html code in the richedit.
I load the html from the rich edit into twebbrowser;
I want to click on any text or object (image)in the twebbrowser  and go to that place in the code on the richedit.
I am looking for example code to do this in Delphi p7.
Avatar of Emmanuel PASQUIER
Flag of France image

That's going to be a lot of fun. I think you should extend the zone to more experts, we could need help :
- Delphi Programming
- Internet & Delphi Programming
I will try to take a closer look at this later this afternoon.  I am only writing this to let you know that someone else is looking into it.
Avatar of Grant1842


This is a Hard question Does anyone have an Ideal How to do this.
Maybe with this :

we could add an onclick event on every object of the page...
I am getting Service Unavailable when  I click that link
oh ? it works for me...

here is the code related to this article
unit Unit1;


  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw, MSHTMLEvents, MSHTML_TLB;

  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    procedure FormCreate(Sender: TObject);
    procedure WebBrowser1DocumentComplete(Sender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
    HTMLAnch : OleVariant;
    AnchEvents : Array Of TMSHTMLHTMLAnchorEvents2;
    Function LinkOnClick(Sender: TObject; const pEvtObj: IHTMLEventObj) : WordBool;

  Form1: TForm1;


{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);

Function TForm1.LinkOnClick(Sender: TObject; const pEvtObj: IHTMLEventObj) : WordBool;
     pEvtObj.srcElement.innerText := 'Clicked';

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
Var aa : Byte;
     HTMLAnch := WebBrowser1.OleObject.document.getElementsByTagName('A');
     For aa := 0 To HTMLAnch.length-1 Do
          SetLength(AnchEvents, Length(AnchEvents)+1);
          AnchEvents[aa] := TMSHTMLHTMLAnchorEvents2.Create(Self);
          AnchEvents[aa].onclick := LinkOnClick;


Open in new window

THanks for that . THe site was down the other day for me when I clicked on it I see its up now.
I tried that and its not really what I need.........

With the example you posted It seems to me there should be a way to at least get in a BLOCK OF CODE or RANGE of where you clicked.
It is useless to have a twebbrowser in edit mode if you can not detect where you clicked and get close to it in the richedt so you can work on your code..........

Any experts here have an ideal to get this done ?
Avatar of developmentguru
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanks for your reply.
I have allready tried TEmbeddedWB and its many example but I have failed to find a way to catch the click and go to the region in the richedit source.
Ah, I bellieve it should give you the means of finding out where the user clicked.  Beyond that you will need to scan your source (in the rich edit) to find the location.  This can be a lot more difficult than it would first appear.  A simple search for text will not do.

1) You will need to verify that you can get the click event from the TEmbeddedWB.
2) figure out a way to parse the source to find the relevant code.

It sounds like you are working on a web authoring tool.  Knowing what you are aiming for would help.  What type of page is this?  Pure HTML?  ASPX? What?
Thanks for your help. I am working on editing joomla 1.5 templets. so it is mixed PHP/HTML.
Did you get the click event?
Have not had time to look into it. I am currently looking into the GECKO html engine. I think it will take a couple of modules one for the active x to interface to the GECKO engine and One to get the code back into the rich edit.
THanks for your help with these questions. If the GECKO works I will accept you answer on the other question.
Solution was found useing A Custom Twebbrowser.
No need for the GECKO stuff.