我是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()获得文本?我真的很困惑。如果有人能帮我解释的话,非常感谢。
发布于 2013-01-29 07:24:35
CEdit不是ATL类。名称空间ATLControls从何而来?有一个具有此名称的WTL类,从中获取文本很容易:
ATLASSERT(Edit.IsWindow()); // Make sure the control holds a handle
CString sWindowText;
Edit.GetWindowText(sWindowText);然而,GetWindowText方法来自ATL,它封装了GetWindowTextLength和GetWindowText API。后一篇MSDN文章还提供了一个显示典型用法的代码片段。
由于您提到IsWindow不适合您,最可能的问题是您的编辑控件包装器类变量没有真正控件的句柄,因此不可能从任何地方获取文本。
发布于 2017-03-29 01:02:42
这已经在MFC &VS2015中进行了测试:
//
// Get char string/CString from CEdit m_ceDate;
// where
// DDX_Control(pDX, IDC_EDIT_DATE, m_ceDate);
char cdateBuf[128];
UINT nCountOfCharacters = GetDlgItemText(IDC_EDIT_DATE, cdateBuf, 16);
CString csDate = cdateBuf; https://stackoverflow.com/questions/14575228
复制相似问题