📄 sthtmldialog.cpp
字号:
// STHtmlDialog.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "STHtmlDialog.h"
#include <Htmlctrl.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSTHtmlDialog dialog
HINSTANCE CSTHtmlDialog::m_HtmlViewInstance = 0;
CSTHtmlDialog::CSTHtmlDialog(UINT nIDTemplate, CWnd* pParentWnd)
: CDialog(nIDTemplate, pParentWnd)
{
//{{AFX_DATA_INIT(CSTHtmlDialog)
//}}AFX_DATA_INIT
}
BEGIN_MESSAGE_MAP(CSTHtmlDialog, CDialog)
//{{AFX_MSG_MAP(CSTHtmlDialog)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSTHtmlDialog message handlers
BOOL CSTHtmlDialog::OnInitDialog()
{
CDialog::OnInitDialog();
CreateHtmlWindow();
return TRUE;
}
void CSTHtmlDialog::CreateHtmlWindow()
{
if (m_HtmlViewInstance == 0) {
m_HtmlViewInstance = ::LoadLibrary(L"htmlview.dll");
}
VERIFY(InitHTMLControl(AfxGetInstanceHandle()));
DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS;
RECT rect;
GetClientRect(&rect);
m_hwndHtml = ::CreateWindow (DISPLAYCLASS,
NULL,
dwStyle,
rect.left,
rect.top,
rect.right,
rect.bottom,
m_hWnd,
0,
m_HtmlViewInstance,
NULL);
::SetWindowLong(m_hwndHtml, GWL_ID, 12321);
::SetFocus (m_hwndHtml);
::SendMessage(m_hwndHtml, WM_SETTEXT, 0, (LPARAM)(LPCTSTR)_T(""));
}
void CSTHtmlDialog::SetHtml(const CString &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);
}
LRESULT CSTHtmlDialog::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_NOTIFY:
NM_HTMLVIEW * pnmHTML = (NM_HTMLVIEW *) lParam;
LPNMHDR pnmh = (LPNMHDR) &(pnmHTML->hdr);
CString strReg = _T("register");
switch(pnmh->code)
{
case NM_HOTSPOT:
OnLink(pnmHTML->szTarget);
break;
case NM_INLINE_IMAGE:
CString strSrcText = pnmHTML->szTarget;
DWORD dwCookieValue = pnmHTML->dwCookie;
CBitmap* pBitmap;
if (! m_imagesCache.Lookup(strSrcText, (CObject*&)pBitmap)) {
::SendMessage(m_hwndHtml, DTM_IMAGEFAIL, 0, (LPARAM)(LPINLINEIMAGEINFO)dwCookieValue);
break;
}
INLINEIMAGEINFO imageInfo;
imageInfo.dwCookie = dwCookieValue;
imageInfo.bOwnBitmap = FALSE;
imageInfo.hbm = (HBITMAP)(*pBitmap);
CSize size = pBitmap->GetBitmapDimension();
imageInfo.iOrigWidth = size.cx;
imageInfo.iOrigHeight = size.cy;
::SendMessage(m_hwndHtml, DTM_SETIMAGE, 0, (LPARAM)(LPINLINEIMAGEINFO)&imageInfo);
CString data;
data.Format(L"Image width=%d, height=%d, pBitmap=%p, pic name=%s", size.cx, size.cy, pBitmap, (LPCTSTR)strSrcText);
break;
}
}
return CDialog::WindowProc(message, wParam, lParam);
}
void CSTHtmlDialog::RegisterHtmlImage(int nResourceId, const CString &strHtmlImageName)
{
CBitmap* pBitmap = new CBitmap();
pBitmap->LoadBitmap(nResourceId);
m_imagesCache.SetAt(strHtmlImageName, (CObject*)pBitmap);
}
void CSTHtmlDialog::RegisterHtmlImage(CBitmap *pBitmap, const CString &strHtmlImageName)
{
m_imagesCache.SetAt(strHtmlImageName, (CObject*)pBitmap);
}
void CSTHtmlDialog::DeleteHtmlImages()
{
POSITION pos = m_imagesCache.GetStartPosition();
CString key;
CBitmap* bmp;
while (pos != NULL) {
m_imagesCache.GetNextAssoc(pos, key, (CObject*&)bmp);
delete bmp;
m_imagesCache.RemoveKey(key);
}
}
void CSTHtmlDialog::OnLink(const CString &strHref)
{
MessageBox(strHref, _T("Link is clicked"), MB_OK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -