www.pudn.com > guess.rar > guessDlg.cpp


// guessDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "guess.h" 
#include "guessDlg.h" 
#include"DialogDlg.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// 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) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CGuessDlg dialog 
 
CGuessDlg::CGuessDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CGuessDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CGuessDlg) 
	m_num0 = 0; 
	m_num1 = 0; 
	m_num2 = 0; 
	m_num3 = 0; 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CGuessDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CGuessDlg) 
	DDX_Control(pDX, IDC_BUTTON2, m_guess); 
	DDX_Control(pDX, IDC_BUTTON3, m_restart); 
	DDX_Control(pDX, IDC_BUTTON1, m_start); 
	DDX_Text(pDX, IDC_EDIT1, m_num0); 
	DDV_MinMaxInt(pDX, m_num0, 0, 9); 
	DDX_Text(pDX, IDC_EDIT2, m_num1); 
	DDV_MinMaxInt(pDX, m_num1, 0, 9); 
	DDX_Text(pDX, IDC_EDIT3, m_num2); 
	DDV_MinMaxInt(pDX, m_num2, 0, 9); 
	DDX_Text(pDX, IDC_EDIT4, m_num3); 
	DDV_MinMaxInt(pDX, m_num3, 0, 9); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CGuessDlg, CDialog) 
	//{{AFX_MSG_MAP(CGuessDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1) 
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2) 
	ON_WM_TIMER() 
	ON_BN_CLICKED(IDC_BUTTON3, OnRestart) 
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4) 
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CGuessDlg message handlers 
//srand((unsigned)time(NULL)); 
BOOL CGuessDlg::OnInitDialog() 
{ 
	 
	CDialog::OnInitDialog(); 
    m_restart.EnableWindow(0); 
    m_guess.EnableWindow(0); 
	// 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 
	 
	// TODO: Add extra initialization here*/ 
	CRect dlgRect;  
	GetWindowRect(dlgRect);  
	CRect desktopRect;  
	GetDesktopWindow()->GetWindowRect(desktopRect);  
	MoveWindow(  
	(desktopRect.Width() - dlgRect.Width()) / 2,  
	(desktopRect.Height() - dlgRect.Height()) / 2,  
	dlgRect.Width(), dlgRect.Height() );  
	MoveWindow(  
	(desktopRect.Width() - dlgRect.Width()) / 2,  
	(desktopRect.Height() - dlgRect.Height()) / 2,  
	0,  
	0 );  
	nWidth=dlgRect.Width();  
	nHeight=dlgRect.Height();  
	dx=8; // you can modify this  
	dy=4; // you can modify this  
	::SetTimer(this->m_hWnd, IDT_TIMER3,10 , NULL);  
	m_stStatic[1].Create("", WS_CHILD|WS_VISIBLE, CRect(10, 25, 140, 45), this); 
	m_stStatic[2].Create("", WS_CHILD|WS_VISIBLE, CRect(10, 45, 140, 65), this); 
    m_stStatic[0].Create("", WS_CHILD|WS_VISIBLE, CRect(10, 5, 140, 25), this); 
	m_stStatic[3].Create("", WS_CHILD|WS_VISIBLE, CRect(10, 65, 140, 85), this); 
    m_stStatic[4].Create("", WS_CHILD|WS_VISIBLE, CRect(10, 85, 140, 105), this); 
	m_stStatic[5].Create("", WS_CHILD|WS_VISIBLE, CRect(10, 105, 140, 125), this); 
	m_stStatic[6].Create("", WS_CHILD|WS_VISIBLE, CRect(10, 125, 140, 145), this); 
    m_stStatic[8].Create("", WS_CHILD|WS_VISIBLE, CRect(180, 25, 350, 45), this); 
    m_stStatic[9].Create("耗时", WS_CHILD|WS_VISIBLE, CRect(140, 25, 180, 45), this); 
	m_stStatic[10].Create("耗步", WS_CHILD|WS_VISIBLE, CRect(140, 45, 180, 65), this); 
    m_stStatic[11].Create("", WS_CHILD|WS_VISIBLE, CRect(180, 45, 350, 65), this); 
    m_stStatic[12].Create("", WS_CHILD|WS_VISIBLE, CRect(140, 65, 180, 85), this); 
	m_stStatic[13].Create("", WS_CHILD|WS_VISIBLE, CRect(180, 65, 350, 85), this); 
	m_stStatic[7].Create("按开始键猜吧", WS_CHILD|WS_VISIBLE, CRect(140, 5, 340, 25), this); 
     
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CGuessDlg::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 CGuessDlg::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 
	{ 
		CDialog::OnPaint(); 
		 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CGuessDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CGuessDlg::OnButton1()  
{ 
	if(m_uTimer)//计时器成功 
	KillTimer(m_uTimer); 
    //min=sec=total=0; 
    //m_uTimer = SetTimer(100, 1000, NULL); 
    min=sec=total=0; 
	m_start.EnableWindow(0); 
    m_guess.EnableWindow(1); 
    a=0; 
    b=0; 
    c=0; 
    num[1]=rand()%10; 
    num[2]=rand()%10;      
    num[3]=rand()%10; 
    num[0]=rand()%10; 
    CString stt; 
    stt.Format("%d步",c); 
	//gra.Format("%d分",total); 
    m_stStatic[11].SetWindowText(stt); 
    m_stStatic[8].SetWindowText(""); 
	m_stStatic[13].SetWindowText(""); 
    while((num[1]==num[2])||(num[3]==num[2])||(num[3]==num[0])||(num[1]==num[0])||(num[1]==num[3])||(num[0]==num[2])) 
	{ 
      num[1]=rand()%10; 
      num[2]=rand()%10; 
      num[3]=rand()%10; 
      num[0]=rand()%10; 
	} 
/*CString str; 
str.Format("%d-%d-%d-%d",num[0],num[1],num[2],num[3]); 
AfxMessageBox(str); */ 
 
 
    m_stStatic[7].SetWindowText("猜吧"); 
    for(int i=0;i<7;i++) 
    m_stStatic[i].SetWindowText(""); 
} 
 
void CGuessDlg::OnButton2()  
{ 
   int arr[4]; 
   UpdateData(); 
   arr[0]=m_num0; 
   arr[1]=m_num1; 
   arr[2]=m_num2; 
   arr[3]=m_num3; 
   if((arr[0]==arr[1])||(arr[0]==arr[2])||(arr[0]==arr[3])||(arr[1]==arr[2])||(arr[1]==arr[3])||(arr[2]==arr[3])) 
   { 
	 AfxMessageBox("输入错误"); 
 
   } 
   else 
   { 
     if(m_uTimer)//计时器成功 
	KillTimer(m_uTimer); 
     
    m_uTimer = SetTimer(100, 1000, NULL); 
	   m_stStatic[7].SetWindowText("按重新开始键或继续猜"); 
	 m_restart.EnableWindow(1); 
     if(c==7) 
	 { 
       CString ans; 
       ans.Format("傻B你输了%d-%d-%d-%d",num[0],num[1],num[2],num[3]); 
       m_stStatic[7].SetWindowText(ans); 
		 
		KillTimer(m_uTimer); 
    m_start.EnableWindow(1); 
	m_restart.EnableWindow(0); 
	m_guess.EnableWindow(0); 
	 } 
   if(c!=7) 
   { 
    a=b=0; 
	   for(int x=0;x<4;x++) 
	{ 
       if(arr[x]==num[x])a++; 
       for(int y=0;y<4;y++) 
	   { 
         if((arr[x]==num[y])&&(x!=y))b++; 
	   } 
	} 
    c++; 
	CString sText,stt; 
	sText.Format("%d-%d-%d-%d第%d步%dA%dB",arr[0],arr[1],arr[2],arr[3],c,a,b); 
	m_stStatic[c-1].SetWindowText(sText); 
	stt.Format("%d步",c); 
	m_stStatic[11].SetWindowText(stt); 
    if(a==4) 
	{ 
	CString sText="狗屎运不错嘛居然赢了"; 
	KillTimer(m_uTimer); 
	total=7-c+60*(9-min)+59-sec; 
	CString stt,gra; 
	stt.Format("%d步",c); 
	gra.Format("%d分",total); 
	m_stStatic[9].SetWindowText("耗时"); 
	m_stStatic[10].SetWindowText("耗步"); 
	m_stStatic[11].SetWindowText(stt); 
	m_stStatic[12].SetWindowText("得分"); 
	m_stStatic[13].SetWindowText(gra); 
    m_stStatic[7].SetWindowText(sText); 
	m_start.EnableWindow(1); 
	m_restart.EnableWindow(0); 
	m_guess.EnableWindow(0); 
	} 
 
} 
}   
 
 
 
 
} 
 
void CGuessDlg::WinHelp(DWORD dwData, UINT nCmd)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	 
	CDialog::WinHelp(dwData, nCmd); 
} 
 
BOOL CGuessDlg::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)  
{ 
	//AfxMessageBox("oo");// TODO: Add your specialized code here and/or call the base class 
	 
	return CDialog::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo); 
} 
 
