0

ユーザーが Web ページでフレーム/iframe をプレビューし、その要素を強調表示し、一度強調表示すると、選択した要素の div または id を取得できるブラウザー コントロールを探しています。

それを行う方法はありますか?

4

3 に答える 3

1

クロス フレームの問題を解決するには、イベント ハンドラーをフレーム ドキュメントにアタッチする際に、コードを 1 行変更する必要があります。

    IHTMLDocument2 frameDocument = (IHTMLDocument2) CrossFrame.GetDocumentFromWindow  (frameWindow);

Interop.SHDocVw.dll をインクルードし、以下のクラスを追加します。

 public class CrossFrame
 {
    // Returns null in case of failure.
    public static IHTMLDocument2 GetDocumentFromWindow(IHTMLWindow2 htmlWindow)
    {
        if (htmlWindow == null)
        {
            return null;
        }

        // First try the usual way to get the document.
        try
        {
            IHTMLDocument2 doc = htmlWindow.document;
            return doc;
        }
        catch (COMException comEx)
        {
            // I think COMException won't be ever fired but just to be sure ...
            if (comEx.ErrorCode != E_ACCESSDENIED)
            {
                return null;
            }
        }
        catch (System.UnauthorizedAccessException)
        {
        }
        catch
        {
            // Any other error.
            return null;
        }

        // At this point the error was E_ACCESSDENIED because the frame contains a document from another domain.
        // IE tries to prevent a cross frame scripting security issue.
        try
        {
            // Convert IHTMLWindow2 to IWebBrowser2 using IServiceProvider.
            IServiceProvider sp = (IServiceProvider)htmlWindow;

            // Use IServiceProvider.QueryService to get IWebBrowser2 object.
            Object brws = null;
            sp.QueryService(ref IID_IWebBrowserApp, ref IID_IWebBrowser2, out brws);

            // Get the document from IWebBrowser2.
            SHDocVw.IWebBrowser2 browser = (SHDocVw.IWebBrowser2)(brws);

            return (IHTMLDocument2)browser.Document;
        }
        catch
        {
        }

        return null;
    }

    private const int E_ACCESSDENIED = unchecked((int)0x80070005L);
    private static Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
    private static Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E");
}

// This is the COM IServiceProvider interface, not System.IServiceProvider .Net interface!
[ComImport(), ComVisible(true), Guid("6D5140C1-7436-11CE-8034-00AA006009FA"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IServiceProvider
{
    [return: MarshalAs(UnmanagedType.I4)]
    [PreserveSig]
    int QueryService(ref Guid guidService, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppvObject);
}
于 2012-07-28T19:37:03.773 に答える
1

別の方法があります...

          using System.Runtime.InteropServices;

デリゲートを宣言する

          public delegate void DOMEvent(IHTMLEventObj e);

  private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

        if (this.webBrowser1.ReadyState != WebBrowserReadyState.Complete)

            return;
        else
        {

            HTMLDocument htmlDoc = (HTMLDocument)this.webBrowser1.Document.DomDocument;
            DispHTMLDocument doc = (DispHTMLDocument)htmlDoc;

            DOMEventHandler onmousedownhandler = new DOMEventHandler(doc);
            onmousedownhandler.Handler += new DOMEvent(Mouse_Down);
            doc.onmousedown = onmousedownhandler;

            if (htmlDoc.frames.length > 0)
            {
                FramesCollection FrameList = htmlDoc.frames;

                for (int i = 0; i < FrameList.length; i++)
                {
                    object id = (object)i;

                    IHTMLWindow2 frameWindow = (IHTMLWindow2)FrameList.item(ref id);
                    HTMLDocument frameDoc = (HTMLDocument)frameWindow.document;
                    DispHTMLDocument frameDispDoc = (DispHTMLDocument)frameDoc;
                    DOMEventHandler onmousedownhand = new DOMEventHandler(frameDispDoc);
                    onmousedownhand.Handler += new DOMEvent(Mouse_Down);
                    frameDispDoc.onmousedown = onmousedownhand;

                }
            }

        }
    }


        public class DOMEventHandler
        {
        public DOMEvent Handler;
        DispHTMLDocument Document;
        public DOMEventHandler(DispHTMLDocument doc)
        {

            this.Document = doc;

        }
        [DispId(0)]

        public void Call()
        {
            Handler(Document.parentWindow.@event);
        }
    }

       public void Mouse_Down(IHTMLEventObj e)
        {
        IHTMLElement element = (mshtml.IHTMLElement)e.srcElement;
        label1.Text = element.id;

         }
于 2012-07-29T11:24:24.967 に答える
1

答えが出ました…!このコードを試して、Web ページに存在する iframe とフレームを取得し、同じドキュメント ハンドラーを追加します。シンプルです。各フレーム/iframe にドキュメント ハンドラーを追加する必要があります。

    private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

        if (this.webBrowser1.ReadyState != WebBrowserReadyState.Complete)

            return;
        else
        {

            HtmlDocument _document = webBrowser1.Document;
            _document.MouseOver += new HtmlElementEventHandler(document_MouseOver);
            _document.MouseLeave += new HtmlElementEventHandler(document_MouseLeave);
            mshtml.HTMLDocumentEvents2_Event iEvent;
            IHTMLDocument2 currentDoc = (IHTMLDocument2)webBrowser1.Document.DomDocument;
            iEvent = (mshtml.HTMLDocumentEvents2_Event)currentDoc;
            iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(clickDocumentHandler);

            if (currentDoc.frames.length > 0)
            {
                FramesCollection FrameList = currentDoc.frames;

                for (int i = 0; i < FrameList.length; i++)
                {
                    object id = (object)i;

                    IHTMLWindow2 frameWindow = (IHTMLWindow2)FrameList.item(ref id);
                    IHTMLDocument2 frameDocument = (IHTMLDocument2)frameWindow.document;
                    iEvent = (mshtml.HTMLDocumentEvents2_Event)frameDocument;
                    iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(clickDocumentHandler);



                }

            }




        }
    }


    private bool clickDocumentHandler(IHTMLEventObj pEvtObj)
    {
        IHTMLElement element = (mshtml.IHTMLElement)pEvtObj.srcElement;
        label2.Text = element.id;
        pEvtObj.cancelBubble = true;
        pEvtObj.returnValue = false; 
        return false;
    }

    private bool clickElementHandler(IHTMLEventObj pEvtObj)
    {
        IHTMLElement element = (mshtml.IHTMLElement)pEvtObj.srcElement;
        label1.Text = element.id;
        pEvtObj.cancelBubble = true;
        pEvtObj.returnValue = false;
        return false;
    }

    private void document_MouseOver(object sender, HtmlElementEventArgs e)
    {
        HtmlElement element = e.FromElement;
        mshtml.HTMLElementEvents2_Event iEvent;
        iEvent = element.DomElement as mshtml.HTMLElementEvents2_Event;
        if (iEvent == null)
            return;
        iEvent.onclick += new mshtml.HTMLElementEvents2_onclickEventHandler(clickElementHandler);
    }

    private void document_MouseLeave(object sender, HtmlElementEventArgs e)
    {
        HtmlElement element = e.FromElement;
        mshtml.HTMLElementEvents2_Event iEvent;
        iEvent = element.DomElement as mshtml.HTMLElementEvents2_Event;
        if (iEvent == null)
            return;
        iEvent.onclick -= new mshtml.HTMLElementEvents2_onclickEventHandler(clickElementHandler);
    }
于 2012-07-28T12:42:40.297 に答える