MFC interacts with WebBrowser (via JS)

Source: Internet
Author: User
Tags blank page

Recently modified the old MFC project, with the Web interface, and HTML interactive use of the COM method, cumbersome, ugly and low performance, so consider using JS to interface interaction, check the information of the day, now collated as follows, for later needs of the people reference, Although almost no one will use MFC to do the project (laughter), with WinForm or WPF or Web interface, high-performance parts with C + + encapsulated DLL may be a better choice, of course, directly in the EXE to provide HTTP access interface is the most roar (go language race high)! Interface This is a relatively large topic, you can open a topic slowly said ...

The core of a Cwebpage class is written by a foreigner, encapsulated a number of COM operations, and WebBrowser to make a JS call, but can only invoke the existing JS code, sometimes need to inject some JS function to the Web page (you know), you can be on a blank page, Use JS to generate the entire page.

ihtmldocument2* Phtmldoc = (ihtmldocument2*) web.get_document (); if(Phtmldoc! =NULL) {IHTMLWindow2*Phtmlwnd; Phtmldoc->get_parentwindow (&Phtmlwnd); if(Phtmlwnd! =NULL)            {CString Js_code; Js_code="function Test (str) {alert (str); return str;}";//This is the actual JavaScript code that was written//Js_code. Format (L "gotolocation ('%s ');", Jscode);//JS function name + parameterVARIANT ret; Phtmlwnd->execscript (Js_code. AllocSysString (), L"Javascript", &ret);//executing javascript codePhtmlwnd->Release ();            Cwebpage page; Page.            Setdocument (Web.get_document ());            CComVariant Varresult; Page. Calljscript ("Test","123",&varresult);        MessageBox (CString (varresult.bstrval)); } Phtmldoc-Release (); }   

First injection to the Web page (this sounds taller) a JS function (code is only one of the methods, and other methods are not enumerated), and then through the webpage encapsulated method call function and return the value, this will be more than C + + with COM interface operation Web page simple and faster (and no test, Theoretically it should be)

Here just to solve the MFC call Js,js call MFC is more complex and do not go deep, com this thing feels really ancient relics, the project is to intercept the page's jump address to determine the need to carry out the operation, basically enough, Remember WinForm WebBrowser and JS call each other is to encapsulate the object into a COM object exposed to webbrowser.

Source: Download

MFC interacts with WebBrowser (via JS)

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.