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

📄 mfc_funnymousedlg.cpp

📁 用MFC实现的一个有趣的小游戏。我也是从别的地方下载的
💻 CPP
字号:
// MFC_FunnyMouseDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFC_FunnyMouse.h"
#include "MFC_FunnyMouseDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMFC_FunnyMouseDlg dialog

CMFC_FunnyMouseDlg::CMFC_FunnyMouseDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMFC_FunnyMouseDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMFC_FunnyMouseDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	
}



BEGIN_MESSAGE_MAP(CMFC_FunnyMouseDlg, CDialog)
	//{{AFX_MSG_MAP(CMFC_FunnyMouseDlg)
	ON_BN_CLICKED(IDC_AUTHOR, OnAuthor)
	ON_WM_PAINT()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_CLEAR, OnClear)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMFC_FunnyMouseDlg message handlers

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

	// 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
	
 	// TODO: Add extra initialization here
	
	GetWindowRect(&rcLarge);
	CRect rcStatic;
 	GetDlgItem(IDC_STATIC_AUTHOR)->GetWindowRect(&rcStatic);
 	SetWindowPos(NULL,0,0,rcLarge.Width(),rcStatic.top-rcLarge.top,SWP_NOMOVE|SWP_NOZORDER);
	GetWindowRect(&rcSmall);
	m_rcDrawField=CRect(5,0,360,70);
 
 	m_bShowingAuthor=FALSE;

	m_nStartPos=m_nGradient=m_nDelay=0;
	m_nEllapse=100;
	m_pBitmap=new CBitmap;
	m_pFont=new CFont;
    m_pBrush=new CBrush;
	m_pFont->CreatePointFont(150,"Arial");
	m_pBrush->CreateSolidBrush(RGB(0,0,0));
    DrawBmp();
    
    SetTimer(0x8000,m_nEllapse,NULL);	
   	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.



// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.

//动态显示或隐藏作者信息
void CMFC_FunnyMouseDlg::OnAuthor() 
{
	// TODO: Add your control notification handler code here
	CRect rc;
	int nDelay=25;
	int nIncrement=2;
	if(!m_bShowingAuthor)
	{
		GetDlgItem(IDC_AUTHOR)->SetWindowText(_T("..<<作者"));
		GetDlgItem(IDC_STATIC_AUTHOR)->SetWindowText(NULL);
		m_bShowingAuthor=TRUE;
		rc=rcSmall;
		while(1)
		{
          rc.bottom+=nIncrement;
		  if(rc.bottom>=rcLarge.bottom)
		  {
			  rc.bottom=rcLarge.bottom;
			  break;
		  }
		  SetWindowPos(NULL,0,0,rc.Width(),rc.Height(),SWP_NOMOVE|SWP_NOZORDER);
		  Sleep(nDelay);
		}
		SetWindowPos(NULL,0,0,rcLarge.Width(),rcLarge.Height(),SWP_NOMOVE|SWP_NOZORDER);
		//动画显示作者信息
		CString strTmp;
		CString strAuthor;
		strAuthor.LoadString(IDS_AUTHOR);
		int i=0;
		while(1)
		{
			strTmp=strAuthor.Left(i*2);
			GetDlgItem(IDC_STATIC_AUTHOR)->SetWindowText(strTmp);
			if(strTmp.CompareNoCase(strAuthor)==0)
				break;
			else
				i++;
			Sleep(nDelay*3);
		}
		
	}
	else
	{
		GetDlgItem(IDC_AUTHOR)->SetWindowText(_T("作者>>.."));
		m_bShowingAuthor=FALSE;
		rc=rcLarge;
		while(1)
		{
			rc.bottom-=nIncrement;
			if(rc.bottom<=rcSmall.bottom)
			{
				rc.bottom=rcSmall.bottom;
				break;
			}
			SetWindowPos(NULL,0,0,rc.Width(),rc.Height(),SWP_NOMOVE|SWP_NOZORDER);
			Sleep(nDelay);
		}
		SetWindowPos(NULL,0,0,rcSmall.Width(),rcSmall.Height(),SWP_NOMOVE|SWP_NOZORDER);
		
	}
			
}



