首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IHTMLDocument2 -在iframe内单击按钮

IHTMLDocument2 -在iframe内单击按钮
EN

Stack Overflow用户
提问于 2013-01-21 12:53:12
回答 2查看 2.1K关注 0票数 0

我正在为一些非常老的应用程序创建一个DCOM扩展。主应用程序内部有IE控件。

使用C#,我能够获得IE控件的处理程序,如下所示:

代码语言:javascript
复制
Process pr = Process.GetCurrentProcess();

var title = pr.MainWindowTitle;
if(title.IndexOf("My old application -")<0)
{
    MessageBox.Show("No such window");
    return;
}
var wlwWindow = pr.MainWindowHandle;
var ieWindow = PI.FindWindowRecursive(wlwWindow, "Internet Explorer_Server", null);
if (ieWindow == IntPtr.Zero)
{
    MessageBox.Show("Unable to locate IE window.");
    return;
}
var myDocument= PI.GetIEDocumentFromWindowHandle(ieWindow);
MessageBox.Show(myDocument.url);

下面是我的助手方法:

代码语言:javascript
复制
    public static IntPtr FindWindowRecursive(IntPtr parent, string windowClass, string windowCaption)
    {
        var found = FindWindowEx(parent, IntPtr.Zero, windowClass, windowCaption);
        if (found != IntPtr.Zero)
        {
            return found;
        }

        var child = FindWindowEx(parent, IntPtr.Zero, null, null);
        while (child != IntPtr.Zero)
        {
            found = FindWindowRecursive(child, windowClass, windowCaption);
            if (found != IntPtr.Zero)
            {
                return found;
            }
            child = GetNextWindow(child, 2);
        }
        return IntPtr.Zero;
    }

    public static IHTMLDocument2 GetIEDocumentFromWindowHandle(IntPtr hWnd)
    {
        IHTMLDocument2 htmlDocument = null;
        if (hWnd != IntPtr.Zero)
        {
            uint lMsg = RegisterWindowMessage("WM_HTML_GETOBJECT");
            UIntPtr lResult;
            SendMessageTimeout(hWnd, lMsg, UIntPtr.Zero, UIntPtr.Zero,
                SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 1000, out lResult);
            if (lResult != UIntPtr.Zero)
            {
                htmlDocument = ObjectFromLresult(lResult, typeof(IHTMLDocument).GUID, IntPtr.Zero) as IHTMLDocument2;
                if (htmlDocument == null)
                {
                    throw new COMException("Unable to cast to an object of type IHTMLDocument");
                }
            }
        }
        return htmlDocument;
    }

这个部分工作得很好,我可以从temp获得html页面的内容:

代码语言:javascript
复制
<body id="state" class="saveFavorite" onload="load()" onunload="RUIMaster.Destroy()" style="margin: 0px; padding: 0px; overflow: hidden;">
<p style="display: none;">
<object classid="clsid:22FD36F1-A133-11d4-A0E4-005056E2D8AA" height="0" width="0" id="RUIMaster">
</object>
</object>
<form>
<input id="id" type="hidden" value="" />
</form>
</p>
<iframe name="AAMain" style="margin: 0px; border: 0px; padding: 0px;" id="contents" src="rdaui_frame.htm" width="100%" height="100%" frameborder="0" />
</body>

我必须用名字AAMain访问iframe。

该iframe文档的内容如下:

代码语言:javascript
复制
<frameset cols="135,*" border="0" frameborder="no" framespacing="0">
    <frame name="Menu" scrolling="no" noresize marginwidth="0" marginheight="0" src="blank.htm">
    <frameset rows="310,*,31">
        <frame name="Top" noresize marginwidth="0" marginheight="0" src="blank.htm">
        <frame name="Centre" noresize marginwidth="0" marginheight="0" src="blank.htm">
        <frame name="Bottom" noresize marginwidth="0" marginheight="0" src="blank.htm">
    </frameset>
</frameset>

因此,基本上我必须访问frame name Centre I iframe,名为AAMain

myDocument -> iframe(AAMain) -> frame(中间) ->,然后单击图像

我试图迭代myDocument的帧,但是我得到了无效的强制转换异常。

代码语言:javascript
复制
try
{
    FramesCollection frames = myDocument.frames;
    object index = 0;
    IHTMLWindow2 frame = (IHTMLWindow2)frames.item(ref index);
    var xx = (HTMLDocument)frame.document;
    MessageBox.Show(xx.body.innerHTML);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

即使是这样的代码:

代码语言:javascript
复制
MessageBox.Show(myDocument.frames.length.ToString());

给了我同样的特例。

EN

回答 2

Stack Overflow用户

发布于 2013-05-25 10:20:06

这应该可以通过iframe元素的contentWindow或contentDocument属性来实现,这取决于根据contentwindow.asp您拥有的IE版本--然而,我一直无法让它工作,contentWindow似乎返回了一些元素,但是它有空的document属性(在我的例子中是IE9)。

既然这个问题已经发布了一段时间了,我想知道你是否在这方面取得了一些进展?

票数 0
EN

Stack Overflow用户

发布于 2014-12-24 14:27:05

尝试使用单线程架构(STA Thread)访问框架集合。在确认帧内容的功能单元测试中,这对我起了作用。在切换线程模型之前,我遇到了相同的强制转换错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14439081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档