IE中很可能只是一个jpg图片,或者Pdf文档,而没有HTML文档。
如何判断IE中是不是一个HTML页面呢?
MSDN里给了一个方法,哦试了一下不行,大家帮我看看错在哪里。
var
    IE:IWebbrowser2;
    iHTML:IHTMLDocument2;
    iUnKn:IUnknown;    iUnKn:=IE.Document;
    iHTML:=iUnkn as IHTMLDocument2;
    if (Assigned(iHTML)) then ShowMessage('is HTML')
    else ShowMessage('is not HTML');
    ShowMessage(iHTML.body.outerText);
    无论IE中是什么内容结果都是'is HTML'
    是HTML的时候一般Body.outerText不会是空,而其他情况则总是空字符。
    
    疑问还有一个,如果IE.Document不是IHTMLDocument2为什么as的时候没有EInvalidCast异常。
到底如何判断IE里面显示的是不是一个HTML页面呢?

解决方案 »

  1.   

    补充一下,MSDN上C++的代码是这样的
      CComQIPtr<IWebBrowser2, &IID_IWebBrowser2> m_spWebBrowser2;HRESULT CViewSource::GetDocumentContent()
    {
      USES_CONVERSION;
      
      // Get the WebBrowser's document object
      CComPtr<IDispatch> pDisp;
      HRESULT hr = m_spWebBrowser2->get_Document(&pDisp);
      if (FAILED(hr))
       return hr;  // Verify that what we get is a pointer to a IHTMLDocument2 
      // interface. To be sure, let's query for 
      // the IHTMLDocument2 interface (through smart pointers)
      CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> spHTML;
      spHTML = pDisp;  // Extract the source code of the document
      if (spHTML)
      {
        // Get the BODY object
        hr = spHTML->get_body(&m_pBody); 
        if (FAILED(hr))
            return hr;    // Get the HTML text
        BSTR bstrHTMLText;
        hr = m_pBody->get_outerHTML(&bstrHTMLText); 
        if (FAILED(hr))
         return hr;    // Convert the text from Unicode to ANSI
        LPTSTR psz = new TCHAR[SysStringLen(bstrHTMLText)];
        lstrcpy(psz, OLE2T(bstrHTMLText));
          
        // Enable changes to the text
        HWND hwnd = m_dlgCode.GetDlgItem(IDC_TEXT);
        EnableWindow(hwnd, true);
        hwnd = m_dlgCode.GetDlgItem(IDC_APPLY);
        EnableWindow(hwnd, true);    // Set the text in the Code Window
        m_dlgCode.SetDlgItemText(IDC_TEXT, psz); 
        delete [] psz;
      }
      else   // The document isn't a HTML page
      {
        m_dlgCode.SetDlgItemText(IDC_TEXT, ""); 
        HWND hwnd = m_dlgCode.GetDlgItem(IDC_TEXT);
        EnableWindow(hwnd, false);
        hwnd = m_dlgCode.GetDlgItem(IDC_APPLY);
        EnableWindow(hwnd, false);
      }  return S_OK;  
    }
    IDispatch和IUnknow哦都试过了,一样无效。
    MSDN里这篇文章在ms-help://MS.MSDNQTR.2003APR.1033/dnwebgen/html/bho.htm
      

  2.   

    试了一下
    if iUnKn.QueryInterface(IHTMLDocument2,iHTML)=S_OK then
          ShowMessage('Is HTML')
    else SHowMessage('Is not HTML');
    结果也一样,无论什么都是Is HTML
      

  3.   

    使用MimeType属性:procedure TForm1.Button1Click(Sender: TObject);
    VAR
        IE:IWebbrowser2;
        iHTML:IHTMLDocument2;
        iUnKn:IUnknown;
    begin
        IE := WebBrowser1.DefaultInterface;
        iUnKn:=IE.Document;
        iHTML:=iUnkn as IHTMLDocument2;
        showmessage(iHTML.mimeType);
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      WebBrowser1.Navigate('http://www.applevb.com');
    end;procedure TForm1.Button3Click(Sender: TObject);
    begin
      WebBrowser1.Navigate('http://www.applevb.com/image/main.gif');
    end;
      

  4.   

    点击Button3浏览图片,Button2浏览页面,然后点击Button1会有不同的提示。