Link to home
Start Free TrialLog in
Avatar of The Master
The Master

asked on

Looking for a simple IWebBrowser2 sample

I'm looking for a simple example showing how to create a WebBrowser control - NOT using MFC.

Ideally I would like a sample EXE that creates a main window which in turn contains the WebBrowser control. Every example I can find uses MFC. I'm in the process of learning ATL, so an ATL example would be fine, but even a straight bare-bones COM example would do.

Anyone know where I can find what I'm looking for? Thanks in advance!
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here's another example:

#include <windows.h>
#include <mshtml.h>
#include <exdisp.h>

#pragma warning(disable:4786)
#include <vector>
#include <string>



int function1 (void)
{
     CLSID clsid;
     LPUNKNOWN punk=NULL;
     IWebBrowser2 *pWB = NULL;
     IHTMLDocument2 *pHTML = NULL;
     IHTMLElementCollection *pElement = NULL;
     IDispatch *pdisp = NULL;
     DISPID  dispidNamed = DISPID_PROPERTYPUT;
     VARIANT_BOOL bBusy;
     VARIANT varName, varIndex, var;
     BSTR bstr;

     OleInitialize (NULL);
     CLSIDFromProgID (OLESTR("InternetExplorer.Application"), &clsid);
     CoCreateInstance (clsid, NULL, CLSCTX_SERVER, IID_IUnknown, (LPVOID *) &punk);
     punk->QueryInterface (IID_IWebBrowser2, (LPVOID *) &pWB);
     punk->Release();
     pWB->put_Visible (TRUE);
     BSTR bstrVal = SysAllocString (L"https://www.experts-exchange.com/jsp/qShow.jsp?qid=20165536");
     var.vt = VT_I4;
     var.lVal = 0;
     pWB->Navigate (bstrVal, &var, &var, &var, &var);
     SysFreeString (bstrVal);
     do
     {
      Sleep (500);
      pWB->get_Busy (&bBusy);
     }
     while (bBusy);
     pWB->get_Document (&pdisp);
     if (pdisp == NULL)
     {//Report error here
          return 0;
     }
     pdisp->QueryInterface (IID_IHTMLDocument2, (LPVOID *) &pHTML);
     pdisp->Release();
     pHTML->get_all (&pElement);
     long NumItems = 0;
     pElement->get_length(&NumItems);
     if (!NumItems)
     {//Report error here
          return 0;
     }

     CStdioFile TagFile("tagfile.txt",CFile::modeCreate|CFile::modeWrite);
     std::vector<std::string> ListOfTagNames;
     std::vector<std::string> ListOfInnerHTML;
     std::vector<std::string> ListOfOuterHTML;
     int InputElementQty = 0;
     for (int item_number_index = 0;item_number_index < NumItems;item_number_index++)
     {
          varName.vt = VT_I4;
          varName.lVal = item_number_index;//Get item via index number (zero based number)
          varIndex.vt = VT_I4;
          varIndex.lVal = 1;
          IDispatch *pdisp2 = NULL;
          if (pElement->item (varName, varIndex, &pdisp2) != S_OK)
          {//Report error here
                return 0;
          }
          if (pdisp2 == NULL)
          {//Report error here
                return 0;
          }
          IHTMLElement *pItem = NULL;
          pdisp2->QueryInterface (IID_IHTMLElement, (LPVOID *) &pItem);
          pdisp2->Release();
          if (pItem == NULL)
          {//Report error here
                return 0;
          }
          CString TempVariable;


          pItem->get_tagName(&bstr);
          TempVariable = bstr;
          std::string szTagName = (LPCTSTR)TempVariable;
          ListOfTagNames.push_back(szTagName);
          TagFile.WriteString(TempVariable);
          TagFile.WriteString("\n");

          if (TempVariable == "TEXTAREA")
          {
               IHTMLTextAreaElement  *pTextAreaElement = NULL;
               pdisp2->QueryInterface (IID_IHTMLTextAreaElement, (LPVOID *) &pTextAreaElement);
               if (pTextAreaElement == NULL)
               {//Report error here
                    AfxMessageBox("Error:  pTextAreaElement == NULL");
                    return 0;
               }
               BSTR bstrHelloWorld = SysAllocString (L"Hello World.  This really works!");
               pTextAreaElement->put_value(bstrHelloWorld);
               SysFreeString (bstrHelloWorld);
          }
          else if (TempVariable == "INPUT")
          {
               InputElementQty++;
               if (InputElementQty == 2)
               {
                    IHTMLOptionButtonElement *pOptionButtonElement = NULL;
                    pdisp2->QueryInterface (IID_IHTMLOptionButtonElement , (LPVOID *) &pOptionButtonElement);
                    if (pOptionButtonElement == NULL)
                    {//Report error here
                         AfxMessageBox("Error:  pOptionButtonElement == NULL");
                         return 0;
                    }
                    VARIANT_BOOL bIsChecked = TRUE;
                    pOptionButtonElement->put_checked(bIsChecked);
               }
          }
               
          pItem->get_innerHTML(&bstr);
          TempVariable = bstr;
          std::string szInnerHTML = (LPCTSTR)TempVariable;
          ListOfInnerHTML.push_back(szInnerHTML);

          pItem->get_outerHTML(&bstr);
          TempVariable = bstr;
          std::string szOuterHTML = (LPCTSTR)TempVariable;
          ListOfOuterHTML.push_back(szOuterHTML);

          pItem->Release();
     }
     AfxMessageBox("Move the web page to the bottom, and you'll notice that ANSWER is selected, and HELLO
WORLD in the textarea.\n\nDon't click OK until after you view changes.");
     TagFile.Close();
     pWB->Quit();
     pWB->Release();
     OleUninitialize ();
     return 1;
}


The last example code shows you how to use the Control Element Objects to make modifications
to the Internet Explorer.

The code opens this EE question.
Then it changes the COMMENT/ANSWER selection so that ANSWER is selected.
Then it puts some text into the Comment-Textarea.
If you need more information send me an email at "david@axter.com", and I'll post answer here.

The Email notification is not working, so if you just post comments here, I may not see it.
Avatar of AlexVirochovsky
AlexVirochovsky

Avatar of The Master

ASKER

Thanks for both of your comments.  However, it's not quite what I'm looking for.  What I need is an example of how to create a simple main window, in which I can create a MS Web Browser control.  I'm not real clear on what the minimal requirements are for the main window (in order to act as a container for the web browser control), nor am I very clear on how to attach the web browser control to the main window, once the main window is created.

I don't need anything else - no menu, no toolbars, no address bar, etc.  Just a simple window that would have the web browser control as a child.  I eventually want to have as much control over this window and it's message loop as possible, so I'd prefer that it actually be a window, and not a dialog box.

Thanks in advance for any further assistance!
Here's some example code that will put IE into your application.
All you have to do is pass the handle to your application window.


#include <mshtml.h>
#include <exdisp.h>
CLSID clsid;
LPUNKNOWN punk=NULL;
IWebBrowser2 *pWB = NULL;
IHTMLDocument2 *pHTML = NULL;
IHTMLElementCollection *pElement = NULL;
IDispatch *pdisp = NULL;
DISPID  dispidNamed = DISPID_PROPERTYPUT;
VARIANT_BOOL bBusy;
VARIANT var;

void PutIE_InWindow(HWND hWnd)
{
     OleInitialize (NULL);
     CLSIDFromProgID (OLESTR("InternetExplorer.Application"), &clsid);
     CoCreateInstance (clsid, NULL, CLSCTX_SERVER, IID_IUnknown, (LPVOID *) &punk);
     punk->QueryInterface (IID_IWebBrowser2, (LPVOID *) &pWB);
     pWB->put_Visible (TRUE);
     BSTR bstrVal = SysAllocString (L"https://www.experts-exchange.com"); //Set web page on  IE
     var.vt = VT_I4;
     var.lVal = 0;
     pWB->Navigate (bstrVal, &var, &var, &var, &var);
     SysFreeString (bstrVal);
     do
     {
          Sleep (500);
          pWB->get_Busy (&bBusy);
     }
     while (bBusy);
     
     
     long IE_HWND = 0;
     if (pWB->get_HWND(&IE_HWND) != S_OK)
     {
        AfxMessageBox("Failed: pWB->get_HWND");
        return;
     }

     HWND IEhWnd = (HWND)IE_HWND;

     //Code to put IE into a window
     //This will put IE into your app window
     ::SetParent(IEhWnd ,hWnd);

}
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "Axter" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: https://www.experts-exchange.com/commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
Per recommendation, force-accepted by
Netminder
CS Moderator
Hello
The answer accepted is uses MFC.( CString , etc...). But the user has strictly denying to use MFC.

Please convert the code without using MFC. Whatever it may be but really Good!

Thanks
Subrat.
If u need the complete code, plz let me know!