C++ 从 MFC 和 VS2010 中的编辑控件中读取文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11065919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:47:44  来源:igfitidea点击:

Read text from edit control in MFC and VS2010

c++visual-studio-2010user-interfacemfc

提问by Marcus Barnet

I'm writing a simple MFC application with a Dialog window and some buttons. I added also a edit control in order to let user insert a text string.

我正在编写一个带有对话框窗口和一些按钮的简单 MFC 应用程序。我还添加了一个编辑控件,以便让用户插入文本字符串。

I'd like to read the value which is present in the edit control and to store it in a string but i do not know how to do this.

我想读取编辑控件中存在的值并将其存储在字符串中,但我不知道如何执行此操作。

I have no compilation error, but I always read only a "." mark.

我没有编译错误,但我总是只读取一个“。” 标记。

I added a variable name to the text edit control which is filepath1and this is the code:

我在文本编辑控件中添加了一个变量名称filepath1,这是代码:

    // CMFC_1Dlg dialog
    class CMFC_1Dlg : public CDialogEx
    {
    // Construction
    public:
        CMFC_1Dlg(CWnd* pParent = NULL);    // standard constructor

    // Dialog Data
        enum { IDD = IDD_MFC_1_DIALOG };

        protected:
        virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support


    // Implementation
    protected:
        HICON m_hIcon;

        // Generated message map functions
        virtual BOOL OnInitDialog();
        afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
        afx_msg void OnPaint();
        afx_msg HCURSOR OnQueryDragIcon();
        DECLARE_MESSAGE_MAP()
    public:
        afx_msg void OnBnClickedButton1();
        afx_msg void OnBnClickedButton2();
        afx_msg void OnEnChangeEdit1();
        CString filePath1;
    }

    //...
void CMFC_1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    else
    {
        CDialogEx::OnSysCommand(nID, lParam);
    }
}

    CMFC_1Dlg::CMFC_1Dlg(CWnd* pParent /*=NULL*/)
        : CDialogEx(CMFC_1Dlg::IDD, pParent)
        ,filePath1(("..\Experiments\Dirs\"))
    {
        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }

    void CMFC_1Dlg::DoDataExchange(CDataExchange* pDX)
    {
        CDialogEx::DoDataExchange(pDX);
        DDX_Text(pDX, IDC_EDIT1, filePath1);

    }

    // then i try to get the string value with
    CString txtname=filePath1;
    _cprintf("Value %s\n", txtname); // but i always read just a "."

回答by Chris Dargis

_cprintf("Value %S\n", txtname.GetString());

Note the capital 'S'

注意大写的“S”

or you can cast:

或者你可以投射:

_cprintf("Value %S\n", (LPCTSTR)txtname);

You would be better off using an edit control. To create a CEdit variable, right click on the edit box in VS and select "Add Member Variable", give the variable a name and click OK.

最好使用编辑控件。要创建 CEdit 变量,请右键单击 VS 中的编辑框并选择“添加成员变量”,为变量命名并单击“确定”。

You can then retrieve the text in the edit box like this:

然后,您可以像这样检索编辑框中的文本:

CEdit m_EditCtrl;
// ....
CString filePath1 = m_EditCtrl.GetWindowText()

回答by Miles

I think your original code was OK for DDX use and CString. The advice to use a control variable and avoid the DDX/DDV functions is really one of preference and not the issue.

我认为您的原始代码可以用于 DDX 和 CString。使用控制变量和避免 DDX/DDV 函数的建议实际上是一种偏好,而不是问题。

I suspect you are compiling with the UNICODE libraries but explicitly calling an ASCII function _cprintf. UNICODE is held as two bytes, for ASCII characters one of these will be 0. If you pass this to an ASCII string function it will stop after the first character.

我怀疑您正在使用 UNICODE 库进行编译,但显式调用 ASCII 函数_cprintf。UNICODE 保存为两个字节,对于 ASCII 字符,其中一个将为 0。如果您将其传递给 ASCII 字符串函数,它将在第一个字符后停止。

If you are using UNICODE then call _cwprintfor use the tchar.hmacro _tcprintfwhich will call the correct version for the compiler switch.

如果您正在使用 UNICODE,则调用_cwprintf或使用tchar.h_tcprintf,它将为编译器开关调用正确的版本。

Tip: If you are targeting UNICODE only and will never require MBCS support then avoid using the tchar.hmacros as they will obscure any issues with charand TCHARdata type mixing.

提示:如果你只指定UNICODE,绝不会要求MBCS支持则避免使用tchar.h宏,因为它们将混淆的任何问题charTCHAR数据类型的混合。

回答by Swapon

Step 1: Create a CEdit control variable using "Add Variable List". Step 2: Use GetDlgItemText() to hold the text of that edict control.

步骤 1:使用“添加变量列表”创建 CEdit 控制变量。第 2 步:使用 GetDlgItemText() 保存该法令控件的文本。

Example: such as CEdit control list variable is mc_strChatPane, then GetDlgItemText(mc_strChatPane, message) where message is a user defined CString variable.

示例:如CEdit 控件列表变量是mc_strChatPane,那么GetDlgItemText(mc_strChatPane, message) 其中message 是用户定义的CString 变量。