close
文章出處
用VC做GUI是一件相當鬧心的事,自繪、各種細節調整真的需要程序員很要有耐心,而且當碰到朝令夕改的情況時,那就會讓人抓狂了.現在發現很多軟件的顯示都是基于網頁控件了,這辦法好啊,改界面的事完全可以推給美工了,大善!。MFC提供了類CDHtmlDialog用來顯示HTML對話框.要在非MFC框架下(如WTL)下顯示HTML對話框就要自己動手了.
mshtml.dll中有個ShowHTMLDialog可以顯示HTML對話框,MSDN定義如下:
HRESULT ShowHTMLDialog( HWND hwndParent,
IMoniker *pMk,
VARIANT *pvarArgIn,
WCHAR *pchOptions,
VARIANT *pvarArgOut
);
在此,我結合網上的一些資源,簡單地封裝了它的用法,網頁事件的獲取還沒實現,以后會慢慢補上:
IMoniker *pMk,
VARIANT *pvarArgIn,
WCHAR *pchOptions,
VARIANT *pvarArgOut
);
#include <urlmon.h>
#pragma comment(lib, "urlmon.lib")
/*
* 獲取uIDHtml指定的HTML資源的源碼
*/
inline CString LoadHtmlResource(_U_STRINGorID uIDHtml)
{
HRSRC hrsrc = ::FindResource(ATL::_AtlBaseModule.GetResourceInstance(), uIDHtml.m_lpstr, RT_HTML);
if( hrsrc != NULL )
{
DWORD dwSize = ::SizeofResource(ATL::_AtlBaseModule.GetResourceInstance(), hrsrc);
HGLOBAL hGlobal = ::LoadResource(ATL::_AtlBaseModule.GetResourceInstance(), hrsrc);
if( hGlobal != NULL )
{
LPSTR pszData = (LPSTR) ::LockResource( hGlobal );
if( pszData != NULL )
{
pszData[dwSize] = 0;
CString strHtml = pszData;
UnlockResource( hGlobal );
::FreeResource( hGlobal );
return strHtml;
}
}
}
return CString();
}
class CHtmlDialog
{
private:
HINSTANCE m_hInstMSHTML;
SHOWHTMLDIALOGFN* m_pfnShowHTMLDialog;
public:
CHtmlDialog()
{
m_hInstMSHTML = ::LoadLibrary( _T("MSHTML.DLL") );
ATLASSERT( m_hInstMSHTML != NULL );
m_pfnShowHTMLDialog = (SHOWHTMLDIALOGFN*)::GetProcAddress( m_hInstMSHTML, "ShowHTMLDialog" );
ATLASSERT( m_pfnShowHTMLDialog != NULL );
}
/*
* 根據HTML資源顯示對話框
* hWnd: 指定父窗口
* bstrHtml: HTML源代碼
*/
BOOL ShowHtmlDialog( HWND hWnd, BSTR bstrHtml, VARIANT *pvIn = NULL, BSTR bstrOptions = NULL, VARIANT *pvOut = NULL )
{
ATLASSERT( m_pfnShowHTMLDialog != NULL );
if( m_pfnShowHTMLDialog == NULL )return FALSE;
if( hWnd==NULL ) hWnd = ::GetForegroundWindow();
ATLASSERT( ::IsWindow( hWnd ) );
//CAtlTemporaryFileEx源于我對ATL類CAtlTemporaryFile,詳情請見我的另一篇博文<無語的CAtlTemporaryFile類>
CAtlTemporaryFileEx fileTmp;
fileTmp.Create();
//將HTML源碼寫入臨時文件
fileTmp.Write( COLE2CT( bstrHtml ), wcslen( bstrHtml ) * sizeof( wchar_t ) );
fileTmp.Close();
CComBSTR bstrText(L"file:///");
bstrText += fileTmp.TempFileName();
CComPtr<IMoniker> spMoniker;
HRESULT hr;
hr = ::CreateURLMoniker( NULL, bstrText, &spMoniker );
if( SUCCEEDED(hr) )
{
//顯示HTML對話框
hr = (*m_pfnShowHTMLDialog)( hWnd, spMoniker.p, pvIn, CT2W( COLE2CT( bstrOptions ) ), pvOut );
ATLASSERT( SUCCEEDED(hr) );
spMoniker.Release();
}
return ( hr==S_OK );
}
/*
* 重載ShowHtmlDialog方法,用HTML資源ID作為輸入參數
*/
BOOL ShowHtmlDialog( HWND hWnd, _U_STRINGorID uIDHtml, VARIANT *pvIn = NULL, BSTR bstrOptions = NULL, VARIANT *pvOut = NULL )
{
CString strHtml;
strHtml = LoadHtmlResource( uIDHtml );
return ShowHtmlDialog( hWnd, CT2OLE( strHtml ), pvIn, bstrOptions, pvOut );
}
~CHtmlDialog()
{
if( m_hInstMSHTML != NULL )
{
::FreeLibrary( m_hInstMSHTML );
}
}
};
使用示例:
#pragma comment(lib, "urlmon.lib")
/*
* 獲取uIDHtml指定的HTML資源的源碼
*/
inline CString LoadHtmlResource(_U_STRINGorID uIDHtml)
{
HRSRC hrsrc = ::FindResource(ATL::_AtlBaseModule.GetResourceInstance(), uIDHtml.m_lpstr, RT_HTML);
if( hrsrc != NULL )
{
DWORD dwSize = ::SizeofResource(ATL::_AtlBaseModule.GetResourceInstance(), hrsrc);
HGLOBAL hGlobal = ::LoadResource(ATL::_AtlBaseModule.GetResourceInstance(), hrsrc);
if( hGlobal != NULL )
{
LPSTR pszData = (LPSTR) ::LockResource( hGlobal );
if( pszData != NULL )
{
pszData[dwSize] = 0;
CString strHtml = pszData;
UnlockResource( hGlobal );
::FreeResource( hGlobal );
return strHtml;
}
}
}
return CString();
}
class CHtmlDialog
{
private:
HINSTANCE m_hInstMSHTML;
SHOWHTMLDIALOGFN* m_pfnShowHTMLDialog;
public:
CHtmlDialog()
{
m_hInstMSHTML = ::LoadLibrary( _T("MSHTML.DLL") );
ATLASSERT( m_hInstMSHTML != NULL );
m_pfnShowHTMLDialog = (SHOWHTMLDIALOGFN*)::GetProcAddress( m_hInstMSHTML, "ShowHTMLDialog" );
ATLASSERT( m_pfnShowHTMLDialog != NULL );
}
/*
* 根據HTML資源顯示對話框
* hWnd: 指定父窗口
* bstrHtml: HTML源代碼
*/
BOOL ShowHtmlDialog( HWND hWnd, BSTR bstrHtml, VARIANT *pvIn = NULL, BSTR bstrOptions = NULL, VARIANT *pvOut = NULL )
{
ATLASSERT( m_pfnShowHTMLDialog != NULL );
if( m_pfnShowHTMLDialog == NULL )return FALSE;
if( hWnd==NULL ) hWnd = ::GetForegroundWindow();
ATLASSERT( ::IsWindow( hWnd ) );
//CAtlTemporaryFileEx源于我對ATL類CAtlTemporaryFile,詳情請見我的另一篇博文<無語的CAtlTemporaryFile類>
CAtlTemporaryFileEx fileTmp;
fileTmp.Create();
//將HTML源碼寫入臨時文件
fileTmp.Write( COLE2CT( bstrHtml ), wcslen( bstrHtml ) * sizeof( wchar_t ) );
fileTmp.Close();
CComBSTR bstrText(L"file:///");
bstrText += fileTmp.TempFileName();
CComPtr<IMoniker> spMoniker;
HRESULT hr;
hr = ::CreateURLMoniker( NULL, bstrText, &spMoniker );
if( SUCCEEDED(hr) )
{
//顯示HTML對話框
hr = (*m_pfnShowHTMLDialog)( hWnd, spMoniker.p, pvIn, CT2W( COLE2CT( bstrOptions ) ), pvOut );
ATLASSERT( SUCCEEDED(hr) );
spMoniker.Release();
}
return ( hr==S_OK );
}
/*
* 重載ShowHtmlDialog方法,用HTML資源ID作為輸入參數
*/
BOOL ShowHtmlDialog( HWND hWnd, _U_STRINGorID uIDHtml, VARIANT *pvIn = NULL, BSTR bstrOptions = NULL, VARIANT *pvOut = NULL )
{
CString strHtml;
strHtml = LoadHtmlResource( uIDHtml );
return ShowHtmlDialog( hWnd, CT2OLE( strHtml ), pvIn, bstrOptions, pvOut );
}
~CHtmlDialog()
{
if( m_hInstMSHTML != NULL )
{
::FreeLibrary( m_hInstMSHTML );
}
}
};
TCHAR szHtml[] = _T("<html> \
<head> \
<title>MyHtmlDialog</title> \
</head> \
<body bgcolor=\"#66cc55\"> \
<BUTTON id=BUTTON1 style=\"Z-INDEX: 100; LEFT: 155px; POSITION: absolute; TOP: 73px\">Button</BUTTON> \
</body> \
</html>");
CHtmlDialog dlgHtml;
dlgHtml.ShowHtmlDialog( m_hWnd, CT2OLE( szHtml ) );
dlgHtml.ShowHtmlDialog( m_hWnd, IDR_HTML2 );
<head> \
<title>MyHtmlDialog</title> \
</head> \
<body bgcolor=\"#66cc55\"> \
<BUTTON id=BUTTON1 style=\"Z-INDEX: 100; LEFT: 155px; POSITION: absolute; TOP: 73px\">Button</BUTTON> \
</body> \
</html>");
CHtmlDialog dlgHtml;
dlgHtml.ShowHtmlDialog( m_hWnd, CT2OLE( szHtml ) );
dlgHtml.ShowHtmlDialog( m_hWnd, IDR_HTML2 );
![]() |
不含病毒。www.avast.com |
全站熱搜