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

📄 viewtestview.cpp

📁 实现标尺控件, 类似WORD和VISIO中的标尺, 可用于SDI或MDI程序
💻 CPP
字号:
// ViewTestView.cpp : implementation of the CViewTestView class
//

#include "stdafx.h"
#include "ViewTest.h"

#include "ViewTestDoc.h"
#include "CntrItem.h"
#include "ViewTestView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

const int m_LowerBound  = 50;
const int m_UpperBound  = 250;

/////////////////////////////////////////////////////////////////////////////
// CViewTestView
#define ID_VERTSCROLLBAR 0x4178445
#define ID_HORZSCROLLBAR 0x2a7c534

#define ID_HORZ 0x12
#define ID_VERT 0x34

IMPLEMENT_DYNCREATE(CViewTestView, CView)

BEGIN_MESSAGE_MAP(CViewTestView, CView)
	//{{AFX_MSG_MAP(CViewTestView)
	ON_WM_DESTROY()
	ON_WM_SETFOCUS()
	ON_WM_SIZE()
	ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
	ON_COMMAND(ID_CANCEL_EDIT_CNTR, OnCancelEditCntr)
	ON_WM_CREATE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_SETCURSOR()
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

BEGIN_EVENTSINK_MAP(CViewTestView, CView)
    //{{AFX_EVENTSINK_MAP(CTestRulerDlg)
	ON_EVENT(CViewTestView, ID_HORZ, 1 /* StartTracking */, OnStartTrackingRulerHorz, VTS_I2 VTS_I4 VTS_I4)
	ON_EVENT(CViewTestView, ID_HORZ, 2 /* StopTracking */, OnStopTrackingRulerHorz, VTS_I2 VTS_I4 VTS_I4)
	ON_EVENT(CViewTestView, ID_HORZ, 3 /* Track */, OnTrackRulerHorz, VTS_I2 VTS_I4 VTS_I4)
	ON_EVENT(CViewTestView, ID_VERT, 4 /* StartTracking */, OnStartTrackingRulerVert, VTS_I2 VTS_I4 VTS_I4)
	ON_EVENT(CViewTestView, ID_VERT, 5 /* StopTracking */, OnStopTrackingRulerVert, VTS_I2 VTS_I4 VTS_I4)
	ON_EVENT(CViewTestView, ID_VERT, 6 /* Track */, OnTrackRulerVert, VTS_PI4)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
/////////////////////////////////////////////////////////////////////////////
// CViewTestView construction/destruction

CViewTestView::CViewTestView()
{
	m_pSelection = NULL;
	// TODO: add construction code here
    m_bTracking = FALSE;
	m_pCurLine = NULL;
}

CViewTestView::~CViewTestView()
{
  for(POSITION nPos = m_HorzLines.GetHeadPosition();nPos != NULL;)
	  delete m_HorzLines.GetNext(nPos);

  for(nPos = m_VertLines.GetHeadPosition(); nPos != NULL;)
	  delete m_VertLines.GetNext(nPos);
}

BOOL CViewTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
   cs.style |= WS_CLIPCHILDREN;
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CViewTestView drawing

void CViewTestView::OnDraw(CDC* pDC)
{
	//CViewTestDoc* pDoc = GetDocument();
	//ASSERT_VALID(pDoc);
	CRect rc;
	GetClientRect(&rc);
    CDC mdc;
	mdc.CreateCompatibleDC(pDC);
    CBitmap bmp;
	bmp.CreateCompatibleBitmap(pDC,rc.Width(),rc.Height());
	CBitmap* pOldBmp = mdc.SelectObject(&bmp);

	CBrush bkgrnd;
	bkgrnd.CreateSolidBrush(RGB(255,255,255));
	mdc.FillRect(&rc,&bkgrnd);
	bkgrnd.DeleteObject();
	
	CPen lightBlue;
	lightBlue.CreatePen(PS_DOT,1,RGB(159,140,236));
	CPen* pOldPen = mdc.SelectObject(&lightBlue);
	
	for(POSITION nPos = m_VertLines.GetHeadPosition(); nPos != NULL;)
	{
      CViewTestView::_RuleLine* pLine = m_VertLines.GetNext(nPos);
	  if(pLine->nFlag == 0)
	  {
	    mdc.MoveTo(pLine->m_nRect.left+4,0);
	    mdc.LineTo(pLine->m_nRect.left+4,200);
	  }
	  else
	  if(pLine->nFlag == 1)
	  {
	    mdc.MoveTo(pLine->m_nRect.right,0);
	    mdc.LineTo(pLine->m_nRect.right,200);
	  }
	  else
	  {
	    mdc.MoveTo(pLine->m_nRect.left,0);
	    mdc.LineTo(pLine->m_nRect.left,200);
	  }
	}

	if(m_pCurLine != NULL)
	{
      CPen blackPen;
	  blackPen.CreatePen(PS_DOT,1,RGB(0,0,0));
	  CPen* pOldpen = mdc.SelectObject(&blackPen);

	  if(m_pCurLine->nFlag == 0)
	  {
	    mdc.MoveTo(m_pCurLine->m_nRect.left+4,0);
	    mdc.LineTo(m_pCurLine->m_nRect.left+4,200);
	  }
	  else
	  if(m_pCurLine->nFlag == 1)
	  {
	    mdc.MoveTo(m_pCurLine->m_nRect.right,0);
	    mdc.LineTo(m_pCurLine->m_nRect.right,200);
	  }
	  else
	  {
	    mdc.MoveTo(m_pCurLine->m_nRect.left,0);
	    mdc.LineTo(m_pCurLine->m_nRect.left,200);
	  }
	  mdc.SelectObject(pOldpen);
	}

	pDC->BitBlt(rc.left,rc.top,rc.Width(),rc.Height(),&mdc,0,0,SRCCOPY);

	mdc.SelectObject(pOldPen);
	mdc.SelectObject(pOldBmp);
	mdc.DeleteDC();
	ReleaseDC(pDC);
}

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


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

}

