// MainDlg.cpp : implementation of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include "MainDlg.h"
#include "AboutDlg.h"
LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
// center the dialog on the screen
CenterWindow();
// set icons
HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME),
IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
SetIcon(hIcon, TRUE);
HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME),
IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
SetIcon(hIconSmall, FALSE);
//查询得到IWebBrowser2接口
CAxWindow wndIE = GetDlgItem(IDC_IE);
if (wndIE.IsWindow())
{
wndIE.QueryControl(&m_spWebBrowser);
}
NavigateTo(L"http://www.baidu.com");
return TRUE;
}
void CMainDlg::OnClose()
{
EndDialog(0);
return;
}
void CMainDlg::OnGo(UINT uNotifyCode, int nID, CWindow wndCtl)
{
WCHAR szUrl[MAX_PATH] = {0};
if (GetDlgItemText(IDE_URL, szUrl, MAX_PATH))
{
NavigateTo(szUrl);
}
}
void CMainDlg::OnBack(UINT uNotifyCode, int nID, CWindow wndCtl)
{
if (m_spWebBrowser)
{
m_spWebBrowser->GoBack();
}
}
void CMainDlg::OnForward(UINT uNotifyCode, int nID, CWindow wndCtl)
{
if (m_spWebBrowser)
{
m_spWebBrowser->GoForward();
}
}
void CMainDlg::OnStop(UINT uNotifyCode, int nID, CWindow wndCtl)
{
if (m_spWebBrowser)
{
m_spWebBrowser->Stop();
}
}
void CMainDlg::OnRefresh(UINT uNotifyCode, int nID, CWindow wndCtl)
{
CComVariant vLevel;
if (m_spWebBrowser)
{
vLevel = REFRESH_COMPLETELY;
m_spWebBrowser->Refresh2(&vLevel);
}
}
void CMainDlg::OnAbout(UINT uNotifyCode, int nID, CWindow wndCtl)
{
CAboutDlg dlg;
dlg.DoModal();
}
void CMainDlg::NavigateTo(LPCWSTR pszUrl)
{
if (m_spWebBrowser)
{
CComVariant v;
m_spWebBrowser->Navigate(CComBSTR(pszUrl), &v, &v, &v, &v);
}
}
void __stdcall CMainDlg::OnDownloadBegin()
{
OutputDebugString(L"OnDownloadBegin");
}
void __stdcall CMainDlg::OnDownloadComplete()
{
OutputDebugString(L"OnDownloadComplete");
}
void __stdcall CMainDlg::OnBeforeNavigate(LPDISPATCH pDisp, VARIANT* URL, VARIANT* Flags, VARIANT* TargetFrameName, VARIANT* PostData, VARIANT* Headers, BOOL* Cancel)
{
if (URL)
{
CString strUrl;
strUrl.Format(L"OnBeforeNavigate %s", URL->bstrVal);
OutputDebugString(strUrl);
//可指定Cancel来阻止访问
if (strUrl.CompareNoCase(L"OnBeforeNavigate http://www.sina.com/")==0)
{
*Cancel = TRUE;
OutputDebugString(L"http://www.sina.com/ is Canceled!");
}
}
}
void __stdcall CMainDlg::OnNavigateComplete(LPDISPATCH pDisp, VARIANT* URL)
{
if (URL)
{
CString strUrl;
strUrl.Format(L"OnNavigateComplete %s", URL->bstrVal);
OutputDebugString(strUrl);
}
}
void __stdcall CMainDlg::OnDocumentComplete(LPDISPATCH pDisp, VARIANT* URL)
{
if (URL)
{
CString strUrl;
strUrl.Format(L"OnDocumentCompleteIe %s", URL->bstrVal);
OutputDebugString(strUrl);
}
}