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

📄 marqudlg.cpp

📁 C:Documents and SettingsAdministrator桌面VC++多媒体特效制作百例CHAR04MARQUEE
💻 CPP
字号:
// marqudlg.cpp : implementation file
//

#include "stdafx.h"
#include "marquee.h"
#include "marqudlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMarqueeDlg dialog

CMarqueeDlg::CMarqueeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMarqueeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMarqueeDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

CMarqueeDlg::~CMarqueeDlg()
{
	//Free memory.
	GlobalUnlock(hPal);
	GlobalFree(hPal);
}

void CMarqueeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMarqueeDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMarqueeDlg, CDialog)
	//{{AFX_MSG_MAP(CMarqueeDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_SIZE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMarqueeDlg message handlers

BOOL CMarqueeDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	CenterWindow();
		
	int colorIndex;

	//Allocate memory for the palette entry array.
	hPal = GlobalAlloc(GMEM_MOVEABLE, sizeof(LOGPALETTE) + 4 * sizeof(PALETTEENTRY));
	if (hPal == NULL)
		{
		AfxMessageBox("Memory allocation error");
		return TRUE;
		}
	//Initialize the LPLOGPALETTE pointer to point at the
	//beginning of the locked allocated memory.
	lpLogPal = (LPLOGPALETTE) GlobalLock(hPal);

	//Fill members of the LOGPALETTE structure to indicate the
	//Windows version and the number of palette entries.
	lpLogPal->palVersion = 0x300;
	lpLogPal->palNumEntries = 4;

	//Fill all palette enties with colors.
	for (colorIndex = 0; colorIndex < 4; colorIndex++)
		{
		lpLogPal->palPalEntry[colorIndex].peRed = (127 + colorIndex * 128 / 3);
		lpLogPal->palPalEntry[colorIndex].peGreen = (127 + colorIndex * 128 / 3);
		lpLogPal->palPalEntry[colorIndex].peBlue = 0;
		lpLogPal->palPalEntry[colorIndex].peFlags = PC_RESERVED;
		}
		
	//Create a logical palette from the array values.
	if (!palette.CreatePalette(lpLogPal))
		AfxMessageBox("Palette creation failed!");

	//Note: we don't unlock and free the memory because
	//we'll need this array later.
	
	//Create an array of four CBrush objects.
	for (long index = 0; index < 4; index++)
		brush[index].CreateSolidBrush(0x1000000 | index);
	
	//Set up the timer. Change the second parameter
	//for faster or slower animation.
	int nTimer=SetTimer(1,200,NULL);
		ASSERT(nTimer != 0);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMarqueeDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		//Device context for painting.
		CPaintDC dc(this); 
		
		//Here's where we create the marquee lights.
		int dotWidth;
		long colorIndex;
				
		//Select and realize the palette.
		dc.SelectPalette(&palette, FALSE);
		dc.RealizePalette();
				
		//Calculate size of each "light."
		CRect rect;
		GetClientRect(&rect);
		dotWidth = rect.Width() / 24;
		CRect rect1(dotWidth * 1.5, dotWidth * .5, dotWidth * 2.5, dotWidth * 1.5);
		
		//Draw a black background.
		dc.SelectStockObject(BLACK_BRUSH);
		dc.Rectangle(rect);
	
		colorIndex = 0;
		//Draw the top row of lights.
		do
		{
			dc.SelectObject(&brush[colorIndex]);
			dc.Ellipse(rect1);
			colorIndex == 3 ? colorIndex = 0: colorIndex++;
			rect1.OffsetRect(dotWidth * 1.5, 0);
		} while (rect1.BottomRight().x < (rect.Width() - dotWidth));
		
		//Left column.
		rect1.SetRect(dotWidth * .5, dotWidth * 1.5, dotWidth * 1.5, dotWidth * 2.5);
		do
		{
			dc.SelectObject(&brush[colorIndex]);
			dc.Ellipse(rect1);
			colorIndex == 3 ? colorIndex = 0: colorIndex++;
			rect1.OffsetRect(0, dotWidth * 1.5);
		} while (rect1.BottomRight().y < (rect.Height() - dotWidth));

		//Right column.
		rect1.SetRect((rect.Width() - dotWidth*1.5), dotWidth*1.5, (rect.Width() - dotWidth*.5), dotWidth*2.5);
		do
		{
			dc.SelectObject(&brush[colorIndex]);
			dc.Ellipse(rect1);
			colorIndex == 3 ? colorIndex = 0: colorIndex++;
			rect1.OffsetRect(0, dotWidth * 1.5);
		} while (rect1.BottomRight().y < (rect.Height() - dotWidth));

		//Bottom row.
		rect1.SetRect(dotWidth * 1.5, (rect.Height()-dotWidth*1.5), dotWidth * 2.5, (rect.Height()-dotWidth*.5));
		do
		{
			dc.SelectObject(&brush[colorIndex]);
			dc.Ellipse(rect1);
			colorIndex == 3 ? colorIndex = 0: colorIndex++;
			rect1.OffsetRect(dotWidth * 1.5, 0);
		} while (rect1.BottomRight().x < (rect.Width() - dotWidth));

	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMarqueeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMarqueeDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	//Redraw the entire client area.
	Invalidate(FALSE);	
}

void CMarqueeDlg::OnTimer(UINT nIDEvent) 
{
	long index;
	
	for (index = 4; index > 0; index--)
		lpLogPal->palPalEntry[index] = lpLogPal->palPalEntry[index - 1];
		
	lpLogPal->palPalEntry[0] = lpLogPal->palPalEntry[4];
	palette.AnimatePalette(0, 4, lpLogPal->palPalEntry);
}

⌨️ 快捷键说明

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