c++ - 如何从 CEdit 控件中获取文本

标签 c++ atl cedit

我是 ATL 的新人。所以请原谅我问这个问题。

问题描述: 将一个 CEdit 控件添加到 ATL 对话框类中。它附加在对话框初始化函数中。

//Define the edit control
ATLControls::CEdit  m_txtInput;

//In the OnInitDialog function
m_txtInput.Attach(GetDlgItem(IDC_INPUT_LINE));

m_txtInput.SetWindowText(_T("New directory"));

//In the public memeber function of the dialog GetInput()
//I have tried three kinds of method to get the text. But all of them are throw an 
//assert exception, IsWindow() failed. 
//1.
GetDlgItemText(IDC_INPUT_LINE, input);
//2.
ZeroMemory(m_lptstrInput, MAX_PATH);
m_txtInput.GetLine(0, m_lptstrInput, MAX_PATH);
//3.
BSTR input; 
m_txtInput.GetWindowText(input);

Here是关于如何从 CEdit 获取文本的主题,但它不起作用。

为什么CEdit控件可以用SetWindowText()函数设置文本,但是不能用GetWindowText()函数获取文本>?这真的让我很困惑。如果有人能为我解释一下,非常感谢。

最佳答案

CEdit 不是 ATL 类。命名空间 ATLControls 来自哪里?有一个具有此名称的 WTL 类,从中获取文本很容易:

    ATLASSERT(Edit.IsWindow()); // Make sure the control holds a handle
    CString sWindowText;
    Edit.GetWindowText(sWindowText);

方法 GetWindowText 然而来自 ATL 并包装了 GetWindowTextLengthGetWindowText应用程序接口(interface)。后一篇 MSDN 文章也有一个显示典型用法的代码片段。

由于您提到 IsWindow 对您不起作用,最可能的问题是您的编辑控件包装器类变量没有真正控件的句柄,因此从无到有获取文本是不可能的。

关于c++ - 如何从 CEdit 控件中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14575228/

相关文章:

multithreading - 在流程外环境中,公寓 “live”是位于服务器端还是位于客户端?

c++ - 文本字段如何响应 <Enter>

c++ - MFC CEdit Ctrl问题

c++ - 从 MPL 元函数类创建仿函数

c++ - 覆盖智能卡的 RSA 私钥操作?

c++ - Visual Studio 2010 中新 ATL 项目的资源 ID 差距

c++ - 在 Win32 中将图像放置在 CEdit 控件中

c++ - 读取字符串的长度,然后反转/反转字符串

c++ - SFINAE 没有按预期工作

c++ - 如何使用 C++ 使用 CAtlRegExp 验证电子邮件地址