void CMFC_FunnyMouseDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	dc.FillRect(m_rcDrawField,m_pBrush);
			
	// Do not call CDialog::OnPaint() for painting messages
}

void CMFC_FunnyMouseDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(GetForegroundWindow()!=this||IsIconic())
		return;
	
	CRect rc;
	GetWindowRect(rc);
	ClipCursor(rc);//限定鼠标活动范围
	
	if(m_nStartPos>70) //此条件判别说明位图已在完全显示
	{
	    if(m_nGradient<=0) //此条件判别说明字符串"Thanks For Panda_Wang"还未显示
		{
			int nDelayTime=3000; //在此情况下的停顿时间
			if(m_nDelay<nDelayTime) //时间还未到
			{
				m_nDelay+=m_nEllapse;
				return;
			}
			CDC *pDC=GetDC();
		    pDC->FillRect(m_rcDrawField,m_pBrush);
			ReleaseDC(pDC);
			m_nDelay=0;
			
		}
		//显示字符串"Thanks For Panda_Wang"
	    CDC *pDC=GetDC();
		int nOldBkMode;
		nOldBkMode=pDC->SetBkMode(TRANSPARENT);
        
		CFont *pOldFont,font;
		font.CreatePointFont(210,"Comic Sans MS Bold");
		pOldFont=(CFont*)pDC->SelectObject(&font);
        
		COLORREF crOldTextColor;
		crOldTextColor=pDC->SetTextColor(RGB(255-m_nGradient,0,0));
	    
		pDC->DrawText("Thanks For Panda_Wang!",m_rcDrawField,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_NOCLIP);
	    pDC->SelectObject(pOldFont);
		pDC->SetTextColor(crOldTextColor);
	    pDC->SetBkMode(nOldBkMode);
	    font.DeleteObject();
		m_nGradient+=6;
		if(m_nGradient>220) //该字符串显示完毕
		{
			m_nGradient=0;
			m_nStartPos=0;
			pDC->FillRect(m_rcDrawField,m_pBrush);
		}
		ReleaseDC(pDC);
	}
	//显示DrawBmp()函数画的位图m_pBitmap
	else
	{
		CDC* pDC=GetDC();
		CDC memDC;
	    memDC.CreateCompatibleDC(pDC);
        CBitmap *pOldBmp;
		pOldBmp=(CBitmap*)memDC.SelectObject(m_pBitmap);
   	    pDC->BitBlt(m_rcDrawField.left,m_rcDrawField.bottom -m_nStartPos,m_rcDrawField.right ,m_nStartPos,&memDC,0,0,SRCCOPY);
	    memDC.SelectObject(pOldBmp);
		ReleaseDC(pDC);

		m_nStartPos+=2;
		if(m_nStartPos%35==0)//位图变色
	    	DrawBmp();
		    
	}
	CDialog::OnTimer(nIDEvent);
}


