// guess_numberDlg.cpp : implementation file
//
#include "stdafx.h"
#include "guess_number.h"
#include "guess_numberDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CGuess_numberDlg dialog
CGuess_numberDlg::CGuess_numberDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGuess_numberDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGuess_numberDlg)
m_list = _T("");
m_num = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CGuess_numberDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGuess_numberDlg)
DDX_Control(pDX, ID_Start, m_start);
DDX_Text(pDX, IDC_List, m_list);
DDX_Text(pDX, IDC_InputNum, m_num);
DDV_MinMaxInt(pDX, m_num, 0, 999);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGuess_numberDlg, CDialog)
//{{AFX_MSG_MAP(CGuess_numberDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_Guess, OnGuess)
ON_BN_CLICKED(IDC_See, OnSee)
ON_BN_CLICKED(ID_Start, OnStart)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGuess_numberDlg message handlers
BOOL CGuess_numberDlg::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
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CGuess_numberDlg::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 CGuess_numberDlg::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 CGuess_numberDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CGuess_numberDlg::OnGuess()
{
// TODO: Add your control notification handler code here
UpdateData(true);
CString str;
if(m_num>rnd)
str="太大了";
else if(m_num<rnd)
str="太小了";
else
{str="恭喜猜中";
m_start.EnableWindow(true);}
m_list=m_list+str+"\r\n";
UpdateData(false);
}
void CGuess_numberDlg::OnSee()
{
// TODO: Add your control notification handler code here
CString str;
str.Format("随机数:%d",rnd);
MessageBox(str);
}
void CGuess_numberDlg::OnStart()
{
// TODO: Add your control notification handler code here
srand(time(NULL));
rnd=rand()%1000;
m_start.EnableWindow(false);
m_list="";
m_num=0;
UpdateData(false);
}
183行UpdateData(true); 会触发73行DoDataExchange函数,在DoDataExchange中DDV_MinMaxInt(pDX, m_num, 0, 999);验证数据有效性时会弹出提示框。 如果你不想要这个提示框,可以不使用DDV_MinMaxInt,而是写自己的验证逻辑。