开发者

login form creation in mfc

I wrote a code in mfc for login form

my code is here

// 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);

}

But I got this error

Error 3 error C2664: 'strcmp' : cannot convert parameter 1 from 'LPCTSTR' to 'const char *' e:\win32\test\login1\login1dlg.cpp 130 1 login1

i want little help from u


try Change the 2 if statement code to this

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

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


from the error i can see cannot convert parameter 1 from 'LPCTSTR' to 'const char *'
strcmp is used with char ANSI but try using the Unicode version of it instead
as you know LPCTSTR is dependent on unicode or ansi if you use unicode library use the following function wcscmp for more information help at msdn
also for types windows data types on msdn


so you replace strcmp at line 129 and 135 with wcscmp if this solve the problem just let us know

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