⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wingrvw.cpp

📁 API经典入门
💻 CPP
字号:
// wingrvw.cpp : implementation of the CWingrepView class
//

#include "stdafx.h"
#include "wingrep.h"

#include "wingrdoc.h"
#include "cntritem.h"
#include "wingrvw.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWingrepView

IMPLEMENT_DYNCREATE(CWingrepView, CView)

BEGIN_MESSAGE_MAP(CWingrepView, CView)
	//{{AFX_MSG_MAP(CWingrepView)
	ON_WM_SETFOCUS()
	ON_WM_SIZE()
	ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
	ON_COMMAND(ID_CANCEL_EDIT, OnCancelEdit)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_SETCURSOR()
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWingrepView construction/destruction

CWingrepView::CWingrepView()
{
	// TODO: add construction code here
}

CWingrepView::~CWingrepView()
{
}

/////////////////////////////////////////////////////////////////////////////
// CWingrepView drawing

void CWingrepView::OnDraw(CDC* pDC)
{
	CWingrepDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
	// TODO: also draw all OLE items in the document

	// Draw the selection at an arbitrary position.  This code should be
	//  removed once your real drawing code is implemented.  This position
	//  corresponds exactly to the rectangle returned by CWingrepCntrItem,
	//  to give the effect of in-place editing.

	// TODO: remove this code when final draw code is complete.

	// **** This code causes the ghost image that you see with
	// **** the default AppWizard application.
	//if (m_pSelection == NULL)
	//{
	//	POSITION pos = pDoc->GetStartPosition();
	//	m_pSelection = (CWingrepCntrItem*)pDoc->GetNextClientItem(pos);
	//}
	//if (m_pSelection != NULL)
	//	m_pSelection->Draw(pDC, CRect(10, 10, 210, 210));
	
	// **** What we want is an intellegent drawing routine.
	
	// draw the OLE items from the list
	POSITION pos = pDoc->GetStartPosition();
	while (pos != NULL)
	{
		// draw the item
		CWingrepCntrItem* pItem = (CWingrepCntrItem*)pDoc->GetNextItem(pos);
		pItem->Draw(pDC, pItem->m_rectPosition);
		
		// draw the tracker over the item
		CRectTracker tracker;
		CRect rectTrue;
		SetupTracker(&tracker, pItem, &rectTrue);
		tracker.Draw(pDC);
	}
}

void CWingrepView::OnInitialUpdate()
{
	CView::OnInitialUpdate();

	// TODO: remove this code when final selection model code is written
	m_pSelection = NULL;    // initialize selection

}

/////////////////////////////////////////////////////////////////////////////
// OLE Client support and commands

CWingrepCntrItem* CWingrepView::GetItemContaining(CPoint point)
{
	CWingrepDoc* pDoc = GetDocument();
	CWingrepCntrItem* pTestItem;
	CWingrepCntrItem* pHitItem = NULL;
	
	// Initialize the iterator.
	POSITION pos = pDoc->GetStartPosition();
	while (pos != NULL)
	{
		// Iterate through the list, testing each item.
		pTestItem = (CWingrepCntrItem*)pDoc->GetNextItem(pos);
		if (pTestItem->m_rectPosition.PtInRect(point))
			pHitItem = pTestItem;
	}
	
	// Return the last item hit (this corresponds to 
	// the topmost item containing the point).
	return pHitItem; 
}

void CWingrepView::SetSelection (CWingrepCntrItem* pNewSelection)
{
	CWingrepDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// If the new selection is already the current 
	// selection there is nothing for us to do.
	if ( pNewSelection == m_pSelection )
		return;
		
	// Get the currently active item (if any).
	COleClientItem* pActiveItem = 
		pDoc->GetInPlaceActiveItem(this);
		
	// If there is no active item, there is nothing
	// to close.  If the active item is the "new"
	// selection there is nothing to do.
	if ( (pActiveItem != NULL) && 
		 (pActiveItem != pNewSelection) )
	{
		// Close the active item and make sure it closed.
		pActiveItem->Close();
		ASSERT(pDoc->GetInPlaceActiveItem(this) == NULL);
	}
		
	// Tell the view that it needs to be redrawn.
 	Invalidate();
 	
 	// And finally, update the selection.
	m_pSelection = pNewSelection;
}

