因为工作关系,需要在sdk下面嵌入一个web浏览器,但是程序是sdk开发的,网上有很多文章,但是都是设计mfc的,后来在网友帮助下面得到了两种实现方法.
1.是基于atl的:
#include CComModule _Module;#include #pragma comment(lib,"atl") int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) { MSG msg; //初始化com和atl CoInitialize(NULL); AtlAxWinInit(); HWND hwnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_UNLIMITED), 0, MainDlgProc); if(!hwnd) { MessageBox(NULL, TEXT("Fail to create the dialog!"), "Test", MB_ICONERROR); return 0; } ShowWindow(hwnd, SW_SHOW); while(GetMessage(&msg, NULL, 0, 0)) { if(hwnd == 0 || !IsDialogMessage(hwnd, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } CoUninitialize(); return msg.wParam;} BOOL CALLBACK MainDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { RECT rc; IWebBrowser2* WebBrowser; VARIANT varMyURL; static CAxWindow WinContainer;//这个是atl提供的com容器 switch(message) { case WM_INITDIALOG: rc.top = 8; rc.left = 8; rc.bottom = 250; rc.right = 180; WinContainer.Create(hDlg, rc, LPCTSTR("Microsoft.IExplorer.4"), WS_CHILD|WS_VISIBLE|WS_VSCROLL );//create a browser control WinContainer.QueryControl( __uuidof(IWebBrowser2), (void**)&WebBrowser); VariantInit(&varMyURL); varMyURL.vt = VT_BSTR;#ifndef UNICODE { wchar_t *buffer; DWORD size; size = MultiByteToWideChar(CP_ACP, 0, "www.xxx.com", -1, 0, 0);
if (!(buffer = (wchar_t *)GlobalAlloc(GMEM_FIXED, sizeof(wchar_t) * size))) return FALSE;
MultiByteToWideChar(CP_ACP, 0, "www.xxx.com", -1, buffer, size);
varMyURL.bstrVal = SysAllocString(buffer);
GlobalFree(buffer);
}
#else
varMyURL.bstrVal = SysAllocString("www.xxx.com");
#endif
WebBrowser->Navigate2(&varMyURL, 0, 0, 0, 0);
VariantClear(&varMyURL);
WebBrowser->Release();
break;
}//end switch(message)
return FALSE;
}