📄 employeeinfodlg.cpp
字号:
// EmployeeInfoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "EmployeeInfo.h"
#include "EmployeeInfoDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CEmployeeInfoDlg dialog
CEmployeeInfoDlg::CEmployeeInfoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEmployeeInfoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEmployeeInfoDlg)
m_csName = _T("");
m_csSex = _T("");
m_csAge = _T("");
m_csTelephone = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEmployeeInfoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEmployeeInfoDlg)
DDX_Text(pDX, IDC_EDIT_NAME, m_csName);
DDX_Text(pDX, IDC_EDIT_SEX, m_csSex);
DDX_Text(pDX, IDC_EDIT_AGE, m_csAge);
DDX_Text(pDX, IDC_EDIT_TELEPHONE, m_csTelephone);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEmployeeInfoDlg, CDialog)
//{{AFX_MSG_MAP(CEmployeeInfoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_VALITATE, OnButtonValitate)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEmployeeInfoDlg message handlers
BOOL CEmployeeInfoDlg::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 CEmployeeInfoDlg::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 CEmployeeInfoDlg::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 CEmployeeInfoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEmployeeInfoDlg::OnButtonValitate()
{
UpdateData();
//验证函数
bool bIsEnglishChar = CheckEnglishLetter(m_csSex,2);
bool bIsDigit = CheckDigit(m_csAge,3);
bool bIsValidTelephone = CheckTelNumber(m_csTelephone);
//判断员工信息的有效性
if (m_csName.IsEmpty()) //员工姓名
{
AfxMessageBox(_T("姓名不能为空!"));
return;
}
if (m_csSex.IsEmpty()) //员工性别
{
AfxMessageBox(_T("性别不能为空!"));
return;
}
else if (!bIsEnglishChar)
{
AfxMessageBox(_T("你输入的员工性别不是英文字符!"));
ClearEmployeeInfo(IDC_EDIT_SEX);
return;
}
else if ( 0 != m_csSex.Compare(_T("M")) &&
0 != m_csSex.Compare(_T("FM")))
{
AfxMessageBox(_T("你输入的员工性别无效,应该是M或FM!"));
ClearEmployeeInfo(IDC_EDIT_SEX);
return;
}
if (m_csAge.IsEmpty()) //员工年龄
{
AfxMessageBox(_T("年龄不能为空!"));
return;
}
else if (!bIsDigit)
{
AfxMessageBox(_T("你输入的员工年龄不是数字!"));
ClearEmployeeInfo(IDC_EDIT_AGE);
return;
}
else if (0 < m_csAge.Compare(_T("150")))
{
AfxMessageBox(_T("你输入的员工年龄不能大于150!"));
ClearEmployeeInfo(IDC_EDIT_AGE);
return;
}
if (m_csTelephone.IsEmpty()) //员工电话
{
AfxMessageBox(_T("电话不能为空!"));
return;
}
else if (!bIsValidTelephone)
{
ClearEmployeeInfo(IDC_EDIT_TELEPHONE);
return;
}
}
bool CEmployeeInfoDlg::CheckDigit(CString csTargetString,int nLength)
{
int nTmpLength = 0;
if (nLength >= csTargetString.GetLength())
{
nTmpLength = csTargetString.GetLength();
}
else
{
nTmpLength = nLength;
}
for (int i = 0; i < nTmpLength; i++)
{
TCHAR c = csTargetString.GetAt(i) ;
//判断输入的字符是否是数字
if(!isdigit(c))
{
return false;
}
}
return true;
}
bool CEmployeeInfoDlg::CheckEnglishLetter(CString csTargetString,int nLength)
{
int nTmpLength = 0;
if (nLength >= csTargetString.GetLength())
{
nTmpLength = csTargetString.GetLength();
}
else
{
nTmpLength = nLength;
}
for (int i = 0; i < nTmpLength; i++)
{
TCHAR c = csTargetString.GetAt(i) ;
//判断输入的字符是否是英文字符
if(!isalpha(c))
{
return false;
}
}
return true;
}
bool CEmployeeInfoDlg::CheckTelNumber(CString csTargetString)
{
int nLength = csTargetString.GetLength();
if (nLength > 8)
{
AfxMessageBox(_T("你输入的员工电话号码的长度不能大于8,\r\n应该是形如123-5678"));
return false;
}
else
{
for (int i = 0; i < nLength; i++)
{
TCHAR c = csTargetString.GetAt(i) ;
//判断输入的字符是否是数字或连字符
if(c <48 ||c >57 )
{
return false;
}
//判断输入的字符串的第4位是否是连字符
if ( i == 3 && c!='-')
{
AfxMessageBox(_T("你输入的员工电话号码的第四位必须是连字符\'-',\r\n应该是形如123-5678"));
return false;
}
}
}
return true;
}
void CEmployeeInfoDlg::ClearEmployeeInfo(int nID)
{
GetDlgItem(nID)->SetWindowText(_T("")); //设定文本框为空
GetDlgItem(nID)->SetFocus();//设定焦点
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -