个人技术分享

新建一个login工程项目对话框,主对话框IDD_LOGIN_DIALOG中一个显示按钮IDC_BUTTON1、一个密码按钮IDC_BUTTON2。添加一个密码对话框IDD_DIALOG1,添加类password,在对话框中添加一个编辑框IDC_EDIT1、一个确定按钮IDC_BUTTON1。
程序功能:运行程序显示按钮灰色不可用,点击密码按钮弹出密码对话框,在编辑框中输入WHCC点击确定按钮显示按钮可用。
在这里插入图片描述
在这里插入图片描述
主要代码如下:`
l1、oginDlg.cpp中

BOOL CLoginDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	...
	GetDlgItem(IDC_BUTTON1)->EnableWindow(false);
	return TRUE;  // return TRUE  unless you set the focus to a control
}
void CLoginDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	AfxMessageBox("按钮可用");
}

void CLoginDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	password dlg;
	dlg.DoModal();
	CString pass=dlg.LOGIN;
  if (pass=="WHCC") 
  {
 GetDlgItem(IDC_BUTTON1)->EnableWindow(true);
  }
  else
  {
	  GetDlgItem(IDC_BUTTON1)->EnableWindow(false);
  }


}

2、password.h中添加一个公共变量

class password : public CDialog
{
// Construction
public:
	password(CWnd* pParent = NULL);   // standard constructor
	CString LOGIN;
	...
}

3、

void password::OnButton1() 
{
	// TODO: Add your control notification handler code here

	GetDlgItem(IDC_EDIT1)->GetWindowText(LOGIN);
	if (LOGIN=="WHCC")
	{
		UpdateData(false);
		EndDialog(0);
	}
	else
	{
		MessageBox("输入密码有误!");
	GetDlgItem(IDC_EDIT1)->SetFocus();
	}

}

运行程序

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
源码下载