📄 simplebrowser.cpp
字号:
VARIANT parameter;
VariantInit(¶meter);
V_VT(¶meter) = VT_ARRAY | VT_BYREF;
V_ARRAY(¶meter) = parameter_array;
// start printing browser contents
hr = _Browser->ExecWB(OLECMDID_PRINT,OLECMDEXECOPT_DODEFAULT,¶meter,NULL);
// Note: There is no simple way to determine that printing has completed.
// In fact, if the browser is destroyed while printing is in progress,
// only part of the contents will be printed.
// release SAFEARRAY
if (!SUCCEEDED(hr)) {
VariantClear(&header_variant);
VariantClear(&footer_variant);
if (parameter_array != NULL) {
SafeArrayDestroy(parameter_array);
}
}
}
}
// Miscellaneous
bool SimpleBrowser::GetBusy()
{
bool busy = false;
if (_Browser != NULL) {
VARIANT_BOOL variant_bool;
HRESULT hr = _Browser->get_Busy(&variant_bool);
if (SUCCEEDED(hr)) {
busy = (variant_bool == VARIANT_TRUE);
}
}
return busy;
}
CString SimpleBrowser::GetLocationName()
{
CString location_name = _T("");
if (_Browser != NULL) {
BSTR location_name_BSTR = NULL;
HRESULT hr = _Browser->get_LocationName(&location_name_BSTR);
if (SUCCEEDED(hr)) {
location_name = location_name_BSTR;
}
::SysFreeString(location_name_BSTR);
}
return location_name;
}
CString SimpleBrowser::GetLocationURL()
{
CString location_URL = _T("");
if (_Browser != NULL) {
BSTR location_URL_BSTR = NULL;
HRESULT hr = _Browser->get_LocationURL(&location_URL_BSTR);
if (SUCCEEDED(hr)) {
location_URL = location_URL_BSTR;
}
::SysFreeString(location_URL_BSTR);
}
return location_URL;
}
READYSTATE SimpleBrowser::GetReadyState()
{
READYSTATE readystate = READYSTATE_UNINITIALIZED;
if (_Browser != NULL) {
_Browser->get_ReadyState(&readystate);
}
return readystate;
}
bool SimpleBrowser::GetSilent()
{
bool silent = false;
if (_Browser != NULL) {
VARIANT_BOOL silent_variant;
HRESULT hr = _Browser->get_Silent(&silent_variant);
if (SUCCEEDED(hr)) {
silent = (silent_variant == VARIANT_TRUE);
}
}
return silent;
}
void SimpleBrowser::PutSilent(bool silent)
{
if (_Browser != NULL) {
VARIANT_BOOL silent_variant;
if (silent) silent_variant = VARIANT_TRUE;
else silent_variant = VARIANT_FALSE;
_Browser->put_Silent(silent_variant);
}
}
IHTMLDocument2 *SimpleBrowser::GetDocument()
{
IHTMLDocument2 *document = NULL;
if (_Browser != NULL) {
// get browser document's dispatch interface
IDispatch *document_dispatch = NULL;
HRESULT hr = _Browser->get_Document(&document_dispatch);
if (SUCCEEDED(hr) && (document_dispatch != NULL)) {
// get the actual document interface
hr = document_dispatch->QueryInterface(IID_IHTMLDocument2,
(void **)&document);
// release dispatch interface
document_dispatch->Release();
}
}
return document;
}
/////////////////////////////////////////////////////////////////////////////
// Message handlers
/////////////////////////////////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(SimpleBrowser,CWnd)
//{{AFX_MSG_MAP(SimpleBrowser)
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// Resize control window as this window is resized
void SimpleBrowser::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
if (_Browser != NULL) {
CRect rect(0,0,cx,cy);
_BrowserWindow.MoveWindow(&rect);
}
}
/////////////////////////////////////////////////////////////////////////////
// Event handlers
/////////////////////////////////////////////////////////////////////////////
BEGIN_EVENTSINK_MAP(SimpleBrowser,CWnd)
ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
DISPID_BEFORENAVIGATE2,
_OnBeforeNavigate2,
VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
DISPID_DOCUMENTCOMPLETE,
_OnDocumentComplete,
VTS_DISPATCH VTS_PVARIANT)
ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
DISPID_DOWNLOADBEGIN,
_OnDownloadBegin,
VTS_NONE)
ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
DISPID_PROGRESSCHANGE,
_OnProgressChange,
VTS_I4 VTS_I4)
ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
DISPID_DOWNLOADCOMPLETE,
_OnDownloadComplete,
VTS_NONE)
ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
DISPID_NAVIGATECOMPLETE2,
_OnNavigateComplete2,
VTS_DISPATCH VTS_PVARIANT)
ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
DISPID_STATUSTEXTCHANGE,
_OnStatusTextChange,
VTS_BSTR)
ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
DISPID_TITLECHANGE,
_OnTitleChange,
VTS_BSTR)
END_EVENTSINK_MAP()
SimpleBrowser::Notification::Notification(HWND hwnd,UINT ID,NotificationType type)
{
hdr.hwndFrom = hwnd;
hdr.idFrom = ID;
hdr.code = type;
URL = _T("");
frame = _T("");
post_data = NULL;
post_data_size = 0;
headers = _T("");
progress = 0;
progress_max = 0;
text = _T("");
}
// Called before navigation begins; application may cancel if required
void SimpleBrowser::_OnBeforeNavigate2(LPDISPATCH lpDisp,
VARIANT FAR *URL,
VARIANT FAR *Flags,
VARIANT FAR *TargetFrameName,
VARIANT FAR *PostData,
VARIANT FAR *Headers,
VARIANT_BOOL *Cancel)
{
Unused(Flags); // Note: flags value is reserved
if (lpDisp == _BrowserDispatch) {
CString URL_string;
CString frame;
unsigned char *post_data = NULL;
int post_data_size = 0;
CString headers;
if ((URL != NULL) &&
(V_VT(URL) == VT_BSTR)) {
URL_string = V_BSTR(URL);
}
if ((TargetFrameName != NULL) &&
(V_VT(TargetFrameName) == VT_BSTR)) {
frame = V_BSTR(TargetFrameName);
}
if ((PostData != NULL) &&
(V_VT(PostData) == (VT_VARIANT | VT_BYREF))) {
VARIANT *PostData_variant = V_VARIANTREF(PostData);
if ((PostData_variant != NULL) &&
(V_VT(PostData_variant) != VT_EMPTY)) {
SAFEARRAY *PostData_safearray = V_ARRAY(PostData_variant);
if (PostData_safearray != NULL) {
char *post_data_array = NULL;
SafeArrayAccessData(PostData_safearray,(void HUGEP **)&post_data_array);
long lower_bound = 1;
long upper_bound = 1;
SafeArrayGetLBound(PostData_safearray,1,&lower_bound);
SafeArrayGetUBound(PostData_safearray,1,&upper_bound);
post_data_size = (int)(upper_bound - lower_bound + 1);
post_data = new unsigned char[post_data_size];
memcpy(post_data,post_data_array,post_data_size);
SafeArrayUnaccessData(PostData_safearray);
}
}
}
bool cancel = OnBeforeNavigate2(URL_string,
frame,
post_data,post_data_size,
headers);
if (Cancel != NULL) {
if (cancel) *Cancel = VARIANT_TRUE;
else *Cancel = VARIANT_FALSE;
}
delete []post_data;
}
}
bool SimpleBrowser::OnBeforeNavigate2(CString URL,
CString frame,
void *post_data,int post_data_size,
CString headers)
{
bool cancel = false;
CWnd *parent = GetParent();
if (parent != NULL) {
Notification notification(m_hWnd,GetDlgCtrlID(),BeforeNavigate2);
notification.URL = URL;
notification.frame = frame;
notification.post_data = post_data;
notification.post_data_size = post_data_size;
notification.headers = headers;
LRESULT result = parent->SendMessage(WM_NOTIFY,
notification.hdr.idFrom,
(LPARAM)¬ification);
if (result) {
cancel = true;
}
}
return cancel;
}
// Document loaded and initialized
void SimpleBrowser::_OnDocumentComplete(LPDISPATCH lpDisp,VARIANT *URL)
{
if (lpDisp == _BrowserDispatch) {
CString URL_string;
if ((URL != NULL) &&
(V_VT(URL) == VT_BSTR)) {
URL_string = V_BSTR(URL);
}
OnDocumentComplete(URL_string);
}
}
void SimpleBrowser::OnDocumentComplete(CString URL)
{
CWnd *parent = GetParent();
if (parent != NULL) {
Notification notification(m_hWnd,GetDlgCtrlID(),DocumentComplete);
notification.URL = URL;
LRESULT result = parent->SendMessage(WM_NOTIFY,
notification.hdr.idFrom,
(LPARAM)¬ification);
}
}
// Navigation/download progress
void SimpleBrowser::_OnDownloadBegin()
{
OnDownloadBegin();
}
void SimpleBrowser::OnDownloadBegin()
{
CWnd *parent = GetParent();
if (parent != NULL) {
Notification notification(m_hWnd,GetDlgCtrlID(),DownloadBegin);
LRESULT result = parent->SendMessage(WM_NOTIFY,
notification.hdr.idFrom,
(LPARAM)¬ification);
}
}
void SimpleBrowser::_OnProgressChange(long progress,long progress_max)
{
OnProgressChange((int)progress,(int)progress_max);
}
void SimpleBrowser::OnProgressChange(int progress,int progress_max)
{
CWnd *parent = GetParent();
if (parent != NULL) {
Notification notification(m_hWnd,GetDlgCtrlID(),ProgressChange);
notification.progress = progress;
notification.progress_max = progress_max;
LRESULT result = parent->SendMessage(WM_NOTIFY,
notification.hdr.idFrom,
(LPARAM)¬ification);
}
}
void SimpleBrowser::_OnDownloadComplete()
{
OnDownloadComplete();
}
void SimpleBrowser::OnDownloadComplete()
{
CWnd *parent = GetParent();
if (parent != NULL) {
Notification notification(m_hWnd,GetDlgCtrlID(),DownloadComplete);
LRESULT result = parent->SendMessage(WM_NOTIFY,
notification.hdr.idFrom,
(LPARAM)¬ification);
}
}
// Navigation to a link has completed
void SimpleBrowser::_OnNavigateComplete2(LPDISPATCH lpDisp,VARIANT *URL)
{
if (lpDisp == _BrowserDispatch) {
CString URL_string;
if ((URL != NULL) &&
(V_VT(URL) == VT_BSTR)) {
URL_string = V_BSTR(URL);
}
OnNavigateComplete2(URL_string);
}
}
void SimpleBrowser::OnNavigateComplete2(CString URL)
{
CWnd *parent = GetParent();
if (parent != NULL) {
Notification notification(m_hWnd,GetDlgCtrlID(),NavigateComplete2);
notification.URL = URL;
LRESULT result = parent->SendMessage(WM_NOTIFY,
notification.hdr.idFrom,
(LPARAM)¬ification);
}
}
// Status text has changed
void SimpleBrowser::_OnStatusTextChange(BSTR bstrText)
{
CString text;
if (bstrText != NULL) {
text = (LPCTSTR)bstrText;
}
OnStatusTextChange(text);
}
void SimpleBrowser::OnStatusTextChange(CString text)
{
CWnd *parent = GetParent();
if (parent != NULL) {
Notification notification(m_hWnd,GetDlgCtrlID(),StatusTextChange);
notification.text = text;
LRESULT result = parent->SendMessage(WM_NOTIFY,
notification.hdr.idFrom,
(LPARAM)¬ification);
}
}
// Title text has changed
void SimpleBrowser::_OnTitleChange(BSTR bstrText)
{
CString text;
if (bstrText != NULL) {
text = (LPCTSTR)bstrText;
}
OnTitleChange(text);
}
void SimpleBrowser::OnTitleChange(CString text)
{
CWnd *parent = GetParent();
if (parent != NULL) {
Notification notification(m_hWnd,GetDlgCtrlID(),TitleChange);
notification.text = text;
LRESULT result = parent->SendMessage(WM_NOTIFY,
notification.hdr.idFrom,
(LPARAM)¬ification);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -