在mfc中创建登录表单

时间:2011-02-27 13:27:44

标签: mfc

我在mfc中为登录表单

编写了一个代码

我的代码在这里

// login1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "login1.h"
#include "login1Dlg.h"
#include "afxdialogex.h"
//#include "LOGINDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// Clogin1Dlg dialog




Clogin1Dlg::Clogin1Dlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(Clogin1Dlg::IDD, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    m_username = _T("");
    m_password = _T("");
}

void Clogin1Dlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_USERNAME_EDIT, m_username);
    DDX_Text(pDX, IDC_PASSWORD_EDIT, m_password);
}

BEGIN_MESSAGE_MAP(Clogin1Dlg, CDialogEx)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_OK_BUTTON, &Clogin1Dlg::OnBnClickedOkButton)
END_MESSAGE_MAP()


// Clogin1Dlg message handlers

BOOL Clogin1Dlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    // 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
}

// 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 Clogin1Dlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
    {
        CDialogEx::OnPaint();
    }
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR Clogin1Dlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}



void Clogin1Dlg::OnBnClickedOkButton()
{
    // TODO: Add your control notification handler code here
    UpdateData();

    char UsernameFromFile[20], PasswordFromFile[20];


    FILE *fleCredentials;
    bool ValidLogin = false;

    if(m_username == "" )
    {
        AfxMessageBox(_T("You must provide a username and a password or click Cancel"));
        return;
    }
    if( m_password == "" )
    {
        AfxMessageBox(_T("Invalid Login"));
        return;
    }

    try {
        // Open the file for reading
        fleCredentials = fopen("credentials.txt", "r");

        // Scan the file from beginning to end
        while( !feof(fleCredentials) )
        {
             //Read a username
            fscanf(fleCredentials, "%s", UsernameFromFile);

             //Compare the typed username with the username from the file
            if(strcmp((LPCTSTR)m_username, UsernameFromFile) == 0 )
            {
                // With the current username, retrieve the corresponding password
                fscanf(fleCredentials, "%s", PasswordFromFile);

                 //Compare the typed password with the one on file
                if( strcmp((LPCTSTR)m_password, PasswordFromFile) == 0 )
                {
                    ValidLogin = true;
                }
                else
                    ValidLogin = false;
            }
        }
        if( ValidLogin == true )
            OnOK();
        else
        {
            AfxMessageBox(_T("Invalid Credentials. Please try again"));
            //this->m_EditUsername.SetFocus();
        }

        fclose(fleCredentials);
    }
    catch(...)
    {
        AfxMessageBox(_T("Could not validate the credentials"));
    }

    UpdateData(FALSE);

}

但是我收到了这个错误

错误3错误C2664:'strcmp':无法将参数1从'LPCTSTR'转换为'const char *'e:\ win32 \ test \ login1 \ login1dlg.cpp 130 1 login1

我想要你的帮助

2 个答案:

答案 0 :(得分:1)

尝试将2 if语句代码更改为此

if(strcmp((LPSTR)(LPCTSTR)m_username,UsernameFromFile)== 0)

if(strcmp((LPSTR)(LPCTSTR)m_password,PasswordFromFile)== 0)

答案 1 :(得分:0)

从错误中我可以看到无法将参数1从'LPCTSTR'转换为'const char *'
strcmp与char ANSI一起使用,但尝试使用它的Unicode版本而不是 如您所知LPCTSTR依赖于unicode或ansi如果您使用unicode库,请使用以下函数 wcscmp 获取更多信息help at msdn
也适用于windows data types on msdn

类型

所以你用 wcscmp 替换第129行和第135行的 strcmp ,如果这样可以解决问题,请告诉我们

相关问题