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

📄 mousedemo1view.cpp

📁 用VC编写的鼠标变换的代码
💻 CPP
字号:
// MouseDemo1View.cpp : implementation of
// the CMouseDemo1View class
//

#include "stdafx.h"
#include "MouseDemo1.h"

#include "MouseDemo1Doc.h"
#include "MouseDemo1View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMouseDemo1View

IMPLEMENT_DYNCREATE(CMouseDemo1View, CView)

BEGIN_MESSAGE_MAP(CMouseDemo1View, CView)
	//{{AFX_MSG_MAP(CMouseDemo1View)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_RBUTTONDBLCLK()
	ON_WM_MOUSEMOVE()
	ON_COMMAND(ID_FILE_MOUSEINFORMATIONMODE_GRIDDISPLAY, OnFileMouseGriddisplay)
	ON_UPDATE_COMMAND_UI(ID_FILE_MOUSEINFORMATIONMODE_GRIDDISPLAY, OnUpdateFileMouseGriddisplay)
	ON_COMMAND(ID_FILE_MOUSEINFORMATIONMODE_POSITIONINFORMATION, OnFileMousePositioninformation)
	ON_UPDATE_COMMAND_UI(ID_FILE_MOUSEINFORMATIONMODE_POSITIONINFORMATION, OnUpdateFileMousePositioninformation)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMouseDemo1View construction/destruction

CMouseDemo1View::CMouseDemo1View()
{

	// Start in the mode that shows mouse
	// info, not the grid of rectangles.
	m_nInfoMode = MOUSE_SHOWINFO;

	// Clear the two dimensional grid array.
	for( int y=0; y<10; y++ )
		for( int x=0; x<10; x++ )
			m_nGrid[x][y] = 0;

}

CMouseDemo1View::~CMouseDemo1View()
{
}

