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

📄 flipbdlg.cpp

📁 VC多媒体开发指南
💻 CPP
字号:
// flipbdlg.cpp : implementation file
//

#include "stdafx.h"
#include "flipbook.h"
#include "flipbdlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFlipbookDlg dialog

CFlipbookDlg::CFlipbookDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFlipbookDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFlipbookDlg)
		// 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);
}

CFlipbookDlg::~CFlipbookDlg(void)
{
	//Free memory.
	if (m_palette != NULL)	
		delete m_palette;

	if (m_hDIB != NULL)
		::GlobalFree((HGLOBAL) m_hDIB);
}
void CFlipbookDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFlipbookDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFlipbookDlg, CDialog)
	//{{AFX_MSG_MAP(CFlipbookDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_FORWARD, OnForward)
	ON_BN_CLICKED(IDC_REVERSE, OnReverse)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFlipbookDlg message handlers

BOOL CFlipbookDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	CenterWindow();
	
	//Load the bitmap.
	CFile file;
	if (!file.Open("FLIPBOOK.BMP", CFile::modeRead | CFile::shareDenyWrite, NULL))
		{
		AfxMessageBox("Could not open bitmap file.");
		return FALSE;
		}

	m_hDIB = ::ReadDIBFile(file);

	if (m_hDIB == NULL)
		{
		AfxMessageBox("Cannot load bitmap", NULL, MB_ICONINFORMATION | MB_OK);
		return FALSE;
		}

	//Get the bitmap's palette.
	m_palette = new CPalette;
	if (m_palette == NULL)		//Low on memory!
		{
		::GlobalFree((HGLOBAL) m_hDIB);
		m_hDIB = NULL;
		return FALSE;
		}

	if (::CreateDIBPalette(m_hDIB, m_palette) == NULL)
		{
		//DIB may not have a palette.
		delete m_palette;
		m_palette = NULL;
		}
	
	m_row = m_col = 1;	

	CButton* p;
	p = (CButton*) GetDlgItem(IDC_REVERSE);
	p->EnableWindow(FALSE);

	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 CFlipbookDlg::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
	{
		CPaintDC dc(this); // device context for painting
		
		//Be sure there's a bitmap available.
		if (m_hDIB == NULL)
			return;

		//Get the dialog box size.
		CRect dbRect;
		GetClientRect(&dbRect);

		//Define the output rectangle to be the size of the animation
		//frame, centered left-to-right, near the top of the dialog box.
		m_outRect.left = (dbRect.right - X_SIZE) / 2; 
		m_outRect.right = m_outRect.left + X_SIZE;
		m_outRect.top = dbRect.bottom / 20;
		m_outRect.bottom = m_outRect.top + Y_SIZE;

		//Define the source rectangle, the section of the source bitmap
		//that contains the frame to be displayed.
		CRect bmRect;
		bmRect.left = X_START + (m_col - 1) * X_INCR;
		bmRect.right = bmRect.left + X_SIZE;
		bmRect.top = Y_START + (m_row - 1) * Y_INCR;
		bmRect.bottom = bmRect.top + Y_SIZE;

		//Display the bitmap.
		dc.SelectPalette(m_palette, FALSE);
		dc.RealizePalette();
		::PaintDIB(dc.m_hDC, &m_outRect, m_hDIB, &bmRect, m_palette);
	}
}

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

void CFlipbookDlg::OnForward() 
{
	int nTimer = SetTimer(1, ANIM_SPEED, NULL);
	ASSERT(nTimer != 0);
	m_direction = FORWARD;
	CButton* p;
	p = (CButton*) GetDlgItem(IDC_REVERSE);
	p->EnableWindow(TRUE);
	p = (CButton*) GetDlgItem(IDC_FORWARD);
	p->EnableWindow(FALSE);
}

void CFlipbookDlg::OnReverse() 
{
	int nTimer = SetTimer(1, ANIM_SPEED, NULL);
	ASSERT(nTimer != 0);
	m_direction = REVERSE;
	CButton* p;
	p = (CButton*) GetDlgItem(IDC_REVERSE);
	p->EnableWindow(FALSE);
	p = (CButton*) GetDlgItem(IDC_FORWARD);
	p->EnableWindow(TRUE);
}

void CFlipbookDlg::OnTimer(UINT nIDEvent) 
{
	if (m_direction == FORWARD)	//Going forward.
		{
		if (m_col < NUM_COLS)
			m_col++;
		else
			{
			m_col = 1;
			m_row++;
			}
		InvalidateRect(m_outRect, FALSE);
		if (m_col == NUM_COLS && m_row == NUM_ROWS)
			KillTimer(1);
		}
	else		//Going backward.
	 	{
		if (m_col > 1)
			m_col--;
		else
			{
			m_col = NUM_COLS;
			m_row--;
			}
		InvalidateRect(m_outRect, FALSE);
		if (m_col == 1 && m_row == 1)
			KillTimer(1);
		}
}

⌨️ 快捷键说明

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