明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

//先在工程中添加一个mshtml的引用
//替换内核函数

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mshtml;

namespace WindowsApplication16
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate(@"c:\temp\temp.htm");
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
            vDocument.parentWindow.execScript(
                "function alert(str){if(str=='zswang')confirm(str);}", "javaScript");
        }
    }
}

====================================
如何自动调用WebBrowser控件里DOC的类似BUTTON或者<a>标签的点击事件?


this.webBrowser1.Document.Click += new HtmlElementEventHandler(Document_Click);

 void Document_Click(object sender, HtmlElementEventArgs e)
        {
           
            if (ReadOnly)
            {
                if (webBrowser1.Document.ActiveElement.DomElement is HtmlAnchorElement)
                {
                    HtmlAnchorElement em = (HtmlAnchorElement)webBrowser1.Document.ActiveElement.DomElement;
                    string href = em.href;
                    .......                   
                }
            }
        }



或者:


HtmlDocument hDoc = WebBrowser.Document;
HtmlElement he对象 = hDoc.GetElementById("对象ID");
he对象.InvokeMember("click");

当然也可以用hDoc.GetElementsByTagName("TagName")去取对象

========================================
//C#2005 调试成功

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
    IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
    foreach (IHTMLElement vElement in vDocument.all)
    {
        if (vElement.tagName.ToUpper() == "FRAME")
        {
            IHTMLFrameBase2 vFrameBase2 = vElement as IHTMLFrameBase2;
            vFrameBase2.contentWindow.execScript(
                "function alert(str){confirm('[' + str + ']');}", "javaScript");
        }
    }
}



=========================================
做浏览器,如何获取打开页面中特定的link,并点击它.

其实比较简单
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)this.axWeb_Browser.Document;
mshtml.IHTMLElementCollection myLiks = doc.links;
string tmp_ads_url = "xxxxxxx";
foreach (HTMLAnchorElementClass el in myLiks)
{
    if (el.href.ToString().ToLower() == tmp_ads_url)
    {
        el.click();
        break;
    }
}



好了.点当前link不以新窗口打开的问题也解决了.方法也很简单.只需要在我实现查找指定link的代码中加个
el.target = "_self";
加到什么位置就不用我说了吧.呵呵.