2008年4月10日 星期四

HTML VIEW學習筆記

要在建立html view, 一定要include htmlctrl.h 及 htmlview.lib

//Handle to HTML view control
HWND m_hwndHtml;
HINSTANCE m_HtmlViewInstance;

初始化:

if (m_HtmlViewInstance == 0) {
m_HtmlViewInstance = ::LoadLibrary(L"htmlview.dll");
}
else
return FALSE;

VERIFY(InitHTMLControl(m_HtmlViewInstance));

DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS|WS_BORDER;
RECT rect;
GetClientRect(GetForegroundWindow(), &rect);
m_hwndHtml = CreateWindow (DISPLAYCLASS,
NULL,
dwStyle,
rect.left,
rect.top,
rect.right,
rect.bottom,
hWnd,
0,
m_HtmlViewInstance,
NULL);
if( !m_hwndHtml)
return FALSE;

SetWindowLong(m_hwndHtml, GWL_ID, 77777);
SetFocus (m_hwndHtml);
SendMessage(m_hwndHtml, WM_SETTEXT, 0, (LPARAM)(LPCTSTR)_T(""));

wchar_t None[1024];
LoadString(g_hInst,IDS_HTMLTEXT,None,1024);
SetHtml(None);

=============================================================

void SetHtml(wchar_t *strHtml)
{
SendMessage(m_hwndHtml, WM_SETTEXT, 0, (LPARAM)(LPCTSTR)_T(""));
SendMessage(m_hwndHtml, DTM_ADDTEXTW, FALSE, (LPARAM)(LPCTSTR)strHtml);
SendMessage(m_hwndHtml, DTM_ENDOFSOURCE, 0, 0);
}
==============================================================

接著, 在訊息迴圈中 handle WM_NOTIFY 處理相關的動作。

case WM_NOTIFY:
{
NM_HTMLVIEW * pnmHTML = (NM_HTMLVIEW *) lParam;
LPNMHDR pnmh = (LPNMHDR) &(pnmHTML->hdr);

INLINEIMAGEINFO InlineImageInfo;
//DWORD dwCookieValue;
//LPSTR szSRCText;
HBITMAP hBitmap;

//NMHDR * pnmh = (NMHDR *)lParam;

switch (pnmh->code)
{
case NM_HOTSPOT:
{
OnLink(pnmHTML->szTarget);
//OutputDebugString(TEXT("NM_HOTSPOT\r\n"));
break;
}

case NM_INLINE_IMAGE:
{
if( 0==wcscmp( pnmHTML->szTarget,(LPCWSTR)L"rose")){
hBitmap = LoadBitmap( g_hInst, MAKEINTRESOURCE(IDB_BITMAP1));
}else if( 0==wcscmp(pnmHTML->szTarget ,(LPCWSTR)L"phone")){
hBitmap = LoadBitmap( g_hInst, MAKEINTRESOURCE(IDB_BITMAP2));
}else{
hBitmap = LoadBitmap( g_hInst, MAKEINTRESOURCE(IDB_BITMAP3));
}

BITMAP bmp;
GetObject(hBitmap, sizeof(BITMAP), &bmp);

InlineImageInfo.dwCookie = pnmHTML->dwCookie;
InlineImageInfo.iOrigHeight = bmp.bmHeight;
InlineImageInfo.iOrigWidth = bmp.bmWidth;
InlineImageInfo.hbm = hBitmap;
InlineImageInfo.bOwnBitmap = FALSE;
SendMessage(m_hwndHtml,DTM_SETIMAGE,0,(LPARAM)&InlineImageInfo);

OutputDebugString(TEXT("NM_INLINE_IMAGE\r\n"));

return 1;
/*If you are handling image processing yourself for the
HTML Control, you must return a non-zero value from
NM_INLINE_IMAGE. Otherwise the HTMLControl will attempt
to load the images on it's own, which will override the
results from your NM_INLINE_IMAGE processing code.*/

break;
}

case NM_INLINE_SOUND:
{
OutputDebugString(TEXT("NM_INLINE_SOUND\r\n"));
break;
}

case NM_TITLE:
{
OutputDebugString(TEXT("NM_TITLE\r\n"));
break;
}

case NM_META:
{
OutputDebugString(TEXT("NM_META\r\n"));
break;
}

case NM_BASE:
{
OutputDebugString(TEXT("NM_BASE\r\n"));
break;
}

case NM_CONTEXTMENU:
{
OutputDebugString(TEXT("NM_CONTEXTMENU\r\n"));
break;
}

case NM_INLINE_XML:
{
OutputDebugString(TEXT("NM_INLINE_XML\r\n"));
break;
}

case NM_BEFORENAVIGATE:
{
OutputDebugString(TEXT("NM_BEFORENAVIGATE\r\n"));
break;
}

case NM_DOCUMENTCOMPLETE:
{
OutputDebugString(TEXT("NM_DOCUMENTCOMPLETE\r\n"));
break;
}

case NM_NAVIGATECOMPLETE:
{
OutputDebugString(TEXT("NM_NAVIGATECOMPLETE\r\n"));
break;
}

case NM_TITLECHANGE:
{
OutputDebugString(TEXT("NM_TITLECHANGE\r\n"));
break;
}

default:
{
ASSERT(FALSE);
break;
}
}//switch

DeleteObject(hBitmap);

break;
}

===========================================================
處理NM_INLINE_IMAGE時, 切記要return 一個非零的值, 否則圖片會一閃即逝!!