如何从CEdit控件获取文本

Erx*_*xin 6 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);
Run Code Online (Sandbox Code Playgroud)

是一个关于如何从CEdit获取文本但不起作用的主题.

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

Rom*_* R. 7

CEdit不是ATL课程.名称空间ATLControls来自何处?有一个带有此名称的WTL类,从中获取文本很容易:

    ATLASSERT(Edit.IsWindow()); // Make sure the control holds a handle
    CString sWindowText;
    Edit.GetWindowText(sWindowText);
Run Code Online (Sandbox Code Playgroud)

该方法GetWindowText来自ATL然而包装GetWindowTextLengthGetWindowTextAPI.后一篇MSDN文章还有一个显示典型用法的代码片段.

既然你提到它IsWindow不适合你,最可能的问题是你的编辑控件包装类变量只是没有真正控件的句柄,因此从无到有获取文本是不可能的.