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

📄 playballdlg.cpp

📁 一个小球乱跑的小游戏
💻 CPP
字号:
// playBallDlg.cpp : implementation file
//

#include "stdafx.h"
#include "playBall.h"
#include "playBallDlg.h"
#include "DlgSetUp.h"

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

#define BALL_ID 10000

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPlayBallDlg dialog

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

	m_changeCol = 0;
	m_bClick = FALSE;
	m_nTimeDif = 20;
	m_nSize = 20;
	m_nSpeed = 5;
	m_maxNum = 50;

	m_ballRect.SetRect(0, 0, m_nSize, m_nSize);
	m_bollClr = RGB(255, 0, 0);
	
}

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

BEGIN_MESSAGE_MAP(CPlayBallDlg, CDialog)
	//{{AFX_MSG_MAP(CPlayBallDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_LBUTTONDOWN()
	ON_WM_DESTROY()
	ON_COMMAND(ID_SETUP, OnSetup)
	ON_WM_SIZE()
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_CLEAR, OnClear)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPlayBallDlg message handlers

BOOL CPlayBallDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
//	SetTimer(1, 20, NULL);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPlayBallDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CPlayBallDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CRect rect;
	GetClientRect(&rect);
	
	if (IsIconic())
	{
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);

		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		dc.DrawFocusRect(rect);
		CDialog::OnPaint();
	}
}

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

void CPlayBallDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if(m_aBall.GetSize() >= m_maxNum)
		return;
	CMyBall *pBall = new CMyBall(this, m_aBall.GetSize());
	
	CString strMyClass;

	pBall->m_brush.CreateSolidBrush(m_bollClr);
	try
	{
		strMyClass = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW, 0,
			(HBRUSH)pBall->m_brush, 0);
	}
	catch (CResourceException* pEx)
	{
		AfxMessageBox(_T("Couldn't register class! (Already registered?)"));
		pEx->Delete();
	}
	pBall->m_nSpeed = m_nSpeed;

	CRect rc;
	GetClientRect(&rc);
	CPoint pt = point;
	if(pt.x > rc.right - m_nSize * 2)
		pt.x = rc.right - m_nSize * 2;
	if(pt.y > rc.bottom - m_nSize * 2)
		pt.y = rc.bottom - m_nSize * 2;
	
	pBall->m_rect = CRect(pt.x, pt.y, pt.x + m_nSize * 2, pt.y + m_nSize * 2);
	pBall->Create(strMyClass, NULL, WS_CHILD | WS_VISIBLE,
			pBall->m_rect, this, BALL_ID + m_aBall.GetSize());
	
	pBall->ShowWindow(SW_SHOW);
	
	m_aBall.Add(pBall);

	CDialog::OnLButtonDown(nFlags, point);
}

void CPlayBallDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	while(m_aBall.GetSize() > 0)
	{ 
		CMyBall *pBall = m_aBall.GetAt(0);
		delete pBall;
		m_aBall.RemoveAt(0);
	}
}


void CPlayBallDlg::OnSetup() 
{
	CDlgSetUp dlg;
	dlg.m_nSize = m_nSize;
	dlg.m_nSpeed = m_nSpeed;
	dlg.m_nMaxNum = m_maxNum;
	if(IDOK == dlg.DoModal())
	{
		m_nSize = dlg.m_nSize;
		m_nSpeed = dlg.m_nSpeed;
		m_maxNum = dlg.m_nMaxNum;
	}
}

void CPlayBallDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	if(m_hWnd != NULL)
		RedrawWindow();
}


void CPlayBallDlg::OnRButtonDown(UINT nFlags, CPoint point) 
{
	int n = m_aBall.GetSize();
	if(n == 0)
		return;
	n--;
	CMyBall *pBall = (CMyBall *)m_aBall.GetAt(n);
	delete pBall;
	m_aBall.RemoveAt(n);
	
	CDialog::OnRButtonDown(nFlags, point);
}

void CPlayBallDlg::OnClear() 
{
	int n = m_aBall.GetSize();
	while(n-- > 0)
	{
		CMyBall *pBall = (CMyBall *)m_aBall.GetAt(n);
		delete pBall;
		m_aBall.RemoveAt(n);
	}
}

⌨️ 快捷键说明

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