December 04, 2020, 04:23:09 am

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Rendering HTML directly into HDC

Started by sapero, July 30, 2008, 09:33:20 am

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

sapero

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.
Code Select
//#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);
}
}
}