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

📄 guessnumdlg.cpp

📁 自制 猜数字游戏 mfc程序 记录游戏成绩
💻 CPP
字号:
// guessNumDlg.cpp : implementation file
//

#include "stdafx.h"
#include "guessNum.h"
#include "guessNumDlg.h"

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

int Rand(int max)
{
	static once=1;
	if(once==1)
	{
		time_t t; time(&t);
		srand(t);
	}
	once=0;
	int ret=rand()%(max+1);
	return ret;
}
/////////////////////////////////////////////////////////////////////////////
// CGuessNumDlg dialog

CGuessNumDlg::CGuessNumDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGuessNumDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGuessNumDlg)
	showanswer = _T("");
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGuessNumDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGuessNumDlg)
	DDX_Control(pDX, IDC_INPUT, m_editInput);
	DDX_Text(pDX, IDC_ANSWER, showanswer);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGuessNumDlg, CDialog)
	//{{AFX_MSG_MAP(CGuessNumDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDNEW, OnNew)
	ON_BN_CLICKED(IDOK2, OnOK)
	ON_BN_CLICKED(IDC_BUTTON12, OnOK)
	ON_BN_CLICKED(IDC_ENTER, OnOK)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_COMMAND_RANGE(2000,2000+9, OnNumBtn)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGuessNumDlg message handlers
LRESULT CALLBACK KeyboardPorc(int code,WPARAM w,LPARAM l)
{
	if(w>='A' && w<='Z' ||w==VK_ESCAPE)
		return 1;
	return 0;
}
BOOL CGuessNumDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	time=0;this->SetDlgItemInt(IDC_TIMER,time);
	//SetTimer(1,1000,NULL);
	hHook=SetWindowsHookEx(WH_KEYBOARD,KeyboardPorc,NULL,GetCurrentThreadId());
	for(int i=0;i<4;i++){//init number
INITNUM:
		m_answerNum[i]=Rand(9);
		for(int j=0;j<i;j++)
			if(m_answerNum[i]==m_answerNum[j])
			goto INITNUM;
	}
	guesstime=0;
	out="";	//InvalidateRect(new CRect(20,30,1000,1000));
	GetDlgItem(IDOK2)->EnableWindow(true);
	GetDlgItem(IDC_ENTER)->EnableWindow(true);
	m_editInput.SetFocus();
	m_editInput.SetLimitText(4);
	lastinput="";
	return false;  // 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 CGuessNumDlg::OnPaint() 
{
	CPaintDC dc(this);

	CRect rect=*(new CRect(20,30,1000,1000));

	dc.SetBkMode(TRANSPARENT);
	dc.DrawText(out,rect,0);
	
	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
	{
		CDialog::OnPaint();
	}
}

HCURSOR CGuessNumDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CGuessNumDlg::OnOK() 
{

	m_editInput.SetFocus();
	CString input;
	GetDlgItemText(IDC_INPUT,input);
	m_editInput.SetSel(0,input.GetLength());
//	if(input==lastinput) return;
	lastinput=input;
	if (input.GetLength()!=4)
		return;
	int i,j;
	for(i=0;i<4;i++)
	{
		if(input.GetAt(i)<'0'||input.GetAt(i)>'9')
			return;
		m_guessNum[i]=input.GetAt(i)-'0';
	}
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			if(i!=j&&m_guessNum[i]==m_guessNum[j])
				return;
	                     
//	m_editInput.SetSel(0,4);//is a validate input
	showanswer="";   // you giveup and show answer   there clear
	UpdateData(false);

	int aright,bright;
	aright=bright=0;
	for(i=0;i<4;i++)
		if(m_answerNum[i]==m_guessNum[i])
			aright++;		
	for( i=0;i<4;i++)
	{
		for(j=0;j<4;j++)
			if(m_answerNum[i]==m_guessNum[j]&&j!=i)
				bright++;
	}
	char buf[500];
	guesstime++;
	if(guesstime==1)          //when guess frist start timer
		SetTimer(1,1000,NULL);
	sprintf(buf,"第%2d次 %d %d %d %d  %dA%dB\n",guesstime,m_guessNum[0],m_guessNum[1],m_guessNum[2],m_guessNum[3],
		aright,bright);
	
	out+=buf;
	if(aright==4)   // win
	{
		out+="你赢了^-^";
		notgiveup=1;
		char buf[100];
		sprintf(buf,"你赢了 答案是: %d%d%d%d",m_answerNum[0],m_answerNum[1],m_answerNum[2],m_answerNum[3]);
		showanswer=buf;
		UpdateData(false);
		KillTimer(1);

		InvalidateRect(new CRect(20,30,1000,1000));
		GetDlgItem(IDOK2)->EnableWindow(false);
		GetDlgItem(IDC_ENTER)->EnableWindow(false);
		((CGuessNumApp*)AfxGetApp())->dat.datnew=guesstime;	
		UnhookWindowsHookEx(hHook);
		((CGuessNumApp*)AfxGetApp())->dat.timenew=time;
		((CGuessNumApp*)AfxGetApp())->dat.NewData();
	}
	if(guesstime>11)
	{
		out+="你输了^-^";
		KillTimer(1);
		GetDlgItem(IDOK2)->EnableWindow(false);
		GetDlgItem(IDC_ENTER)->EnableWindow(false);
		char buf[100];
		sprintf(buf,"次数太多了 答案是: %d%d%d%d",m_answerNum[0],m_answerNum[1],m_answerNum[2],m_answerNum[3]);
		showanswer=buf;
		UpdateData(false);
		notgiveup=1;  //mean  not giveup yourself
	}
	InvalidateRect(new CRect(20,30,1000,1000));
	}


void CGuessNumDlg::OnNew() 
{
	KillTimer(1);
	if(notgiveup!=1){
		char buf[100];
		sprintf(buf,"你放弃了 答案是: %d%d%d%d",m_answerNum[0],m_answerNum[1],m_answerNum[2],m_answerNum[3]);
		showanswer=buf;		
	}  notgiveup=0;
	UpdateData(false);
	OnInitDialog();	
}


void CGuessNumDlg::OnNumBtn(int id)
{
	
/*	((CGuessNumApp*)AfxGetApp())->dat.datnew=(guesstime+=2);	
 		UnhookWindowsHookEx(hHook);
		((CGuessNumApp*)AfxGetApp())->input.DoModal();*/  //测试记录
	int num;
	num=GetDlgItemInt(id);
		char tmp[10]=""; itoa(num,tmp,10);
	//MessageBox(tmp);
	CString text;
	m_editInput.GetWindowText(text);
	int start,end;
	m_editInput.GetSel(start,end);
	text.Delete(start,end-start);
	text+=tmp;
	m_editInput.SetWindowText(text);
}

void CGuessNumDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(this->GetActiveWindow()!=this)
		return;
	time++;
	this->SetDlgItemInt(IDC_TIMER,time);
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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