如何从 CEdit 控件获取文本 [英] How to get text from CEdit control
问题描述
我是 ATL 的新人.所以请原谅我提出这个问题.
I'm a new guy with ATL. So forgive me to ask this question.
问题描述:一个 CEdit
控件被添加到 ATL 对话框类中.它附加在对话框初始化函数中.
Problem description:
One CEdit
control is added into a ATL dialog class. It's attached in the dialog initialize function.
//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);
这里是关于如何从获取文本的主题CEdit
但它不起作用.
Here is a topic about how to get text from CEdit
but it is not working.
为什么CEdit
控件可以通过SetWindowText()
函数设置文本,但不能通过GetWindowText()
函数获取文本>?这真的让我很困惑.如果有人能帮我解释一下,非常感谢.
Why the CEdit
control could be set text with the function SetWindowText()
but can't get the text by the function GetWindowText()
? It's really confuse me. Thanks a lot if someone could explain it for me.
推荐答案
CEdit
不是 ATL 类.命名空间 ATLControls
来自哪里?有一个具有此名称的 WTL 类,从中获取文本很容易:
CEdit
is not an ATL class. Where the namespace ATLControls
comes from? There is a WTL class with this name and getting text from it is easy:
ATLASSERT(Edit.IsWindow()); // Make sure the control holds a handle
CString sWindowText;
Edit.GetWindowText(sWindowText);
GetWindowText
方法来自 ATL 并且封装了 GetWindowTextLength
和 GetWindowText
API.后一篇 MSDN 文章也有一段代码片段展示了典型用法.
The method GetWindowText
is coming from ATL however and wraps GetWindowTextLength
and GetWindowText
API. The latter MSDN article also has a code snippet showing typical usage.
既然您提到 IsWindow
对您不起作用,那么最可能的问题是您的编辑控件包装类变量没有实际控件的句柄,因此从无到有获取文本不可能.
Since you mention that IsWindow
does not work for you, the most likely problem is that your edit control wrapper class variable just does not have a handle of a real control, and hence getting text from nothing is impossible.
这篇关于如何从 CEdit 控件获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!