C#的软件如何与网页进行"交流"呢?
我想用C#编写一个windows窗体应用程序,功能是在没有打开网页的前提下,激活网页上的一些功能事件。例如一些按钮啊,一些连接啊等等。给个实例具体点说吧:如果我想在没有代开网页:http://zhidao.baidu.com/question/228721523.html
的前提下,用软件自动点击“最佳答案”下面的
[ 本帖最后由 死亡军团 于 2011-4-13 22:42 编辑 ]
using System; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using WriteAndReadWebpage; namespace FirstWebBrower { public partial class FrmWenBrowser : Form { // 应用程序根目录 private readonly string executePath; // HTML文件名称 private readonly string htmlFileName; // 读取网页实体 private WebPageControl wpc; public FrmWenBrowser() { InitializeComponent(); // 初始化 executePath = System.AppDomain.CurrentDomain.BaseDirectory; htmlFileName = executePath + "WebPageDemo.html"; //htmlFileName = "C:\\Program Files\\Foxmail\\SMIMEHELP\\ENCHELP.html"; wpc = new WebPageControl(this.webBrowser); this.webBrowser.AllowWebBrowserDrop = false; this.webBrowser.IsWebBrowserContextMenuEnabled = false; this.webBrowser.WebBrowserShortcutsEnabled = false; this.cmbNameOrID.SelectedIndex = 0; // Event register this.Load += new EventHandler(FrmWenBrowser_Load); this.btnGetText.Click += new EventHandler(btnGetText_Click); this.btnWrite.Click += new EventHandler(btnWrite_Click); this.btnCommit.Click += new EventHandler(btnCommit_Click); // Web Navigate this.btnFoward.Click += new EventHandler(btnFoward_Click); this.btnBackward.Click += new EventHandler(btnBackward_Click); this.btnStop.Click += new EventHandler(btnStop_Click); this.btnRefresh.Click += new EventHandler(btnRefresh_Click); } // Handling // Window Create void FrmWenBrowser_Load(object sender, EventArgs e) { // 加载html文件 & 打开网址 this.webBrowser.Navigate(htmlFileName); } // 单击按钮Get响应 void btnGetText_Click(object sender, EventArgs e) { try { if (this.txtAttributeName.Text != string.Empty) { this.txtText.Text = this.cmbNameOrID.Text == "By Name" ? this.wpc.ReadText(this.txtAttributeName.Text) : this.wpc.ReadTextByID(this.txtAttributeName.Text); } } catch { MessageBox.Show("读取控件文本失败,请检查网页中是否包含" + this.txtAttributeName.Text + "控件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //单击按钮Write响应 void btnWrite_Click(object sender, EventArgs e) { try { if (this.txtAttributeName.Text != string.Empty) { this.wpc.WriteText(this.txtAttributeName.Text, this.txtText.Text); } } catch { MessageBox.Show("读取控件文本失败,请检查网页中是否包含" + this.txtAttributeName.Text + "控件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // 单击按钮Commit响应 void btnCommit_Click(object sender, EventArgs e) { try { //if (this.txtAttributeName.Text != string.Empty) //{ // this.wpc.WriteText(this.txtAttributeName.Text, this.txtText.Text); //} if (this.cmbNameOrID.Text == "By Name") { this.wpc.ClickButton("btnCommit"); } else { //this.wpc.ClickButtonByID("btnContinue"); } } catch { MessageBox.Show("读取控件文本失败,请检查网页中是否包含" + this.txtAttributeName.Text + "控件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // 网站导航:向前 void btnFoward_Click(object sender, EventArgs e) { this.webBrowser.GoForward(); } // 网站导航:向后 void btnBackward_Click(object sender, EventArgs e) { this.webBrowser.GoBack(); } // 网站导航:停止 void btnStop_Click(object sender, EventArgs e) { this.webBrowser.Stop(); } // 网站导航:刷新 void btnRefresh_Click(object sender, EventArgs e) { this.webBrowser.Refresh(); } private void btnImg_Click(object sender, EventArgs e) { FrmImage img = new FrmImage(); HtmlElement imgEle = this.webBrowser.Document.GetElementById("imgIBM"); HtmlElement imgNew = img.wb.Document.CreateElement("IMG"); imgNew.SetAttribute("src", imgEle.GetAttribute("src")); //img.wb.Document.Body.AppendChild(imgNew); img.ShowDialog(); } } }