void CGuessDlg::OnTimer(UINT nIDEvent)  
{ 
	CRect dlgRect; 
	GetWindowRect(dlgRect); 
 
	CRect desktopRect; 
	GetDesktopWindow()->GetWindowRect(desktopRect); 
 
 
	if(nIDEvent == IDT_TIMER3) 
	{ 
	 
		 
		MoveWindow( 
		(-dx+desktopRect.Width() - dlgRect.Width()) / 2, 
		(-dy+desktopRect.Height() - dlgRect.Height()) / 2,  
		 +dx+dlgRect.Width(), 
		 +dy+dlgRect.Height() ); 
 
		if(dlgRect.Width() >=nWidth)  
			dx=0; // do not over grow 
		if(dlgRect.Height() >=nHeight) 
			dy=0; // do not over grow 
		 
	} 
	 
 
	if((dlgRect.Width() >=nWidth) && (dlgRect.Height() >=nHeight)) 
		::KillTimer(this->m_hWnd, IDT_TIMER3); //Stop the timer 
 
    if(nIDEvent != IDT_TIMER3) 
	{sec=sec+1; 
	if(sec==60){sec=0;min++;} 
	CString str; 
    str.Format("%d分%d秒",min,sec); 
    m_stStatic[8].SetWindowText(str);}// TODO: Add your message handler code here and/or call default 
	 
	CDialog::OnTimer(nIDEvent); 
} 
 