void CWingrepView::SetupTracker( CRectTracker* pTracker, 
		CWingrepCntrItem* pItem, CRect* pTrueRect )
{
	ASSERT(pTracker != NULL);
	ASSERT(pItem != NULL);

	// Set minimum size for tracker.
	pTracker->m_sizeMin.cx = 8;
	pTracker->m_sizeMin.cy = 8;

	// Initialize style to 0.	
	pTracker->m_nStyle = 0;

	// Initialize the tracker rectangle to our
	// client item's rectangle.
	pTracker->m_rect = pItem->m_rectPosition;

	// Set the tracker's style based on the 
	// nature of the client-item.

	// if the client-item is currently selected
	// let the tracker show it by displaying the
	// resize handles.
	if (pItem == m_pSelection)
		pTracker->m_nStyle |= CRectTracker::resizeInside;
		
	// Set the border sytle based on the item type.
	if (pItem->GetType() == OT_LINK)
		pTracker->m_nStyle |= CRectTracker::dottedLine;
	else
		pTracker->m_nStyle |= CRectTracker::solidLine;

	// Draw the item's interior hatched if the item is
	// open or active -- either way the real drawing will
	// be done by the server.  The client-item shouldn't
	// show anything.		
	if ( (pItem->GetItemState() == COleClientItem::openState) ||
		 (pItem->GetItemState() == COleClientItem::activeUIState) )
	{
		pTracker->m_nStyle |= CRectTracker::hatchInside;
	}
	
	// Send back the true size of the tracker via pTrueRect.
	if (pTrueRect != NULL)
		pTracker->GetTrueRect(pTrueRect);
}

BOOL CWingrepView::IsSelected(const CObject* pDocItem) const
{
	// The implementation below is adequate if your selection consists of
	//  only CWingrepCntrItem objects.  To handle different selection
	//  mechanisms, the implementation here should be replaced.

	// TODO: implement this function that tests for a selected OLE client item

	return pDocItem == m_pSelection;
}

void CWingrepView::OnInsertObject()
{
	// Invoke the standard Insert Object dialog box to obtain information
	//  for new CWingrepCntrItem object.
	COleInsertDialog dlg;
	if (dlg.DoModal() != IDOK)
		return;

	BeginWaitCursor();

	CWingrepCntrItem* pItem = NULL;
	TRY
	{
		// Create new item connected to this document.
		CWingrepDoc* pDoc = GetDocument();
		ASSERT_VALID(pDoc);
		pItem = new CWingrepCntrItem(pDoc);
		ASSERT_VALID(pItem);

		// Initialize the item from the dialog data.
		if (!dlg.CreateItem(pItem))
			AfxThrowMemoryException();  // any exception will do
		ASSERT_VALID(pItem);

		// If item created from class list (not from file) then launch
		//  the server to edit the item.
		if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
			pItem->DoVerb(OLEIVERB_SHOW, this);

		ASSERT_VALID(pItem);

		// As an arbitrary user interface design, this sets the selection
		//  to the last item inserted.

		// TODO: reimplement selection as appropriate for your application

		m_pSelection = pItem;   // set selection to last inserted item
		pDoc->UpdateAllViews(NULL);
	}
	CATCH(CException, e)
	{
		if (pItem != NULL)
		{
			ASSERT_VALID(pItem);
			pItem->Delete();
		}
		AfxMessageBox(IDP_FAILED_TO_CREATE);
	}
	END_CATCH

	EndWaitCursor();
}

// The following command handler provides the standard keyboard
//  user interface to cancel an in-place editing session.
void CWingrepView::OnCancelEdit()
{
	// Close any in-place active item on this view.
	COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
	if (pActiveItem != NULL)
	{
		pActiveItem->Close();
	}
	ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
}

// Special handling of OnSetFocus and OnSize are required for a container
//  when an object is being edited in-place.
void CWingrepView::OnSetFocus(CWnd* pOldWnd)
{
	COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
	if (pActiveItem != NULL &&
		pActiveItem->GetItemState() == COleClientItem::activeUIState)
	{
		// need to set focus to this item if it is in the same view
		CWnd* pWnd = pActiveItem->GetInPlaceWindow();
		if (pWnd != NULL)
		{
			pWnd->SetFocus();   // don't call the base class
			return;
		}
	}

	CView::OnSetFocus(pOldWnd);
}

void CWingrepView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);
	COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
	if (pActiveItem != NULL)
		pActiveItem->SetItemRects();
}

/////////////////////////////////////////////////////////////////////////////
// CWingrepView diagnostics

#ifdef _DEBUG
void CWingrepView::AssertValid() const
{
	CView::AssertValid();
}