/////////////////////////////////////////////////////////////////////////////
// CViewTestView printing

BOOL CViewTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CViewTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CViewTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CViewTestView::OnDestroy()
{
	// Deactivate the item on destruction; this is important
	// when a splitter view is being used.
   CView::OnDestroy();
   COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
   if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
   {
      pActiveItem->Deactivate();
      ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
   }
}


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

BOOL CViewTestView::IsSelected(const CObject* pDocItem) const
{
	// The implementation below is adequate if your selection consists of
	//  only CViewTestCntrItem 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 CViewTestView::OnInsertObject()
{
	// Invoke the standard Insert Object dialog box to obtain information
	//  for new CViewTestCntrItem object.
	COleInsertDialog dlg;
	if (dlg.DoModal() != IDOK)
		return;

	BeginWaitCursor();

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

		// Initialize the item from the dialog data.
		if (!dlg.CreateItem(pItem))
			AfxThrowMemoryException();  // any exception will do
		ASSERT_VALID(pItem);
		
        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.  Here,
//  the container (not the server) causes the deactivation.
void CViewTestView::OnCancelEditCntr()
{
	// 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 CViewTestView::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 CViewTestView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);
	COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
	if (pActiveItem != NULL)
		pActiveItem->SetItemRects();

	m_wndHorzRuler.MoveWindow(0,0,cx,30);
	m_wndVertRuler.MoveWindow(0,0,30,cy);
}

/////////////////////////////////////////////////////////////////////////////
// CViewTestView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CViewTestView message handlers

int CViewTestView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

    m_wndHorzRuler.Create("",WS_CHILD|WS_VISIBLE|WS_BORDER,CRect(),this,ID_HORZ);
	m_wndHorzRuler.SetRulerInfo(m_LowerBound,m_UpperBound,5,TRUE,FALSE,FALSE);
	m_wndVertRuler.Create("",WS_CHILD|WS_VISIBLE|WS_BORDER,CRect(),this,ID_VERT);
	m_wndVertRuler.SetRulerInfo(m_LowerBound,m_UpperBound,5,FALSE,TRUE,FALSE);
	return 0;
}


void CViewTestView::OnStartTrackingRulerHorz(short nFlags, long nX, long nY) 
{
	CPoint pt(nX,nY);
	ScreenToClient(&pt);
    m_pCurLine = getLineVert(CPoint(pt.x,pt.y));	
	if(m_pCurLine == NULL)
	{
	  m_pCurLine = new CViewTestView::_RuleLine;
	  /*****/
	  //nFlags ==1 for left/top bar nFlags==2 for right/bottom and 0 for arrow
	  /*****/
	  if(nFlags == 1)//left side bar and top bar for vertical ruler
	  {
		//the 2 is not abitrary,it's the width or hieght depending on weither it's
		//a vertical or horizontal ruler of an invisible handle to a bar that
		//does mouse capture
        m_pCurLine->m_nRect.SetRect(pt.x-2,0,pt.x,200);
		m_pCurLine->nFlag =1;
	  }
	  else
	  if(nFlags == 2)//for right side bar,and bottom bar for vertical ruler
	  {
	    m_pCurLine->m_nRect.SetRect(pt.x,0,pt.x+2,200);
		m_pCurLine->nFlag = 2;
	  }
	  else   
	  {              //for arrows.The 4 offset is used to compensate for the
		             //the width of the arrow image,which is 9 pexils wide,it is 
					 //not abitrary and should be used if u want the kinda effect 
					 //i have in this app with positioning the lines.
					 //The inverse of the above condition is true for a vertical 
					 //ruler

	    m_pCurLine->m_nRect.SetRect(pt.x-4,0,pt.x+4,200);
		m_pCurLine->nFlag = 0;
	  }
	  m_VertLines.AddTail(m_pCurLine);
	}
	m_bTracking = TRUE;Invalidate();UpdateWindow();
}

void CViewTestView::OnStopTrackingRulerHorz(short nFlags, long nX, long nY) 
{
	if(m_bTracking)
	{
       m_pCurLine = NULL;
       m_bTracking = FALSE;
       Invalidate();UpdateWindow();
	}
}

void CViewTestView::OnTrackRulerHorz(short nFlags, long nX, long nY) 
{
   if(m_bTracking)
   {
	 CPoint pt(nX,nY);
	 ScreenToClient(&pt);
	 if(m_pCurLine->nFlag == 0)
	 {
	    int noffset = pt.x - m_pCurLine->m_nRect.left;
	    m_pCurLine->m_nRect.OffsetRect(noffset,0);
	 }
	 else
	 if(m_pCurLine->nFlag == 1)//left/top bar depending on horz or vert ruler
	 {
		 //may want to make a check here for the lowerbound you specified 
		 //when creating the control
		 //u don't want the the line to go passed the lowerbound
		 //same goes for a the right/top and upperbound
	    int noffset = pt.x - m_pCurLine->m_nRect.right;
	    m_pCurLine->m_nRect.OffsetRect(noffset,0);
	 }
	 else                      //vice verse
	 {
	    int noffset = pt.x - m_pCurLine->m_nRect.left;
	    m_pCurLine->m_nRect.OffsetRect(noffset,0);
	 }
   }
   Invalidate();UpdateWindow();
}

void CViewTestView::OnStartTrackingRulerVert(short nFlags, long nX, long nY) 
{

}

void CViewTestView::OnStopTrackingRulerVert(short nFlags, long nX, long nY) 
{


}

void CViewTestView::OnTrackRulerVert(short nFlags, long nX, long nY) 
{


}

void CViewTestView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	m_pCurLine = getLineVert(point);
	if(m_pCurLine != NULL)
	{
      SetCapture();
	  m_bTracking = TRUE;
	  CPoint pt(point);
	  ClientToScreen(&pt);

      m_wndHorzRuler.StartTracking(0,pt.x,pt.y);
	  Invalidate();UpdateWindow();
	}
	CView::OnLButtonDown(nFlags, point);
}

