IonicWind Software

Aurora Compiler => Tips and Tricks => Topic started by: sapero on July 30, 2008, 09:33:20 AM

Title: Rendering HTML directly into HDC
Post by: sapero on July 30, 2008, 09:33:20 AM
Yesterday i got a question how to render html page or string into HDC. You need to open a hidden browser, load the html, then call OleDraw, or IViewObject::Draw to render the html.
//#define UNICODE
#include "mshtml.inc"
#include "atl\\atliface.inc"
#include "tchar.inc"
#include "wchar.inc"


sub main()
{
HDC dc = GetDC(0); // render to desktop

#ifdef UNICODE
RenderHtml(dc, 0, 0, 200, 100, L"<HTML><BODY><B>Aurora</B><INPUT type='text'></BODY></HTML>");
#else
RenderHtml(dc, 0, 0, 200, 100, "<HTML><BODY><B>Aurora</B><INPUT type='text'></BODY></HTML>");
#endif

ReleaseDC(0, dc);
}




sub RenderHtml(HDC dc,int left, int top, int width,int height, LPTSTR html),HRESULT
{
HRESULT hr = E_FAIL;
OleInitialize(0);
IAxWinHostWindow *ax;
IHTMLDocument2 *document;

if (AtlAxWinInit())
{
#ifdef UNICODE
HWND hwnd = CreateWindow(L"#32770", NULL, WS_POPUP,
0, 0, width, height, 0, 0, _hinstance, 0);
#else
HWND hwnd = CreateWindow("#32770", NULL, WS_POPUP,
0, 0, width, height, 0, 0, _hinstance, 0);
#endif

LPWSTR name = new(WCHAR, _tcslen(html) + 8);

if (name)
{
#ifdef UNICODE
swprintf(name, L"MSHTML:%s", html);
#else
swprintf(name, L"MSHTML:%S", html);
#endif
IUnknown *atl;
hr = AtlAxCreateControl(name, hwnd, 0, &atl);
delete name;

if (!hr)
{
hr = atl->QueryInterface(_IID_IAxWinHostWindow, &ax);
atl->Release();
}
if (!hr)
{
hr = ax->QueryControl(_IID_IHTMLDocument2, &document);
ax->Release();
WaitReady(document);
}
if (!hr)
{
IViewObject *view;
hr = document->QueryInterface(_IID_IViewObject, &view);
document->Release();
}
if (!hr)
{
RECT rc;
SetRect(&rc, left, top, left+width, top+height);
hr = view->Draw(DVASPECT_CONTENT, -1, NULL, NULL, 0, dc, &rc, NULL, 0, 0);
view->Release();
}
}
DestroyWindow(hwnd);
}
OleUninitialize();
return hr;
}


sub WaitReady(IHTMLDocument2 *document)
{
int max = 50; // timeout = max * 20ms

while (max)
{
max--;
BSTR readystate;
document->get_readyState(&readystate);
BOOL ready = !wcscmp(readystate, L"complete");
SysFreeString(readystate);
if (ready) break;

MSG msg;
if (PeekMessage(&msg,0,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Sleep(20);
}
}
}