本文主要是介绍疯狂打地鼠游戏核心代码(鼠标变锤子),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 先导入 ICON 和 CURSOR 资源, 并在初始化时赋给成员变量
2 在CGameDlg 类里定义 CStaticEx m_staticEx[17]; //这个派生类见文末HICON icoHole; //地洞HCURSOR curHammerWood0;// 锤子HCURSOR curHammerWood1;HICON icoMoleD0;// 动物1HICON icoMoleD1;HICON icoMoleD2;HICON icoMoleG0;// 动物2HICON icoMoleG1;HICON icoMoleG2;BOOL CGameDlg::OnInitDialog()
{HINSTANCE hInstance = ::AfxGetInstanceHandle();icoHole = (HICON)::LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_HOLE), IMAGE_ICON, 64, 64, 0);//第一组钻入钻出图片icoMoleD0 = (HICON)::LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_MOLE_D0), IMAGE_ICON, 64, 64, 0);icoMoleD1 = (HICON)::LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_MOLE_D1), IMAGE_ICON, 64, 64, 0);icoMoleD2 = (HICON)::LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_MOLE_D2), IMAGE_ICON, 64, 64, 0);//第二组钻入钻出图片icoMoleG0 = (HICON)::LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_MOLE_G0), IMAGE_ICON, 64, 64, 0);icoMoleG1 = (HICON)::LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_MOLE_G1), IMAGE_ICON, 64, 64, 0);icoMoleG2 = (HICON)::LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_MOLE_G2), IMAGE_ICON, 64, 64, 0);// 鼠标指针,一个锤子HCURSOR curHammerWood0 = LoadCursor(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_CUR_HAMMER_WOOD0));HCURSOR curHammerWood1 = LoadCursor(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_CUR_HAMMER_WOOD1));// 16个picture装入初始地洞图片for (int i = IDC_STATIC0; i <= IDC_STATIC15; i++){((CStatic *)GetDlgItem(i))->SetIcon(icoHole);}// 16 + 1 个group 装入自定义的派生类 CStaticEx, 初始化锤子for ( i = 0; i < 18; i++){m_staticEx[i].SubclassDlgItem(IDC_STATIC0 + i, this);m_staticEx[i].SetHoverCursor(curHammerWood0);m_staticEx[i].SetClickCursor(curHammerWood1);}// 设置一秒反应时间,供用户打击地鼠SetTimer(TIMER1, 1000, 0);}void CGameDlg::OnTimer(UINT nIDEvent)
{srand((int)time(0));switch(nIDEvent){case TIMER1:KillTimer(TIMER1);isActive = false;((CStatic *)GetDlgItem(last))->SetIcon(icoHole);Sleep(100);// 随机选择两组中一组动物nIDEvent = rand() % 2;// 随机设置其中一个洞钻出last = IDC_STATIC0 + rand() % 16;((CStatic *)GetDlgItem(last))->SetIcon(nIDEvent ? icoMoleD0 : icoMoleG0);//刚钻出Sleep(100);((CStatic *)GetDlgItem(last))->SetIcon(nIDEvent ? icoMoleD1 : icoMoleG1);//钻出一半Sleep(100);((CStatic *)GetDlgItem(last))->SetIcon(nIDEvent ? icoMoleD2 : icoMoleG2);//完全钻出Sleep(100);isActive = true;break;}CDialog::OnTimer(nIDEvent);
}BEGIN_MESSAGE_MAP(CGameDlg, CDialog)//{{AFX_MSG_MAP(CGameDlg)ON_WM_TIMER()ON_WM_CLOSE()ON_WM_LBUTTONDOWN()ON_WM_MOUSEMOVE()//}}AFX_MSG_MAPON_CONTROL_RANGE(BN_CLICKED, IDC_STATIC0, IDC_STATIC15, OnPictureClick)
END_MESSAGE_MAP()void CGameDlg::OnPictureClick(UINT nPicture)
{if (isActive == true && nPicture == last){// 打中一个地鼠,做相应操作}
}//
// 头文件 StaticEx.h#ifndef _STATIC_EX_H
#define _STATIC_EX_H#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000#ifndef ON_WM_MOUSELEAVE
#define ON_WM_MOUSELEAVE() \
{ WM_MOUSELEAVE, 0, 0, 0, AfxSig_vv, \
(AFX_PMSG)(AFX_PMSGW) \
(static_cast< void (AFX_MSG_CALL CWnd::*)(void) > ( /*&ThisClass :: */OnMouseLeave)) },
#endif#ifndef ON_WM_MOUSEHOVER
#define ON_WM_MOUSEHOVER() \
{ WM_MOUSEHOVER, 0, 0, 0, AfxSig_vwp, \
(AFX_PMSG)(AFX_PMSGW)(void (AFX_MSG_CALL CWnd::*)(UINT, CPoint))&OnMouseHover },
#endifclass CStaticEx : public CStatic
{
public:CStaticEx();~CStaticEx();void SetClickCursor(HCURSOR & cursor){clickCursor = cursor;}void SetHoverCursor(HCURSOR & cursor){hoverCursor = cursor;}
protected://{{AFX_MSG(CStaticEx)afx_msg void OnMouseMove(UINT nFlags, CPoint point);afx_msg BOOL OnClicked();afx_msg void OnLButtonUp(UINT nFlags, CPoint point);afx_msg void OnLButtonDown(UINT nFlags, CPoint point);afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);afx_msg LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam);//}}AFX_MSGDECLARE_MESSAGE_MAP()
private:HCURSOR clickCursor;HCURSOR hoverCursor;int m_bTracking;
};#endif//
// cpp文件 staticEx.cpp
#include "stdafx.h"
#include "StaticEx.h"#ifdef BTNST_USE_SOUND
#pragma comment(lib, "winmm.lib")
#include <Mmsystem.h>
#endif#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifCStaticEx::CStaticEx()
{m_bTracking = 1;
}CStaticEx::~CStaticEx()
{
}BEGIN_MESSAGE_MAP(CStaticEx, CStatic)//{{AFX_MSG_MAP(CStaticEx)ON_WM_MOUSEMOVE()ON_WM_LBUTTONDOWN()ON_WM_LBUTTONUP()ON_WM_SETCURSOR()ON_CONTROL_REFLECT_EX(BN_CLICKED, OnClicked)ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)//}}AFX_MSG_MAP
END_MESSAGE_MAP()BOOL CStaticEx::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{//TraceNoop(_T("OnSetCursor\n"));
// ::SetCursor(hoverCursor);return FALSE;//return CStatic::OnSetCursor(pWnd, nHitTest, message);
}void CStaticEx::OnMouseMove(UINT nFlags, CPoint point)
{//TraceNoop(_T("OnMouseMove\n"));if (m_bTracking){TRACKMOUSEEVENT tme;tme.cbSize = sizeof(TRACKMOUSEEVENT);tme.dwFlags = TME_LEAVE | TME_HOVER;//要触发的消息tme.hwndTrack = this->m_hWnd;tme.dwHoverTime = 10;// 若不设此参数,则无法触发mouseHover 和 mouseLeave// 鼠标在按钮上停留超过 50ms ,才认为状态为 HOVERif (::_TrackMouseEvent(&tme)) //MOUSELEAVE|MOUSEHOVER消息由此函数触发{m_bTracking = 0; }}CStatic::OnMouseMove(nFlags, point);
}LRESULT CStaticEx::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{TraceNoop(_T("OnMouseLeave\n"));m_bTracking = 1;return 0;
}LRESULT CStaticEx::OnMouseHover(WPARAM wParam, LPARAM lParam)
{TraceNoop(_T("OnMouseHover\n"));if (m_bTracking != 2){TraceNoop(_T("SetCursor OnMouseHover\n"));::SetCursor(hoverCursor);}m_bTracking = 2;return 0;
}void CStaticEx::OnLButtonUp(UINT nFlags, CPoint point)
{TraceNoop(_T("OnLButtonUp\n"));::SetCursor(hoverCursor);CStatic::OnLButtonUp(nFlags, point);
}void CStaticEx::OnLButtonDown(UINT nFlags, CPoint point)
{TraceNoop(_T("OnLButtonDown\n"));::SetCursor(clickCursor);this->UpdateData(FALSE);Sleep(50);CStatic::OnLButtonDown(nFlags, point);
}BOOL CStaticEx::OnClicked()
{//TraceNoop(_T("OnClicked\n"));//::SetCursor(clickCursor);return FALSE;
}
这篇关于疯狂打地鼠游戏核心代码(鼠标变锤子)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!