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

标签 c++ visual-studio-2010 user-interface mfc

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

我想读取编辑控件中存在的值并将其存储在字符串中,但我不知道该怎么做。

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

我在 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 "."

最佳答案

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

注意大写的'S'

或者你可以投:

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

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

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

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

关于c++ - 从 MFC 和 VS2010 中的编辑控件读取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11065919/

相关文章:

c# - 对 Windows 服务和 GUI 使用相同的 app.config

c++ - 对 sf::的 undefined reference

mysql - TableAdapter 中查询的 IIF 语句错误

c# - add_library 无法在 CMake 中添加 CSharp 源文件?

java - GUI Jlist 项目监听器

windows - GUI 元素随 MS Outlook 11.0 对象库 + Outlook 2016 Ver 1702 移动

C++ 模板与聚合

c++ - 使用 C++ 在 Linux 中监视磁盘挂载的最佳方法?

c++ - 获取UTF-8编码的std::string的实际长度?

visual-studio-2010 - 使用 Visual Studio 2010 为 Windows Phone 开发?