BOOL CMouseDemo1View::PreCreateWindow(CREATESTRUCT& cs)
{
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMouseDemo1View drawing

void CMouseDemo1View::OnDraw(CDC* pDC)
{
	CMouseDemo1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// We only perform a redraw when we're
	// set to MOUSE_SHOWGRID mode.
	if( m_nInfoMode == MOUSE_SHOWGRID ){

		// Use the client rectangle
		// in order to draw the grid
		// rectangles in a size proportional
		// to the client rectangle.
		RECT Rect;
		GetClientRect( &Rect );

		// Create red, white and blue brushs.
		CBrush RedBrush( RGB( 255, 0, 0 ) );
		CBrush BlueBrush( RGB( 0, 0, 255 ) );
		CBrush WhiteBrush( RGB( 255, 255, 255 ) );
		CBrush *pUseBrush;

		// The grid has ten horizontal and ten
		// vertical components.
		for( int y=0; y<10; y++ ){
			for( int x=0; x<10; x++ ){

				// Assign DrawRect by calculating
				// one tenth of the client
				// rectangle.
				RECT DrawRect;
				DrawRect.left =
					( x * Rect.right ) / 10;
				DrawRect.top =
					( y * Rect.bottom ) / 10;
				DrawRect.right =
					DrawRect.left + ( Rect.right / 10 ) + 1;
				DrawRect.bottom =
					DrawRect.top + ( Rect.bottom / 10 );

				// Select the brush for drawing
				// based on whether the grid
				// is empty, set to left, or
				// set to right.
				pUseBrush = &WhiteBrush;
				if( m_nGrid[x][y] == 1 )
					pUseBrush = &BlueBrush;
				else if( m_nGrid[x][y] == 2 )
					pUseBrush = &RedBrush;

				// Draw the filled rectangle.
				pDC->FillRect( &DrawRect, pUseBrush );
				}
			}
		}

}

/////////////////////////////////////////////////////////////////////////////
// CMouseDemo1View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMouseDemo1View message handlers

void CMouseDemo1View::OnLButtonDown(UINT nFlags,
	CPoint point) 
{

	// Call the function that displays the mouse
	// information.
	ShowMouseInfo( "LButtonDown", point, 1 );

	// Call the default OnLButtonDown() function.
	CView::OnLButtonDown(nFlags, point);
}

void CMouseDemo1View::OnLButtonUp(UINT nFlags,
	CPoint point) 
{

	// Call the function that displays the mouse
	// information.
	ShowMouseInfo( "LButtonUp", point );
	
	// Call the default OnLButtonUp() function.
	CView::OnLButtonUp(nFlags, point);
}

void CMouseDemo1View::OnLButtonDblClk(UINT nFlags,
	CPoint point) 
{

	// Call the function that displays the mouse
	// information.
	ShowMouseInfo( "LButtonDblClk", point );
	
	// Call the default OnLButtonDblClk() function.
	CView::OnLButtonDblClk(nFlags, point);
}

void CMouseDemo1View::OnRButtonDown(UINT nFlags, 
	CPoint point) 
{

	// Call the function that displays the mouse
	// information.
	ShowMouseInfo( "RButtonDown", point, 2 );
	
	// Call the default OnRButtonDown() function.
	CView::OnRButtonDown(nFlags, point);
}

void CMouseDemo1View::OnRButtonUp(UINT nFlags,
	CPoint point) 
{

	// Call the function that displays the mouse
	// information.
	ShowMouseInfo( "RButtonUp", point );
	
	// Call the default OnRButtonUp() function.
	CView::OnRButtonUp(nFlags, point);
}

void CMouseDemo1View::OnRButtonDblClk(UINT nFlags, 
	CPoint point) 
{

	// Call the function that displays the mouse
	// information.
	ShowMouseInfo( "RButtonDblClk", point );
	
	// Call the default OnRButtonDblClk() function.
	CView::OnRButtonDblClk(nFlags, point);
}

void CMouseDemo1View::OnMouseMove(UINT nFlags, 
	CPoint point) 
{

	// Only show the mouse position if
	// we're set to MOUSE_SHOWINFO
	if( m_nInfoMode == MOUSE_SHOWINFO ){
		CClientDC ClientDC( this );

		CString strInfo;

		// Copy the CPoint class so
		// that we can convert it to
		// screen coordinates.
		CPoint pt = point;

		// Convert to screen coordinates.
		ClientToScreen( &pt );

		// Format the information.
		strInfo.Format(
			"X:%d Y:%d ScnX:%d ScnY:%d            ",
			point.x, point.y,
			pt.x, pt.y );

		// Draw the information string to
		// the window.
		ClientDC.TextOut( 0, 0,
			strInfo, strInfo.GetLength() );
		}
	
	// Call the default OnMouseMove() function.
	CView::OnMouseMove(nFlags, point);
}

void CMouseDemo1View::ShowMouseInfo(
	const char *lpszText, CPoint point, int nFlag )
{

	// Perform the following code if
	// we're set to MOUSE_SHOWGRID.
	if( m_nInfoMode == MOUSE_SHOWGRID ){
		if( nFlag != -1 ){

			// Get the client rectangle
			// so that we can calculate which
			// x and y index the current
			// click position.
			RECT Rect;
			GetClientRect( &Rect );

			// Use the client rectangle
			// and divide by ten to calculate
			// the x and y grid indexes.
			int x = ( point.x * 10 ) / Rect.right;
			int y = ( point.y * 10 ) / Rect.bottom;

			// Either set the grid to left or right
			// button states, or clear them so that
			// the grid array is empty.
			if( m_nGrid[x][y] == nFlag )
				m_nGrid[x][y] = 0;
			else
				m_nGrid[x][y] = nFlag;

			// Cause the window to redraw.
			InvalidateRect( NULL, FALSE );
			UpdateWindow();
			}
		return;
		}

	// Get a DC to the client window.
	CClientDC ClientDC( this );

	CString strInfo;

	// Format the output string.
	strInfo.Format(
		"X:%d Y:%d %s        ",
		point.x, point.y, lpszText );

	// Draw the output string to the
	// window.
	ClientDC.TextOut( point.x, point.y,
		strInfo, strInfo.GetLength() );

}

void CMouseDemo1View::OnFileMouseGriddisplay() 
{

	// Set to MOUSE_SHOWGRID mode.
	if( m_nInfoMode != MOUSE_SHOWGRID ){
		m_nInfoMode = MOUSE_SHOWGRID;
		InvalidateRect( NULL, TRUE );
		UpdateWindow();
		}

}

void CMouseDemo1View::OnUpdateFileMouseGriddisplay(
	CCmdUI* pCmdUI) 
{

	// Set the menu check if we're
	// in MOUSE_SHOWGRID mode.
	pCmdUI->SetCheck( m_nInfoMode == MOUSE_SHOWGRID );

}

void CMouseDemo1View::OnFileMousePositioninformation() 
{

	// Set to MOUSE_SHOWINFO mode.
	if( m_nInfoMode != MOUSE_SHOWINFO ){
		m_nInfoMode = MOUSE_SHOWINFO;
		InvalidateRect( NULL, TRUE );
		UpdateWindow();
		}

}

void
CMouseDemo1View::OnUpdateFileMousePositioninformation(
	CCmdUI* pCmdUI) 
{

	// Set the menu check if we're
	// in MOUSE_SHOWINFO mode.
	pCmdUI->SetCheck( m_nInfoMode == MOUSE_SHOWINFO );

}

⌨️ 快捷键说明

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