none
Fastest way to embed OLE windows in your App

Answers

  • If there is some code snippet in C that can make WebBrowser run embedded in my window, that would be lovely.

    In that case please supply the code to do that.

    A simple way is with "AtlAxWin" class =>

    #include <windows.h>
    #include <tchar.h>
    #include <exdisp.h> // IWebBrowser2
    #include <Mshtmhst.h> // IDocHostUIHandler
    #include <ATLBase.h> 
    
    HINSTANCE hInst;
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    int nWidth = 1024, nHeight = 800;
    
    HINSTANCE hATLDLL;
    typedef BOOL(__stdcall *PAAWI)(void);
    PAAWI pAtlAxWinInit;
    typedef HRESULT(__stdcall *PAAGC) (HWND hWnd, IUnknown** pUnknown);
    PAAGC pAtlAxGetControl;
    typedef HRESULT(__stdcall *PAAGH)(HWND h,IUnknown** pp);
    PAAGH pAtlAxGetHost;
    
    int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
    {
    	hInst = hInstance;
    
    	hATLDLL = LoadLibrary(_T("atl.dll"));
    	pAtlAxWinInit = (PAAWI)GetProcAddress(hATLDLL, "AtlAxWinInit");
    	pAtlAxGetControl = (PAAGC)GetProcAddress(hATLDLL, "AtlAxGetControl");
    	pAtlAxGetHost = (PAAGH)GetProcAddress(hATLDLL, "AtlAxGetHost");
    	if (pAtlAxWinInit && pAtlAxGetControl && pAtlAxGetHost)
    		pAtlAxWinInit();
    	else
    		return 1;
    
    	WNDCLASSEX wcex =
    	{
    		sizeof(WNDCLASSEX), 0, WndProc, 0, 0, hInst, LoadIcon(NULL, IDI_APPLICATION),
    		LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW + 1), NULL, TEXT("WindowClass"), NULL,
    	};
    	if (!RegisterClassEx(&wcex))
    		return MessageBox(NULL, L"Cannot register class !", L"Error", MB_ICONERROR | MB_OK);
    	int nX = (GetSystemMetrics(SM_CXSCREEN) - nWidth) / 2, nY = (GetSystemMetrics(SM_CYSCREEN) - nHeight) / 2;
    	HWND hWnd = CreateWindowEx(0, wcex.lpszClassName, TEXT("Test"),
    		WS_OVERLAPPEDWINDOW, nX, nY, nWidth, nHeight, NULL, NULL, hInst, NULL);
    	if (!hWnd)
    		return MessageBox(NULL, L"Cannot create window !", L"Error", MB_ICONERROR | MB_OK);
    	ShowWindow(hWnd, SW_SHOWNORMAL);
    	UpdateWindow(hWnd);
    	MSG msg;
    	while (GetMessage(&msg, NULL, 0, 0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return (int)msg.wParam;
    }
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static HWND hHTMLWindow = NULL;
    	switch (message)
    	{
    	case WM_CREATE:
    	{
    		hHTMLWindow = CreateWindow(_T("AtlAxWin"), _T("about:blank"), WS_CHILD | WS_VISIBLE, 10, 10, nWidth - 40, nHeight - 60, hWnd, (HMENU)1000, hInst, 0);
    		HRESULT hr = E_FAIL;
    		IUnknown* pUnknown = NULL;
    		IWebBrowser2 *pWebBrowser = NULL;
    		hr = pAtlAxGetControl(hHTMLWindow, &pUnknown);
    		if (SUCCEEDED(hr))
    		{
    			hr = pUnknown->QueryInterface(__uuidof(IWebBrowser2), (void**)&pWebBrowser);
    			if (SUCCEEDED(hr))
    			{	
    				VARIANT var;
    				var.vt = VT_EMPTY;
    				BSTR bstrURL;				
    				bstrURL = SysAllocString(L"www.google.com");
    				hr = pWebBrowser->Navigate(bstrURL, &var, &var, &var, &var);
    				SysFreeString(bstrURL);			
    			}
    			pUnknown->Release();
    		}
    		return 0;
    	}
    	break;
    	case WM_SIZE:
    		MoveWindow(hHTMLWindow, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
    		break;
    	case WM_DESTROY:
    	{
    		PostQuitMessage(0);
    		return 0;
    	}
    	break;
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    
    

    Sunday, May 12, 2019 12:13 PM