Making a c++ based program to show a web page on windows [duplicate]

Assuming you use MFC, this is fairly trivial. Create an MFC application with the AppWizard. In the first page of the AppWizard options, change the “Application Style” to “MFC Standard” and (probably) change it to a “Single Document” program. In the last page, change the base class for your view from CView to CFormView.

Go to the project’s resource view, and edit the IDD_<project_name>_FORM dialog. It’ll initially contain a static control saying something about “insert controls here”. Delete that static control, then right-click on the form and select “Insert ActiveX Control…”. That’ll pop up a list of ActiveX controls. Choose “Microsoft Web Browser” from the list.

That’ll put a (small) web browser in your window (you probably want to stretch it to fill the window). Right click on the control, and select “Add variable…”. That’ll bring up a dialog where you need to fill in the name for the variable (e.g., you could name it “browser”).

Then switch to the class view and choose your view class. In the lower pane, double click on “OnInitialUpdate”. After the code that’s already present for that function, add a line like:

browser.Navigate("http://www.google.com", NULL, NULL, NULL, NULL);

[obviously replacing “http://www.google.com” with the URL of the web site you want to display].

Compile and run, and (assuming your computer is connected to the Internet, etc.) it should open the chosen web page at startup.

You probably also want to add a handler for WM_SIZE in your view, and have it resize the control to fill the window when/if the user resizes the window. This is a tiny bit more complicated, because your window will receive WM_SIZE messages before your control is entirely initialized. As such, you normally want to add a bool variable named something like “control_valid”. Initailize it to “false” in the view’s constructor. In the OnInitialUpdate code after the “Navigate” call, add “valid = true;”. Then in the WM_SIZE handler, only resize the control if (valid), something like:

void Cbrowse_fixedView::OnSize(UINT nType, int cx, int cy)
{
    CFormView::OnSize(nType, cx, cy); 

    // This is the block of code we added:
    if(valid) {
        CRect rect;
        GetClientRect(&rect);
        m_browser.MoveWindow(&rect);
    }
}

With that, When the user resizes the application’s window, the browser control will be resized to fill the window at all times.

Leave a Comment