MFC:Edit Control 编辑框

最近更新于 2025-03-04 22:48

测试环境

通过控件变量读写值

新建一个基于对话框的 MFC 应用 editControlProject1
file

删除模板的控件,添加 3 个 static text
file

添加 3 个 edit control
file

密码后面的编辑框属性里密码改为 True
file

结果后面的编辑框属性里垂直滚动多行只读改为 True
file

添加 1 个 Button
file

control 变量方式

为 3 个编辑框右键添加变量,类别都是控件,访问选 private
file
file
file

双击登录按钮会为其创建单击事件的回调方法,写入

    CString accountString, passwordString, outputString;
    this->accountEditControl.GetWindowTextW(accountString); // 读取账号
    this->passwordEditControl.GetWindowTextW(passwordString); // 读取密码

    if (accountString == L"admin" && passwordString == L"123456")
    {
        outputString = L"登录成功!\r\n";
    }
    else
    {
        outputString = L"登录失败!\r\n";
    }

    // 在末尾追加
    int length = outputEditControl.GetWindowTextLengthW();
    outputEditControl.SetSel(length, length);
    outputEditControl.ReplaceSel(outputString);

file
file

value 变量方式

为 3 个编辑框添加变量,类别选
file
file
file

为登录按钮的单机事件回调写入

    UpdateData(TRUE); // 从编辑框刷新到 value 变量中
    if (this->accountEditValue == L"admin" && this->passwordEditValue == L"123456")
    {
        this->outputEditValue += L"登录成功\r\n";
    }
    else
    {
        this->outputEditValue += L"登录失败\r\n";
    }
    UpdateData(FALSE); // 从 value 变量刷新到编辑框中

file

MFC:Edit Control 编辑框
Scroll to top