www.pudn.com > holder.zip > HolderView.cpp


// HolderView.cpp : implementation of the CHolderView class 
// 
 
#include "stdafx.h" 
#include "Holder.h" 
 
#include "HolderDoc.h" 
#include "HolderView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CHolderView 
 
IMPLEMENT_DYNCREATE(CHolderView, CHtmlView) 
 
BEGIN_MESSAGE_MAP(CHolderView, CHtmlView) 
	//{{AFX_MSG_MAP(CHolderView) 
	ON_UPDATE_COMMAND_UI(ID_VIEW_ALLOWIECONTEXTMENU, OnUpdateViewAllowiecontextmenu) 
	ON_COMMAND(ID_VIEW_ALLOWIECONTEXTMENU, OnViewAllowiecontextmenu) 
	//}}AFX_MSG_MAP 
	// Standard printing commands 
	ON_COMMAND(ID_FILE_PRINT, CHtmlView::OnFilePrint) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CHolderView construction/destruction 
 
CHolderView::CHolderView() 
{ 
	EnableAutomation(); 
 
	//SAMPLE: initialize m_bAllowContextMenu to disallow menu 
	m_bAllowContextMenu = FALSE; 
} 
 
CHolderView::~CHolderView() 
{ 
} 
 
BOOL CHolderView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CHtmlView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CHolderView drawing 
 
void CHolderView::OnDraw(CDC* pDC) 
{ 
	CHolderDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
} 
 
void CHolderView::OnInitialUpdate() 
{ 
	CHtmlView::OnInitialUpdate(); 
 
	//SAMPLE: here, we're going to navigate to the current directory 
	// to find a file calle PAGE.HTM. That'll load the HTML page we 
	// came up with. Obviously, I equally could've put this into a 
	// real live webpage or into this program's resources. 
 
	TCHAR sz[1024]; 
	GetCurrentDirectory(1024, sz); 
 
	CString str(_T("file://")); 
	str += sz; 
	str += _T("\\page.htm"); 
 
	Navigate2(str, NULL, NULL); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CHolderView printing 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CHolderView diagnostics 
 
#ifdef _DEBUG 
void CHolderView::AssertValid() const 
{ 
	CHtmlView::AssertValid(); 
} 
 
void CHolderView::Dump(CDumpContext& dc) const 
{ 
	CHtmlView::Dump(dc); 
} 
 
CHolderDoc* CHolderView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHolderDoc))); 
	return (CHolderDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CHolderView message handlers 
 
//SAMPLE: Override create. This code does exactly what the MFC-supplied 
// version would have, except it hooks up our own special 
// COccManager-derived class, named CMyManager. 
 
BOOL CHolderView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName,  
						 DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, 
						 UINT nID, CCreateContext* pContext)  
{ 
	// create the view window itself 
	m_pCreateContext = pContext; 
	if (!CView::Create(lpszClassName, lpszWindowName, 
				dwStyle, rect, pParentWnd,  nID, pContext)) 
	{ 
		return FALSE; 
	} 
 
	//SAMPLE: these three lines are the big change... 
	CHolderApp* pApp = (CHolderApp*) AfxGetApp(); 
	COccManager* pManager = pApp->GetManager(); 
	AfxEnableControlContainer(pManager); 
 
	RECT rectClient; 
	GetClientRect(&rectClient); 
 
	// create the control window 
	// AFX_IDW_PANE_FIRST is a safe but arbitrary ID 
	if (!m_wndBrowser.CreateControl(CLSID_WebBrowser, lpszWindowName, 
				WS_VISIBLE | WS_CHILD, rectClient, this, AFX_IDW_PANE_FIRST)) 
	{ 
		DestroyWindow(); 
		return FALSE; 
	} 
 
	//SAMPLE: the window we've created had better support IWebBrowser2. 
	// If it doesn't, something is terribly wrong and we're in deep 
	// trouble. 
 
	LPUNKNOWN lpUnk = m_wndBrowser.GetControlUnknown(); 
	HRESULT hr = lpUnk->QueryInterface(IID_IWebBrowser2, (void**) &m_pBrowserApp); 
	if (!SUCCEEDED(hr)) 
	{ 
		m_pBrowserApp = NULL; 
		m_wndBrowser.DestroyWindow(); 
		DestroyWindow(); 
		return FALSE; 
	} 
 
	return TRUE; 
} 
 
void CHolderView::Bing(long nNumber)  
{ 
	//SAMPLE: here's one of the two methods in the dispatch 
	// we've decided to implement. We'll just format a string 
	// from the number and show it to the user in a message box. 
	 
	// TODO: Add your dispatch handler code here 
	CString str; 
	str.Format(_T("Bing! Param = %d"), nNumber); 
	MessageBox(str); 
} 
 
//SAMPLE: for OnClick, we're expecting a pointer to the IDispatch of 
// the  of the page. We'll QI that dispinterface for the 
// IHTMLBodyElement used by DHTML. If the DHTML interface is available, 
// we'll set the background color of that object to the variant 
// we receive. Because the script code on the page we have just passes 
// a string for the colour name, we'll accept a VARIANT. The put_bgColor() 
// implementation will accept a variant with a standard colour name 
// in a BSTR--we don't have to do any conversions ourselves. 
 
void CHolderView::OnClick(LPDISPATCH pdispBody, LPVARIANT pvarColor) 
{ 
	CComQIPtr spBody(pdispBody); 
	if (spBody != NULL) 
		spBody->put_bgColor(*pvarColor); 
} 
 
//SAMPLE: here's where we've actually hooked up the Bing and OnClick 
// functions to the dispatch map we're offering. You can add more 
// functions here at will! 
 
BEGIN_DISPATCH_MAP(CHolderView, CHtmlView) 
	//{{AFX_DISPATCH_MAP(HolderView) 
	DISP_FUNCTION(CHolderView, "Bing", Bing, VT_EMPTY, VTS_I4) 
	DISP_FUNCTION(CHolderView, "OnClick", OnClick, VT_EMPTY, VTS_DISPATCH VTS_VARIANT) 
	//}}AFX_DISPATCH_MAP 
END_DISPATCH_MAP() 
 
// Note: we add support for IID_IOtto to support typesafe binding 
//  from VBA.  This IID must match the GUID that is attached to the  
//  dispinterface in the .ODL file. 
 
// {9FD0877D-57E0-11D2-BF25-00A0C91E8585} 
static const IID IID_IHolder = 
{ 0x9fd0877d, 0x57e0, 0x11d2, { 0xbf, 0x25, 0x0, 0xa0, 0xc9, 0x1e, 0x85, 0x85 } }; 
 
BEGIN_INTERFACE_MAP(CHolderView, CHtmlView) 
	INTERFACE_PART(CHolderView, IID_IHolder, Dispatch) 
END_INTERFACE_MAP() 
 
//SAMPLE: here, we'll just toggle (and reflect) the state of the 
// m_bAllowContextMenu variable. 
 
void CHolderView::OnUpdateViewAllowiecontextmenu(CCmdUI* pCmdUI)  
{ 
	pCmdUI->SetRadio(m_bAllowContextMenu); 
} 
 
void CHolderView::OnViewAllowiecontextmenu()  
{ 
	m_bAllowContextMenu = !m_bAllowContextMenu; 
}