anim.cpp

来自「c语言经典教程」· C++ 代码 · 共 136 行

CPP
136
字号
// anim.cpp
// Animation module of the Threads demo program
// Copyright 1994, Mark Andrews

#include "stdafx.h"
#include "anim.h"

// ***************** Member functions of MAnimation class ************

// Constructor.
MAnimation::MAnimation(HWND hWnd)
{
	m_hWnd = hWnd;

	m_nIDColor = 0x00000000;  // Black.
	m_clrShape = RGB(255,0,0);
	m_bFastSpeed = TRUE;	// Start at fast speed
	m_sizeMode = RELATIVE_SIZE;

	// Get private DC (destructor releases it).
	m_clientDC = GetDC(m_hWnd);
	m_ptPixel.x = GetDeviceCaps(m_clientDC, ASPECTX);
	m_ptPixel.y = GetDeviceCaps(m_clientDC, ASPECTY);

	// No need to call MakeNewShape here because the OnSize member 
	// function is always called after OnCreate.
}

void MAnimation::OnColor(UINT nIDColor, COLORREF clrShape)
{
	m_nIDColor = nIDColor;
	m_clrShape = clrShape;

	// Force the client area text to be repainted in the new color
	MakeNewShape();
}

// Set the shape's size and displacement, in accordance
// with the window's size.
void MAnimation::SetSize(HWND hwnd, UINT nType, int cx, int cy)
{
	LONG lScale;
	RECT clientRect;

	::GetClientRect(hwnd, &clientRect);
	cx = clientRect.right;
	cy = clientRect.bottom;
	
	m_ptCenter.x = cx >> 1;
	m_ptCenter.y = cy >> 1;
	m_ptCenter.x += cx >> 3; // make the shape a little off-center

	if (m_sizeMode == STATIC_SIZE) {

		m_sizeRadius.cx = sizeX / 2;
		m_sizeRadius.cy = sizeY / 2;

	} else {

		lScale = min((LONG)cx * m_ptPixel.x,
			(LONG)cy * m_ptPixel.y) >> 4;
		m_sizeRadius.cx = (int)(lScale / m_ptPixel.x);
		m_sizeRadius.cy = (int)(lScale / m_ptPixel.y);
	}
	m_sizeMove.cx = max(1, m_sizeRadius.cy >> 2);
	m_sizeMove.cy = max(1, m_sizeRadius.cy >> 2);

	MakeNewShape();
}

// MakeNewShape:
// Each time a parameter changes in a way that affects the speed 
// or appearance of the shape, this function is called to regenerate 
// the shape's bitmap.
//

void MAnimation::MakeNewShape()
{
	m_sizeTotal.cx = (m_sizeRadius.cx + ABS(m_sizeMove.cx)) << 1;
	m_sizeTotal.cy = (m_sizeRadius.cy + ABS(m_sizeMove.cy)) << 1;

	m_memDC = CreateCompatibleDC(m_clientDC);

	m_bmShape = CreateCompatibleBitmap(m_clientDC, m_sizeTotal.cx, 
		m_sizeTotal.cy);
	if (m_bmShape == NULL)
		MessageBox(m_hWnd, "Failed to create bitmap.", "Error!",
			MB_OK|MB_ICONSTOP);	

	m_oldBitmapDC = SelectObject(m_memDC, m_bmShape);
	Rectangle(m_memDC, -1, -1, m_sizeTotal.cx + 1, m_sizeTotal.cy + 1);
	
	m_hBrush = CreateSolidBrush(m_clrShape);
	m_oldBrushDC = SelectObject(m_memDC, m_hBrush);
	Ellipse(m_memDC, ABS(m_sizeMove.cx), ABS(m_sizeMove.cy),
		m_sizeTotal.cx - ABS(m_sizeMove.cx),
		m_sizeTotal.cy - ABS(m_sizeMove.cy));
		
	SelectObject(m_memDC, m_oldBitmapDC);
	SelectObject(m_memDC, m_oldBrushDC);
}

// Animate the shape.
void MAnimation::AnimateShape(RECT rcClient)
{
	if (m_bmShape == NULL)
		return;     // no bitmap for the shape

	m_memDC = CreateCompatibleDC(m_clientDC);

	m_oldBitmapDC = SelectObject(m_memDC, m_bmShape);
	
	BitBlt(m_clientDC, m_ptCenter.x - m_sizeTotal.cx / 2,
			m_ptCenter.y - m_sizeTotal.cy / 2,
			m_sizeTotal.cx, m_sizeTotal.cy,
			m_memDC, 0, 0, SRCCOPY);

	m_ptCenter.x += m_sizeMove.cx;
	m_ptCenter.y += m_sizeMove.cy;

	if ((m_ptCenter.x + m_sizeRadius.cx >= rcClient.right) ||
		(m_ptCenter.x - m_sizeRadius.cx <= 0))
	{
		m_sizeMove.cx = -m_sizeMove.cx;
	}

	if ((m_ptCenter.y + m_sizeRadius.cy >= rcClient.bottom) ||
		(m_ptCenter.y - m_sizeRadius.cy <= 0))
	{
		m_sizeMove.cy = -m_sizeMove.cy;
	}

	SelectObject(m_memDC, m_oldBitmapDC);
	DeleteDC(m_memDC);
}

⌨️ 快捷键说明

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