menu_controlbarview.cpp

来自「关于使用VC编程的代码」· C++ 代码 · 共 242 行

CPP
242
字号
// Menu_ControlBarView.cpp : implementation of the CMenu_ControlBarView class
//

#include "stdafx.h"
#include "Menu_ControlBar.h"
#include "MainFrm.h"

#include "Menu_ControlBarDoc.h"
#include "Menu_ControlBarView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMenu_ControlBarView

IMPLEMENT_DYNCREATE(CMenu_ControlBarView, CView)

BEGIN_MESSAGE_MAP(CMenu_ControlBarView, CView)
	//{{AFX_MSG_MAP(CMenu_ControlBarView)
	ON_WM_MOUSEMOVE()
	ON_COMMAND(ID_BEGIN_BOUNCE, OnBeginBounce)
	ON_UPDATE_COMMAND_UI(ID_BEGIN_BOUNCE, OnUpdateBeginBounce)
	ON_COMMAND(ID_TEXT_OUT, OnTextOut)
	ON_UPDATE_COMMAND_UI(ID_TEXT_OUT, OnUpdateTextOut)
	ON_COMMAND(ID_CLEAR_TEXT, OnClearText)
	ON_UPDATE_COMMAND_UI(ID_CLEAR_TEXT, OnUpdateClearText)
	ON_COMMAND(ID_END_BOUNCE, OnEndBounce)
	ON_UPDATE_COMMAND_UI(ID_END_BOUNCE, OnUpdateEndBounce)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_SPEED_BUTTON, OnSpeedButton)
	ON_WM_RBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMenu_ControlBarView construction/destruction

CMenu_ControlBarView::CMenu_ControlBarView()
{
	m_bTextOut = FALSE;
	m_strData = "Hello, this is a sample program";
	m_nSpeed = 1;
	m_bBounce = FALSE;
	m_Rect.SetRect(20,20,40,40);
}

CMenu_ControlBarView::~CMenu_ControlBarView()
{
}

BOOL CMenu_ControlBarView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMenu_ControlBarView drawing

void CMenu_ControlBarView::OnDraw(CDC* pDC)
{
	CMenu_ControlBarDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMenu_ControlBarView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMenu_ControlBarView message handlers

void CMenu_ControlBarView::OnMouseMove(UINT nFlags, CPoint point) 
{
	CString str;
	CStatusBar* pStatusBar;

	pStatusBar = (CStatusBar*)AfxGetApp()->m_pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR);
	str.Format("x:%d", point.x);
	pStatusBar->SetPaneText(1, str);
	str.Format("y:%d", point.y);
	pStatusBar->SetPaneText(2, str);

	CView::OnMouseMove(nFlags, point);
}

void CMenu_ControlBarView::OnBeginBounce() 
{
	CDC* pDC = GetDC();
	CBrush *pOldBrush, Brush( RGB(0,255,255) );

	m_bBounce = TRUE;
	pOldBrush = pDC->SelectObject( &Brush );
	pDC->Ellipse(&m_Rect);
	SetTimer(2, m_nSpeed * 100, 0);
	pDC->SelectObject( pOldBrush );
}

void CMenu_ControlBarView::OnUpdateBeginBounce(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable( m_bBounce == FALSE );	
}

void CMenu_ControlBarView::OnTextOut()
{
	CDC* pDC = GetDC();
	pDC->TextOut(0, 0, m_strData);
	m_bTextOut = TRUE;
}

void CMenu_ControlBarView::OnUpdateTextOut(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bTextOut == FALSE);	
}

void CMenu_ControlBarView::OnClearText()
{
	CDC* pDC = GetDC();
	TEXTMETRIC tm;

	pDC->GetTextMetrics(&tm);
	CRect rect(0, 0, m_strData.GetLength()*tm.tmMaxCharWidth, tm.tmHeight + tm.tmExternalLeading );
	CBrush brush( pDC->GetBkColor() );
	pDC->FillRect(&rect, &brush);
	m_bTextOut = FALSE;
}

void CMenu_ControlBarView::OnUpdateClearText(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bTextOut == TRUE);	
}

void CMenu_ControlBarView::OnEndBounce() 
{
	KillTimer(2);
	m_bBounce = FALSE;
}

void CMenu_ControlBarView::OnUpdateEndBounce(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bBounce == TRUE);
}

void CMenu_ControlBarView::OnTimer(UINT nIDEvent) 
{
	CDC* pDC = GetDC();
	CBrush *pOldBrush, bkBrush(pDC->GetBkColor()), fgBrush( RGB(0,255,255) );

	KillTimer(2);
	pDC->FillRect(&m_Rect, &bkBrush);
	pOldBrush = pDC->SelectObject( &fgBrush );

	CRect ClientRect;
	CPoint point(20,20);
	int nHeight,nWidth;

	GetClientRect(&ClientRect);
	m_Rect += point;
	nHeight = m_Rect.Height();
	nWidth = m_Rect.Width();
	if( m_Rect.bottom > ClientRect.bottom )
	{
		m_Rect.top = ClientRect.top + 20;
		m_Rect.bottom = m_Rect.top + nHeight;
	}
	if( m_Rect.right > ClientRect.right )
	{
		m_Rect.left = ClientRect.left;
		m_Rect.right = m_Rect.left + nWidth;
	}
	pDC->Ellipse(&m_Rect);
	SetTimer(2, m_nSpeed * 100, 0);	
	pDC->SelectObject(pOldBrush);

	CView::OnTimer(nIDEvent);
}

void CMenu_ControlBarView::OnSpeedButton() 
{
	CString strSpeed;
	BOOL bIsNum;
	int nIndex,nCount;
	char ch;

	CMainFrame* pFrm = (CMainFrame*)AfxGetApp()->m_pMainWnd;
	pFrm->m_wndDialogBar.GetDlgItem(IDC_SPEED_EDIT)->GetWindowText(strSpeed);	
	nIndex = 0;
	nCount = strSpeed.GetLength();
	bIsNum = TRUE;
	while( (nIndex< nCount) && (bIsNum == TRUE) )
	{
		ch = strSpeed.GetAt(nIndex);
		if( (ch >='0') && (ch <= '9') == FALSE)
		{
			bIsNum = FALSE;
		}
		nIndex++;
	}
	if(bIsNum == TRUE)
	{
		m_nSpeed = atoi(strSpeed);
	}
	else
	{
		AfxMessageBox("Error in setting speed!");
	}
}

void CMenu_ControlBarView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	CMenu PopMenu;
	CRect rect;

	PopMenu.LoadMenu(IDR_POP_MENU);
	PopMenu.TrackPopupMenu(TPM_RIGHTBUTTON, point.x, point.y, this);
	CView::OnRButtonDown(nFlags, point);
}

⌨️ 快捷键说明

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