void CWingrepView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CWingrepDoc* CWingrepView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CWingrepDoc)));
	return (CWingrepDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CWingrepView message handlers

void CWingrepView::OnLButtonDown(UINT nFlags, CPoint point)
{
	CWingrepCntrItem* pHitItem = GetItemContaining(point);
	SetSelection(pHitItem);
	
	if (pHitItem != NULL)
	{
		// Initialize the tracker.
		CRectTracker tracker;
		CRect rectTrue;
		SetupTracker(&tracker, pHitItem, &rectTrue);
		
		// Update the view before invoking Track().
		UpdateWindow();
		// Hand-over control to the tracker.
		if (tracker.Track(this, point))
		{
			// If the tracker succeeds, save the new position
			// and tell the document and view that things
			// may have changed.
			pHitItem->m_rectPosition = tracker.m_rect;
			GetDocument()->SetModifiedFlag();
			Invalidate();
		}
	}
	
	// Finish up by calling the base class.
	CView::OnLButtonDown(nFlags, point);
}

void CWingrepView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	// Take care of the selection process by calling
	// the single button-click code.
	OnLButtonDown(nFlags, point);
	
	// If the user double-clicked on a client-item,
	// then send it an appropriate OLE verb command.
	if (m_pSelection != NULL)
	{
		LONG oleVerb;
		
		// If the control key is pressed when the double-click
		// occurs, force the server to be fully open.  Otherwise,
		// activate the server using its primary verb.
		
		if (GetKeyState(VK_CONTROL) < 0)
			oleVerb = OLEIVERB_OPEN;
		else
			oleVerb = OLEIVERB_PRIMARY;

		m_pSelection->DoVerb(oleVerb, this);
	}
	
	// Finish by calling the base class.
	CView::OnLButtonDblClk(nFlags, point);
}

BOOL CWingrepView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	if ( (pWnd == this) && (m_pSelection != NULL) )
	{
		// give the tracker for the selection a chance
		CRectTracker tracker;
		CRect rectTrue;
		SetupTracker(&tracker, m_pSelection, &rectTrue);
		if (tracker.SetCursor(this, nHitTest))
			return TRUE;
	}
	
	return CView::OnSetCursor(pWnd, nHitTest, message);
}

/////////////////////////////////////////////////////////////////////////////
// CWingrepView drag drop support functions

int CWingrepView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_dropTarget.Register(this);
	
	return 0;
}

DROPEFFECT CWingrepView::OnDragEnter(COleDataObject* pDataObject,
		DWORD dwKeyState, CPoint point)
{
	TRACE0("OnDragEnter called!\n");
	return OnDragOver(pDataObject, dwKeyState, point);
}

void CWingrepView::OnDragLeave()
{
	TRACE0("OnDragLeave called!\n");
	// stubbed
}

DROPEFFECT CWingrepView::OnDragOver(COleDataObject* pDataObject,
		DWORD dwKeyState, CPoint point)
{
	CWingrepDoc* pDoc = GetDocument();
	CLIPFORMAT clipFormat = pDoc->m_fetcTransferProtocol.cfFormat;
	DROPEFFECT de = DROPEFFECT_NONE;
	
	// Don't put trace call here since it will be called
	// continuously while the object is over the window.
	//TRACE0("OnDragOver called!\n");

	if ( GetItemContaining(point) == NULL)
		return de;
	
	if ( pDataObject->IsDataAvailable(clipFormat,
			&pDoc->m_fetcTransferProtocol) )
	{
		TRACE0("Dropped format is acceptable!\n");
		de = DROPEFFECT_COPY;
	}
	
	return de;
}

BOOL CWingrepView::OnDrop(COleDataObject* pDataObject,
		DROPEFFECT dropEffect, CPoint point)
{
	TRACE0("OnDrop called!\n");
	
	CWingrepDoc* pDoc = GetDocument();
	CLIPFORMAT clipFormat = pDoc->m_fetcTransferProtocol.cfFormat;
	HGLOBAL hgData;
	char FAR* lpszData;
	CString strFileName;
	//STGMEDIUM stgMed;
	
	if ( pDataObject->IsDataAvailable(clipFormat,
			&pDoc->m_fetcTransferProtocol) )
	{
		// Dropped format is acceptable!
		//pDataObject->GetData(clipFormat, &stgMed,
		//		&pDoc->m_fetcTransferProtocol);
		//hgData = stgMed.hGlobal;
		hgData = pDataObject->GetGlobalData(clipFormat,
				&pDoc->m_fetcTransferProtocol);
DWORD dwSize = GlobalSize(hgData);
TRACE1("Size of global memory block = %ld.\n",dwSize);
		if (hgData == NULL)
		{
			TRACE0("GetGlobalData() failed!\n");
			return FALSE;
		}
		lpszData = (char FAR*)GlobalLock(hgData);
		if (lpszData == NULL)
		{
			TRACE0("GlobalLock() failed!\n");
			return FALSE;
		}
		
		TRACE1("The filename transferred is : %Fs.\n", lpszData);
		GlobalUnlock(hgData);
		
		return TRUE;
	}
	
	return FALSE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -