我参照树上的一个例子用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。
请那位高手指点一下,谢谢!

解决方案 »

  1.   

    应该在包含那个控件的对话框的成员函数中调用GetDlgItem才对!
      

  2.   

    看看GetWindowText的返回值,调用GetLastError()看看是什么错误。
    我觉得问题出在GetDlgItem()上。
      

  3.   

    在建立工程的第四步,工具栏选择 IE  ReBars,然后对这个工具栏用上述方法。
      

  4.   

    TO: lwg7603(刑满释放人员) :"应该在包含那个控件的对话框的成员函数中调用GetDlgItem才对!"
    你的意思我不怎么明白。我按照树上的一个例子比划过来的,他是在CMainFrame里面写的。TO: vanter(vanter) (  ),不是这里,因为那个句柄就根本取不到。
      

  5.   

    TO :wzt2000(愚人码头)  我是这么做的,但用GetDlgItem的时候就是取不到pWnd ..一直为NULL,
      

  6.   

    同意 lwg7603(刑满释放人员)
      

  7.   

    to 搂主:
      GetDlgItem有两个参数哟,第一个是对话框的handle,你设置了吗??
      

  8.   

    显然地址栏的父窗口不是dialog,你怎么能调用GetDlgItem获取其句柄呢?
    GetDlgItem实现中首先会校验其父窗口是否属于对话框类,不是的话当然返回空咯。
      

  9.   

    下面是MSDN里面的一个例子的用法:
    ********************************************************************
    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);
      

  10.   

    GetDlgItem(IDC_EADDRESS)可以在CMainFrame里面用可以吗?????
    我怀疑书上有错误
      

  11.   

    我也觉得可能,在cdialog里面是没有问题,我今天看书的时候发现了这个问题,你也给了提示,我先找办法解决下,成功了就买单.