我是 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 并包装了 GetWindowTextLength
和 GetWindowText
应用程序接口(interface)。后一篇 MSDN 文章也有一个显示典型用法的代码片段。
由于您提到 IsWindow
对您不起作用,最可能的问题是您的编辑控件包装器类变量没有真正控件的句柄,因此从无到有获取文本是不可能的。
关于c++ - 如何从 CEdit 控件中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14575228/