void CViewTestView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if(m_bTracking)
	{
      ReleaseCapture();
	  m_bTracking = FALSE;
	  CPoint pt(point);
	  ClientToScreen(&pt);
	  m_wndHorzRuler.StopTracking(0,pt.x,pt.y);
	  m_pCurLine =NULL;
	  Invalidate();UpdateWindow();
	}
	CView::OnLButtonUp(nFlags, point);
}


BOOL CViewTestView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
    CView::OnSetCursor(pWnd, nHitTest, message);
	CPoint pt;
	GetCursorPos(&pt);
	ScreenToClient(&pt);
    CViewTestView::_RuleLine* pLine = getLineVert(pt);
	if(pLine != NULL)
	{
      if(pLine->m_nRect.PtInRect(pt))
		  ::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZEWE));
	}return TRUE;
}

CViewTestView::_RuleLine* CViewTestView::getLineHorz(CPoint pt)
{
  for(POSITION nPos = m_HorzLines.GetHeadPosition(); nPos != NULL;)
  {
    CViewTestView::_RuleLine* pLine = m_HorzLines.GetNext(nPos);
	if(pLine->m_nRect.PtInRect(pt))return pLine;

  }return NULL;
}


CViewTestView::_RuleLine* CViewTestView::getLineVert(CPoint pt)
{
  for(POSITION nPos = m_VertLines.GetHeadPosition(); nPos != NULL;)
  {
    CViewTestView::_RuleLine* pLine = m_VertLines.GetNext(nPos);
	if(pLine->m_nRect.PtInRect(pt))return pLine;

  }return NULL;
}

void CViewTestView::OnMouseMove(UINT nFlags, CPoint point) 
{
    if(m_bTracking)
	{
		if(m_pCurLine->nFlag == 0)
		{
	      int noffset = point.x - m_pCurLine->m_nRect.left;
	      m_pCurLine->m_nRect.OffsetRect(noffset,0);
		}
		else
		if(m_pCurLine->nFlag == 1)//left/top bar depending on horz or vert ruler
		{
	      int noffset = point.x - m_pCurLine->m_nRect.right;
	      m_pCurLine->m_nRect.OffsetRect(noffset,0);
		}
		else                      //vice verse
		{
	      int noffset = point.x - m_pCurLine->m_nRect.left;
	      m_pCurLine->m_nRect.OffsetRect(noffset,0);
		}
	   CPoint pt(point);
	   ClientToScreen(&pt);
       m_wndHorzRuler.Track(0,pt.x,pt.y);
	   Invalidate();UpdateWindow();
	}
	CView::OnMouseMove(nFlags, point);
}

⌨️ 快捷键说明

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