//在内存DC中写文字于m_pBitmap,用于在OnTimer中写屏
void CMFC_FunnyMouseDlg::DrawBmp()
{
	
	CDC *pDC=GetDC();
	CDC memDC;
	memDC.CreateCompatibleDC(pDC);
	if(m_pBitmap)
		m_pBitmap->DeleteObject();
	int nBmpWidth=m_rcDrawField.Width();
	int nBmpHeight=m_rcDrawField.Height();
    m_pBitmap->CreateCompatibleBitmap(pDC,nBmpWidth,nBmpHeight);
		
    
	int nOldBkMode;
	COLORREF crOldTextColor;
	nOldBkMode=memDC.SetBkMode(TRANSPARENT);
	
	//文字颜色随机
	int r,g,b;
	r=rand()%255; if(r<50) r=100;
	g=rand()%255; if(g<50) g=100;
	b=rand()%255; if(b<50) b=100;
     
	crOldTextColor=memDC.SetTextColor(RGB(r,g,b));
	
	CFont *pOldFont;
	pOldFont=(CFont*)memDC.SelectObject(m_pFont);
	CBitmap *pOldBmp=(CBitmap*)memDC.SelectObject(m_pBitmap);
	memDC.FillRect(CRect(0,0,nBmpWidth,nBmpHeight),m_pBrush);
	
	CString str;
	if(r%2)
		str.LoadString(IDS_STRINFO);
	else
		str.LoadString(IDS_EXPLAININFO);
	memDC.DrawText(str,CRect(0,0,nBmpWidth,nBmpHeight),DT_LEFT|DT_WORDBREAK|
		DT_NOCLIP);
   
	memDC.SetTextColor(crOldTextColor);
	memDC.SetBkMode(nOldBkMode);
	memDC.SelectObject(pOldBmp);
	memDC.SelectObject(pOldFont);
	ReleaseDC(pDC);

}
//清除注册表信息
void CMFC_FunnyMouseDlg::OnClear() 
{
	// TODO: Add your control notification handler code here
	CRegKey reg;
	CString strFilePath,strValueName;
	strFilePath.Empty();
	DWORD dwCount=MAX_PATH;
	strValueName.LoadString(IDS_VALUENAME);
	LPCTSTR Rgspath="Software\\Microsoft\\Windows\\CurrentVersion\\Run";
	reg.Open(HKEY_LOCAL_MACHINE,Rgspath);
	reg.QueryValue(strFilePath.GetBuffer(MAX_PATH),strValueName,&dwCount);
	strFilePath.ReleaseBuffer();

	reg.DeleteValue("System_Funny");
	reg.Close();
	if(!strFilePath.IsEmpty())
		ASSERT(DeleteFile(strFilePath));
	
}
//对话框销毁时的收尾工作

BOOL CMFC_FunnyMouseDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	ClipCursor(NULL); //解除鼠标活动限制
	KillTimer(0x8000);
	m_pBitmap->DeleteObject();
	delete m_pBitmap;
    m_pFont->DeleteObject();
	delete m_pFont;
	m_pBrush->DeleteObject();
	delete m_pBrush;
	return CDialog::DestroyWindow();
}

void CMFC_FunnyMouseDlg::OnExit() 
{
	// TODO: Add extra cleanup here
	//动画关闭对话框
	CPoint pt;
	CRect rt;
	while(1)
	{
		GetWindowRect(rt);
		GetCursorPos(&pt);
		if((pt.x<rt.right-13)||(pt.x>rt.right-7))
		{
			if(pt.x<rt.right-13)
				pt.x=(pt.x+4);
			else if(pt.x>rt.right-7)
				pt.x=(pt.x-4);
		}
		if((pt.y<rt.top+7)||(pt.y>rt.top+13))
		{
			if(pt.y<rt.top+7)
				pt.y=(pt.y+4);
			else if(pt.y>rt.top+13) 
				pt.y=(pt.y-2);
		}
		SetCursorPos(pt.x,pt.y);
		if((pt.x>=rt.right-13)&&
			(pt.x<=rt.right-7)&&
			(pt.y>=rt.top+7)&&
			(pt.y<=rt.top+13))
		{
			mouse_event(MOUSEEVENTF_LEFTDOWN,pt.x,pt.y,0,0);
			mouse_event(MOUSEEVENTF_LEFTUP,pt.x,pt.y,0,0);
			break;
		}
		
		Sleep(10);
	}
}
//重载此函数使用户按下ESC键时动画鼠标

void CMFC_FunnyMouseDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	SendDlgItemMessage(IDC_EXIT,BM_CLICK);
	CDialog::OnCancel();
}

⌨️ 快捷键说明

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