用户登录的实现
class CLearn18Dlg : public CDialog
{
// 构造
public:
CLearn18Dlg(CWnd* pParent = NULL);// 标准构造函数
// 对话框数据
enum { IDD = IDD_LEARN18_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX);// DDX/DDV 支持
public:
virtual void OnOK();
virtual void OnCancel();
// 实现
protected:
HICON m_hIcon;
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
// 用户名 编辑框
CEdit m_wndUser;
// 用户密码 编辑框
CEdit m_wndPwd;
// 用户名 字符串
CString m_strUser;
// 用户密码 字符串
CString m_strPwd;
// 保存密码 多选框
CButton m_wndSave;
// 自动登录 多选框
CButton m_wndAuto;
// 保存密码 标志位
UINT nSave;
// 自动登录 标志位
UINT nAuto;
private:
_ConnectionPtr m_pCon;
_RecordsetPtr m_pRecordset;
};
// Learn18Dlg.cpp : 实现文件
CLearn18Dlg::CLearn18Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CLearn18Dlg::IDD, pParent),nSave(0),nAuto(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_strUser = "";
m_strPwd = "";
}
void CLearn18Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_NAME, m_wndUser);
DDX_Control(pDX, IDC_PWD, m_wndPwd);
DDX_Text(pDX, IDC_NAME, m_strUser);
DDX_Text(pDX, IDC_PWD, m_strPwd);
DDX_Control(pDX, IDC_SAVE, m_wndSave);
DDX_Control(pDX, IDC_AUTO, m_wndAuto);
}
BOOL CLearn18Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE);// 设置大图标
SetIcon(m_hIcon, FALSE);// 设置小图标
// TODO: 在此添加额外的初始化代码
AfxOleInit();
try
{
HRESULT hr = m_pCon.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
_bstr_t ConnectionString = "Provider=MSDASQL.1;Data Source=user";
m_pCon->Open(ConnectionString,"","",adModeUnknown);
}
}
catch(_com_error e)
{
CString strError;
strError.Format("%s",e.ErrorMessage());
AfxMessageBox(strError);
}
LPSTR lpPath = new CHAR[MAX_PATH];
strcpy(lpPath,"C:\\admin.ini");
GetPrivateProfileString("LoginInfo","username","",(LPSTR)(LPCTSTR)m_strUser,m_strUser.GetLength(),lpPath);
GetPrivateProfileString("LoginInfo","userpwd","",(LPSTR)(LPCTSTR)m_strPwd,m_strPwd.GetLength(),lpPath);
nSave = GetPrivateProfileInt("LoginInfo","nSave",0,lpPath);
nAuto = GetPrivateProfileInt("LoginInfo","nAuto",0,lpPath);
if(1 == nSave)
m_wndSave.SetCheck(TRUE);
else
m_wndSave.SetCheck(FALSE);
if(1 == nAuto)
m_wndAuto.SetCheck(TRUE);
else
m_wndAuto.SetCheck(FALSE);
AfxMessageBox(m_strUser);
return TRUE; // 除非设置了控件的焦点,否则返回 TRUE
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CLearn18Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CLearn18Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CLearn18Dlg::OnOK()
{
UpdateData(TRUE);
LPSTR lpPath = new CHAR[MAX_PATH];
strcpy(lpPath,"C:\\admin.ini");
WritePrivateProfileString("LoginInfo","username",m_strUser,lpPath);
WritePrivateProfileString("LoginInfo","userpwd",m_strPwd,lpPath);
if(m_wndSave.GetCheck())
WritePrivateProfileString("LoginInfo","nSave","1",lpPath);
else
WritePrivateProfileString("LoginInfo","nSave","0",lpPath);
if(m_wndAuto.GetCheck())
WritePrivateProfileString("LoginInfo","nAuto","1",lpPath);
else
WritePrivateProfileString("LoginInfo","nAuto","0",lpPath);
CString vName;
CString vPass;
try
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
_variant_t strSql = "select * from user";
m_pRecordset->Open(strSql,(IDispatch *)m_pCon,adOpenStatic,adLockOptimistic,adCmdText);
while(!m_pRecordset->adoEOF)
{
vName = m_pRecordset->Fields->GetItem("name")->Value;
if(vName == m_strUser)
{
vPass = m_pRecordset->Fields->GetItem("pwd")->Value;
if(vPass == m_strPwd)
{
AfxMessageBox("登录成功");
}
}
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
}
catch(_com_error e)
{
CString strError;
strError.Format("%s",e.ErrorMessage());
AfxMessageBox(strError);
m_pRecordset->Close();
m_pCon->Close();
m_pRecordset = NULL;
m_pCon = NULL;
}
CDialog::OnOK();
}
void CLearn18Dlg::OnCancel()
{
CDialog::OnCancel();
}