void CGuessDlg::OnRestart()  
{ 
		if(m_uTimer)//计时器成功 
		KillTimer(m_uTimer); 
min=sec=0; 
//m_uTimer = SetTimer(100, 1000, NULL); 
 
m_restart.EnableWindow(0); 
m_start.EnableWindow(0); 
m_guess.EnableWindow(1); 
a=0; 
b=0; 
c=0; 
num[1]=rand()%10; 
num[2]=rand()%10; 
num[3]=rand()%10; 
num[0]=rand()%10; 
 
while((num[1]==num[2])||(num[3]==num[2])||(num[3]==num[0])||(num[1]==num[0])||(num[1]==num[3])||(num[0]==num[2])) 
{ 
num[1]=rand()%10; 
num[2]=rand()%10; 
num[3]=rand()%10; 
num[0]=rand()%10; 
} 
/*CString str; 
str.Format("%d-%d-%d-%d",num[0],num[1],num[2],num[3]); 
AfxMessageBox(str); */ 
m_stStatic[11].SetWindowText("0步"); 
    m_stStatic[8].SetWindowText(""); 
	m_stStatic[13].SetWindowText(""); 
 
m_stStatic[7].SetWindowText("猜吧"); 
for(int i=0;i<7;i++) 
m_stStatic[i].SetWindowText(""); 
}// TODO: Add your control notification handler code here 
	 
 
 
void CGuessDlg::OnButton4()  
{ 
   CDialogDlg dlg; 
   if(dlg.DoModal()==IDOK) 
			   { 
				    
			   } 
			   else 
			   { 
			   }// TODO: Add your control notification handler code here 
	 
} 
 
void CGuessDlg::OnButton5()  
{ 
	CAboutDlg dlg; 
	if(dlg.DoModal()==IDOK) 
			   { 
				    
			   } 
			   else 
			   { 
			   }// TODO: Add your control notification handler code here 
	 
}