Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Web Browser control - I'm stumped 1

Status
Not open for further replies.

ITXAssociates

Technical User
Feb 10, 2011
7
GB
I'm using the Web Browser control in design mode as a WYSIWYG editor. On the document object, I want to replace the default right click menu with one of my a own but can't see any way to hook into it to do so.

Can anyone point me in the right direction please?

Thank you
 
I was thinking you might be able to do this with BINDEVENT(). But the browser control doesn't even have a right-click event to bind to, so that probably won't work.

I can only add that I once tried to use the web browser control in edit mode, but eventually gave up, partly because of this same lack of control over the interface, but also because of other frustrations.

I ended up using a third-party control called the TinyMCE Editor instead. You still need a web browser control to host it, but the browser doesn't go into edit mode; it's just needs an HTML form which is a lot simpler.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Create an html document or insert this in the the current one. This will disable the right-click menu, a pop-up a message. But the function2 method can also be showing your own menu
Code:
<html>
<head>
<script language="JavaScript">
    function function2() {
        alert("This image is copyrighted") 
    }
</script>
</head>
<body oncontextmenu="function2(); return false;">
    <p>Right click in the image.</p>
    <img oncontextmenu="function2();return false;"" 
         src="[URL unfurl="true"]http://www.mcrgsoftware.com/bus.png"[/URL] 
         alt="[URL unfurl="true"]http://www.mcrgsoftware.com"[/URL] 
         width="99" 
         height="76">
</body>
</html>


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
Thanks guys but no go.

I can't get EVENTHANDLER() to work unfortunately, and a javascript menu is no use as I need to interact with the VFP form

Thanks anyway
 
Hi mgagnon

The Javascript kills the standard menu OK. What I'm trying to achieve is to process the selected text with a VFP function

Thanks for your help
 
Have a read on this. In short, binding to IE events via EventHandler(), but of course you need to define a class implementing the IE events to be usable as such an event handler. This is described here among many other things:
It's quite at the end. Search for "context menu", the first occurrence is at the start of the article section describing such an event handler class.

It's not the easiest stiff, but the only solution giving you control within foxpro.

Bye, Olaf.
 
Whan you say you can't get EVENTHANDLER() to work ... what have you tried, and in what way doesn't it work? It's worth persevering with this solution, as it will most likely solve the problem.

Regarding Mike's Javascript: you're right that this won't give you a way of bringing up a native VFP menu. But you might be able to provide the equivalent functionality by some other means, such as a keypress or a toolbar.

But please keep in mind that using the web browser control in edit mode is a very limited feature, and offers almost no opportunities for customisation. If you really want a full-feature HTML editor in your app, you should look for third-party products (such as the TinyMCE Editor I mentioned earlier).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Thanks guys, Olaf's link to Rick Strahl's page cracked it for me [thumbsup2]

Thanks to you all for your help
 
Mike, he could copy over the part of Rick Strahls page, but that would infringe copyrights. While that would be a mild issue, as the page is public and meant to be read and used by everyone, it would just cut that out of a very informative presentation about the Webbrowser control.

Rick Strahl has described how to create an eventhandler. The main point is "handle the Document’s OnContext event." You can look out for that text in Rick Strahls page or, as I said above, look out for "context menu", which is also near that passage of the text.

If you want a past of the eventhandler, that would be rather verbose, as there are many events in it. It's easier to follow the instructions on how to create a class definition implementing an interface via drag & drop from object browser to a vfp prg window than to paste all the code generated.

Bye, Olaf.
 
Olaf,

I take your point about copyright, verbose text, etc. I wasn't suggesting that ITXAssociates reproduces any of Rick's article. I was just curious to know how he solved the problem. (Or course, if I wasn't so lazy, I would have followed the link and read the entire article for myself).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
In VFP Tools > Object Browser
Open Type Library and browse for mshtml.tlb
This is in windows\system32 for x86 or windows\syswow64 for x64. Scroll down th left pane and look for Interfaces. Expand and now look for HTMLDocumentEvents . Drag and drop this onto an open edit window and you get something like

DEFINE CLASS myclass AS session OLEPUBLIC

IMPLEMENTS HTMLDocumentEvents IN "C:\WINDOWS\SYSWOW64\MSHTML.TLB"

PROCEDURE HTMLDocumentEvents_onhelp() AS LOGICAL
* add user code here
ENDPROC

PROCEDURE HTMLDocumentEvents_onclick() AS LOGICAL
* add user code here
ENDPROC

PROCEDURE HTMLDocumentEvents_ondblclick() AS LOGICAL
* add user code here
ENDPROC

etc

ENDDEFINE


Rename thus:

DEFINE CLASS HTMLDocumentEvents AS session OLEPUBLIC

IMPLEMENTS HTMLDocumentEvents IN "C:\WINDOWS\SYSWOW64\MSHTML.TLB"

PROCEDURE HTMLDocumentEvents_onhelp() AS LOGICAL
* add user code here
ENDPROC


HTMLDocumentEvents is now the prototype class containing all events for the Document object, including dozens I didn't want. Rather than edit it, I sub-classed that again, and just implemented the events I wanted.

DEFINE CLASS RedlineHtmlDocumentEvents as HtmlDocumentEvents
PROCEDURE HTMLDocumentEvents_oncontextmenu() AS LOGICAL
_screen.ActiveForm.oMenu.showmenu()
RETURN .F. && Suppress the context menu
ENDPROC
ENDDEFINE


I've now got two classes in my classlib. Then it was simply
SET PROCEDURE TO redLineLibrary.prg (my Classlib, in the form init)

Next load a document and bind the event

o = THISFORM.oBrowser && ACtiveX MS WebBrower control
THISFORM.cNavigateFile = GETFILE("htm,html")
o.NAVIGATE(THISFORM.cNavigateFile)

*** Wait for document to load ***
DECLARE INTEGER Sleep IN WIN32API INTEGER nTimeout
DO WHILE this.oBrowser.ReadyState # 4
Sleep(100)
ENDDO

***
oHandler = CREATEOBJECT("RedlineHtmlDocumentEvents")
EVENTHANDLER(o.Document,RedlineHtmlDocumentEvents )

ENDIF


Right click and I have my own menu using the VFP _shortcut menu foundation class

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top