使用windows API函数调用浏览器组件

#include <atlbase.h>
#include <atlwin.h>
#include <windows.h>
#include <tchar.h>
#pragma comment(lib,"atl")
#pragma comment(lib,"User32.lib")

CComModule _Module;
const TCHAR *URL[9] = {
    _T("http://www.csdn.net/"),
    _T("http://www.zol.com.cn/"),
    _T("http://www.cnblogs.com/"),
    _T("http://pal5q.baiyou100.com/2501000.asp"),
    _T("http://www.yinyuetai.com/"),
    _T("http://v.youku.com/v_show/id_XMzgwMjkyMTc2.html?f=17540585"),
    _T("http://social.msdn.microsoft.com/Forums/zh-CN/0631bed7-8b7e-4c0b-a666-90aa38e98075/win32-sdkwebbrowsercomatl"),
    _T("http://blog.csdn.net/geniusice18/article/details/16369499"),
    _T("http://cricode.com/1494.html")
};

LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
    RECT rc;
    IWebBrowser2* iWebBrowser;
    VARIANT varMyURL;
    static int i = 0;
    static CAxWindow WinContainer;
    LPOLESTR pszName=OLESTR("shell.Explorer.2");
    GetClientRect(hWnd, &rc);
    switch(message)
    {
    case WM_CREATE:
        WinContainer.Create(hWnd, rc, 0,WS_CHILD |WS_VISIBLE);
        WinContainer.CreateControl(pszName);
        WinContainer.QueryControl(__uuidof(IWebBrowser2),(void**)&iWebBrowser); 
        VariantInit(&varMyURL);
        varMyURL.vt = VT_BSTR; 
        varMyURL.bstrVal = SysAllocString(_T("http://www.163.com/"));
        iWebBrowser-> Navigate2(&varMyURL,0,0,0,0);
        VariantClear(&varMyURL); 
        iWebBrowser->put_Silent(VARIANT_TRUE);
//        iWebBrowser-> Release(); 
        SetTimer(hWnd,0,10000,NULL);
        break;
    case WM_SIZE:
        break;
    case WM_TIMER:
        {
            WinContainer.QueryControl(__uuidof(IWebBrowser2),(void**)&iWebBrowser); 
            VariantInit(&varMyURL);
            varMyURL.vt = VT_BSTR; 
            varMyURL.bstrVal = SysAllocString(URL[i++]);

            iWebBrowser-> Navigate2(&varMyURL,0,0,0,0);
            VariantClear(&varMyURL); 
//            iWebBrowser-> Release(); 
            break;
        }
    case WM_DESTROY:
        iWebBrowser->Quit();
        _Module.Term();
        PostQuitMessage(0);
        break;
    default:
        return (int)DefWindowProc(hWnd,message,wParam,lParam);
    }
    return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    static TCHAR szAppName[]=TEXT("WebBrowser");
    static TCHAR szClassName[]=TEXT("WebBrowser");
    HWND hWnd;
    MSG msg;
    WNDCLASS wndclass;
    wndclass.style=CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc=WndProc;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hInstance=hInstance;
    wndclass.hIcon=LoadIcon(hInstance, IDI_APPLICATION);
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wndclass.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
    wndclass.lpszMenuName=NULL;
    wndclass.lpszClassName=szClassName;

    if(!RegisterClass(&wndclass))
    {
        MessageBox(NULL,TEXT("Error!"),szAppName,MB_ICONERROR);
        return 0;
    }

    //HRESULT hRes = _Module.Init(NULL, hInstance);
    GUID guid; 
    HRESULT hRes = _Module.Init(NULL, hInstance, &guid);

    hWnd=CreateWindow(szClassName,szAppName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
    ShowWindow(hWnd,nShowCmd);
    UpdateWindow(hWnd);

    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
原文地址:https://www.cnblogs.com/jck34/p/3867900.html