我参照树上的一个例子用chtmlview来做一个简单的例子,在地址栏里输入url,按回车后响应这个时间,代码如下:
void CMainFrame::OnNewAddress()
{
CString sAddress;
CWnd * pWnd =GetDlgItem(IDC_EADDRESS);
pWnd->GetWindowText(sAddress);
GetDlgItem(IDC_EADDRESS)->GetWindowText(sAddress);
((CTestIEView*)GetActiveView())->Navigate(sAddress);
}
但是我为什么在sAddress里面老取不到数据,我调试的时候发现是句柄pWnd一直是NULL,查了下msdn的帮助,GetDlgItem就是这个用法,但我就是取不到pWnd的句柄。为什么呢?其中IDC_EADDRESS是一个对话控上的edit控件,地址栏实际上也是一个cDialog。
请那位高手指点一下,谢谢!
void CMainFrame::OnNewAddress()
{
CString sAddress;
CWnd * pWnd =GetDlgItem(IDC_EADDRESS);
pWnd->GetWindowText(sAddress);
GetDlgItem(IDC_EADDRESS)->GetWindowText(sAddress);
((CTestIEView*)GetActiveView())->Navigate(sAddress);
}
但是我为什么在sAddress里面老取不到数据,我调试的时候发现是句柄pWnd一直是NULL,查了下msdn的帮助,GetDlgItem就是这个用法,但我就是取不到pWnd的句柄。为什么呢?其中IDC_EADDRESS是一个对话控上的edit控件,地址栏实际上也是一个cDialog。
请那位高手指点一下,谢谢!
我觉得问题出在GetDlgItem()上。
你的意思我不怎么明白。我按照树上的一个例子比划过来的,他是在CMainFrame里面写的。TO: vanter(vanter) ( ),不是这里,因为那个句柄就根本取不到。
GetDlgItem有两个参数哟,第一个是对话框的handle,你设置了吗??
GetDlgItem实现中首先会校验其父窗口是否属于对话框类,不是的话当然返回空咯。
********************************************************************
Example// set the text in IDC_MYEDIT
CWnd* pWnd = GetDlgItem(IDC_MYEDIT);
pWnd->SetWindowText(_T("Hockey is best!"));// Get the text back. CString is convenient, because MFC
// will automatically allocate enough memory to hold the
// text--no matter how large it is.CString str;
pWnd->GetWindowText(str);
ASSERT(str == _T("Hockey is best!"));// The LPTSTR override works, too, but it might be too short.
// If we supply a buffer that's too small, we'll only get those
// characters that fit.TCHAR sz[10];
int nRet = pWnd->GetWindowText(sz, 10);// Nine characters, plus terminating null
ASSERT(lstrcmp(sz, _T("Hockey is")) == 0);
ASSERT(nRet == 9);// You can query the length of the text without the length of
// the string using CWnd::GetWindowTextLength()
nRet = pWnd->GetWindowTextLength();
ASSERT(nRet == 15);
它不是取了吗? CWnd* pWnd = GetDlgItem(IDC_MYEDIT);
我怀疑书上有错误