做了一个控件,嵌入在IE中,想获取同一窗口中特定编辑框的内容
下面的代码是获取所有打开窗口的中编辑框的内容,怎样获取指定的内容?
void DyDbaseDlg::GetIHTMLCode()
{
VARIANT id, index;
CComPtr<IDispatch> spDispatch;
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
CComPtr<IHTMLElement> pElement;
CComPtr<IHTMLElementCollection> pElementCol;
CComPtr<IHTMLFormElement> pFormElement;
CComPtr<IHTMLInputTextElement> pInputElement;
// TODO: Add your control notification handler code here
if (m_spSHWinds){
int n = m_spSHWinds->GetCount();//获取当前打开的IE窗口数
for (int i = 0; i < n; i++){
_variant_t v = (long)i;
IDispatchPtr spDisp = m_spSHWinds->Item(v);
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);   //生成一个IE窗口的智能指针
if (spBrowser){
if (SUCCEEDED(spBrowser->get_Document( &spDispatch)))
pDoc2 = spDispatch;
if(pDoc2!=NULL)
{
{
if (SUCCEEDED(pDoc2->get_forms(&pElementCol)))
{
// AfxMessageBox("IHTMLElementCollection");
long p=0;
if(SUCCEEDED(pElementCol->get_length(&p)))
if(p!=0)
{   
// AfxMessageBox("1");
for(long i=0;i<=(p-1);i++)
{

V_VT(&id) = VT_I4;
V_I4(&id) = i;
V_VT(&index) = VT_I4;
V_I4(&index) = 0;

if(SUCCEEDED(pElementCol->item(id,index, &spDispatch)))
// AfxMessageBox("2");
if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLFormElement,(void**)&pFormElement)))
{
// AfxMessageBox("IHTMLFormElement");
long q=0;
if(SUCCEEDED(pFormElement->get_length(&q)))
for(long j=0;j<=(q-1);j++)
{
V_VT(&id) = VT_I4;
V_I4(&id) = j;
V_VT(&index) = VT_I4;
V_I4(&index) = 0;
if(SUCCEEDED(pFormElement->item(id,index, &spDispatch)))

if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputElement)))
{
//AfxMessageBox("IHTMLInputTextElement");
CComBSTR value;
CComBSTR type;
CComBSTR name;

pInputElement->get_name(&name);
pInputElement->get_type(&type);

CString strname(name);
CString strtype(type);
strtype.MakeUpper();
if(strtype.Find("TEXT")!=-1)
{
if(strname.Find("dm")!=-1)//此处可修改对应与IE中编辑框的id
{
pInputElement->get_value(&value);
CString str(value);
if(!str.IsEmpty())
{
SetDlgItemText(IDC_CODECURRENT,_bstr_t(value));
if(str.GetLength()==6)
{                               
strncpy(Code,str,6);
ShowData();
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}

解决方案 »

  1.   

    由于可能打开多个同样窗口,所以要绑定 控件和同一窗口内的编辑框,使的它只能取得这个编辑框的内容,不能跳到别的IE窗口中。
    另外,关闭窗口时会出现内存不能read的错误,是不是没有释放IE窗口指针呢?
      

  2.   

    用MFC创建的浏览器窗口可以用CWnd::GetControlUnknown获得IWebBrowser2接口。
      

  3.   

    并不是MFc创建的浏览器的窗口。是运行IE窗口时调用控件,控件再去取同一页面上某个编辑框中的内容。
      

  4.   

    如果要从网页上的控件访问网页的DOM的话
    http://www.csdn.net/develop/read_article.asp?id=10175
      

  5.   

    这样可以获取 控件所在网页的指针SHDocVw::IShellWindowsPtr m_spSHWinds;
    SHDocVw::IWebBrowser2Ptr m_crWBrows;void DyDbaseDlg::GetIHTMLCode()//获取当前打开窗口指针
    {
        // TODO: Add your control notification handler code here
    if (m_spSHWinds)
    {
    int n = m_spSHWinds->GetCount();//获取当前打开的IE窗口数
    _variant_t v = (long)(n-1);//第n-1个为当前打开窗口
    IDispatchPtr spDisp = m_spSHWinds->Item(v);
    SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);   //生成一个IE窗口的智能指针
    m_crWBrows=spBrowser;
    GetHTMLCode();//在此函数内部处理m_crWbrows
    }
    }
      

  6.   

    获取窗口指针的问题解决了,可是IE窗口关闭后,控件比它后释放,会继续读取IE窗口中的内容,导致内存读错误。如何判断IE窗口已经关闭了呢?
      

  7.   

    去看
    Web Development (General) Technical Articles   
    Browser Helper Objects: The Browser the Way You Want It
    这篇文章
      

  8.   

    去看MSDN里
    Web Development (General) Technical Articles   
    Browser Helper Objects: The Browser the Way You Want It
    这篇文章的示例代码
      

  9.   

    去看MSDN里
    Web Development (General) Technical Articles   
    Browser Helper Objects: The Browser the Way You Want It
    这篇文章的示例代码