class="brush:csharp;collapse:true;;gutter:false;">using System; using System.Windows.Forms; using zoyobar.shared.panzer.web.ib; using zoyobar.shared.panzer.web; using zoyobar.shared.panzer; using System.Drawing;//包含jquery1.6 using System.Data.SQLite; using System.IO; using System.Data; //可操作javascript,webbrowser using System.Runtime.InteropServices; //netframework 4.0内置处理JSON对象 //using System.Runtime.Serialization; //从服务器取得json数据 using System.Net; using System.Web; using System.Web.Script.Serialization; //取得javascript变量值 namespace tsgwy { [ComVisible(true)] public partial class FrmMain : Form { private readonly string homeurl = "http://passport.chinahrt.com/login.do"; private DBDataSet ds; private DBDataSetTableAdapters.tbAdminTableAdapter tba; private bool loading = true;//等待加载完成 private readonly IEBrowser ie; //可打开的最多网页数 const int num = 10; private WebBrowser[] web = new WebBrowser[num]; public FrmMain() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { dbload(); for (int i = 0; i < num; i++) { web[i] = new WebBrowser(); } web[0].Dock = DockStyle.Fill; web[0].Navigate(new Uri(homeurl)); tabControl1.TabPages[0].Controls.Add(web[0]); web[0].DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted); } //主窗体重绘,地址栏自动变换 private void Form1_SizeChanged(object sender, EventArgs e) { combUrl.Size = new Size(this.panel4.Width - 95, 23); } //登录 private void btnLogin_Click(object sender, EventArgs e) { //查找当前激活顶 foreach (TabPage tp in tabControl1.TabPages) { //如果当前顶为,选定的页 if (tabControl1.SelectedTab == tp) { //判断url是否为主页,不是导航到主页。 if (web[tabControl1.SelectedIndex].Url.ToString() != homeurl) { web[tabControl1.SelectedIndex].Navigate(homeurl); if (web[tabControl1.SelectedIndex].ReadyState != WebBrowserReadyState.Complete) { while (loading) { Application.DoEvents(); } } } //改写tab标签 tabControl1.TabPages[tabControl1.SelectedIndex].Text = web[tabControl1.SelectedIndex].DocumentTitle; } //用户名、密码有一项为空,则设置值 if (string.IsNullOrEmpty(this.txtName.Text) || string.IsNullOrEmpty(this.txtPwd.Text)) { this.txtName.Text = listView1.Items[0].SubItems[0].Text.ToString(); this.txtPwd.Text = listView1.Items[0].SubItems[1].Text.ToString(); } //完成后,自动登录。 if (web[tabControl1.SelectedIndex].ReadyState == WebBrowserReadyState.Complete) { try { HtmlElement name = web[tabControl1.SelectedIndex].Document.GetElementById("loginName"); HtmlElement pwd = web[tabControl1.SelectedIndex].Document.GetElementById("loginPwd"); name.SetAttribute("value", this.txtName.Text.Trim()); pwd.SetAttribute("value", this.txtPwd.Text.Trim()); web[tabControl1.SelectedIndex].Document.InvokeScript("checkSubmit"); } catch (Exception my) { MessageBox.Show("请登录到主页!"); } } } } //保存用户信息 private void btnSaveUser_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.txtName.Text)) { return; } if (string.IsNullOrEmpty(this.txtPwd.Text)) { return; } string name = this.txtName.Text.Trim(); string pwd = this.txtPwd.Text.Trim(); DBDataSet ds = new DBDataSet(); DBDataSetTableAdapters.tbAdminTableAdapter tba = new DBDataSetTableAdapters.tbAdminTableAdapter(); tba.Fill(ds.tbAdmin); tba.Insert(name, pwd); this.listView1.Items.Clear(); this.txtName.Text = ""; this.txtPwd.Text = ""; dbload(); } //数据加载 private void dbload() { DBDataSet ds = new DBDataSet(); DBDataSetTableAdapters.tbAdminTableAdapter tba = new DBDataSetTableAdapters.tbAdminTableAdapter(); tba.Fill(ds.tbAdmin); foreach (DBDataSet.tbAdminRow rw in ds.tbAdmin.Rows) { ListViewItem item = new ListViewItem(new string[] { "用户名", "密码" }); item.SubItems[0].Text = rw[0].ToString(); item.SubItems[1].Text = rw[1].ToString(); //listView1.Items.Insert(0, item); this.listView1.Items.Add(item); } } //删除用户 private void btnDelUser_Click(object sender, EventArgs e) { ds = new DBDataSet(); tba = new DBDataSetTableAdapters.tbAdminTableAdapter(); tba.Fill(ds.tbAdmin); string name = listView1.SelectedItems[0].SubItems[0].Text.ToString(); tba.DeleteQuery(name); listView1.SelectedItems[0].Remove(); } //双击直接登录 private void listView1_DoubleClick(object sender, EventArgs e) { //第一列 string x1 = this.listView1.SelectedItems[0].SubItems[0].Text.ToString(); //第二列 string x2 = this.listView1.SelectedItems[0].SubItems[1].Text.ToString(); this.txtName.Text = x1; this.txtPwd.Text = x2; btnLogin_Click(sender, e); } #region 导航命令 private void btnGoTO_Click(object sender, EventArgs e) { web[tabControl1.SelectedIndex].Navigate(this.combUrl.Text.Trim()); } private void btnGoback_Click(object sender, EventArgs e) { web[tabControl1.SelectedIndex].GoBack(); } private void btnGoF_Click(object sender, EventArgs e) { web[tabControl1.SelectedIndex].GoForward(); } private void btnRef_Click(object sender, EventArgs e) { web[tabControl1.SelectedIndex].Refresh(); } private void btnHome_Click(object sender, EventArgs e) { web[tabControl1.SelectedIndex].Navigate(new Uri(homeurl)); } #endregion //网址加载后,地址栏刷新 private void wbBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e) { combUrl.Text = web[tabControl1.SelectedIndex].Url.ToString(); combUrl.Items.Add(combUrl.Text); tabControl1.TabPages[tabControl1.SelectedIndex].Text = web[tabControl1.SelectedIndex].DocumentTitle; } //按回车导航新地址 private void combUrl_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) btnGoTO_Click(sender, e); } //本窗口打开新地址 private void wbBrowser_NewWindow(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; try { string url = this.web[tabControl1.SelectedIndex].Document.ActiveElement.GetAttribute("href"); this.web[tabControl1.SelectedIndex].Url = new Uri(url); } catch (Exception ee) { throw ee; } } //自建导航类 private void Navigate(string address) { if (string.IsNullOrEmpty(address)) return; if (address.Equals("about:blak")) return; if (!address.StartsWith("http://") && !address.StartsWith("https://")) { address = "http://" + address; } try { web[tabControl1.SelectedIndex].Navigate(new Uri(address)); } catch (System.UriFormatException) { return; } } //单击最后个tabpage新建Tab页 private void tabControl1_Click(object sender, EventArgs e) { int n = tabControl1.TabCount; if (tabControl1.SelectedIndex == n - 1) { TabPage tp = new TabPage(); tp.Controls.Add(web[tabControl1.SelectedIndex]); web[tabControl1.SelectedIndex].Dock = DockStyle.Fill; Navigate(combUrl.Text); tabControl1.TabPages.Insert(n - 1, tp); tabControl1.SelectTab(n - 1); web[tabControl1.SelectedIndex].DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted); } } private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) { int n = tabControl1.TabCount - 1; if (tabControl1.SelectedIndex != n) { } } //加载网页完成后的事件,多次用到 private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { combUrl.Text = e.Url.ToString(); tabControl1.SelectedTab.Text = web[tabControl1.SelectedIndex].DocumentTitle; loading = false; } //挂机,取消当前顶层焦点控制!可以失去焦点,继续播放 private void tsbhandup_Click(object sender, EventArgs e) { //IEBrowser ie = new IEBrowser(web[tabControl1.SelectedIndex]); //普通变量交互SET 1: .net数据写入页面中,并显示变量 //ie.InstallScript("function showinfo(){document.body.innerHTML +='姓名:'+name+' 年龄:'+age;}"); //ie.InstallTrace();//保证新变量能运行 //ie.__Set("name", "'小明'");//字符串开需加单引号 //ie.__Set("age", "10"); //ie.InvokeScript("showinfo"); //普通变量交互GET 2: 显示页面中的变量 //ie.InstallScript("function getinfo(){if(age<=10){age+=10};document.body.innerHTML +='姓名:'+name+' 年龄:'+age;}"); //// ie.InstallTrace();//保证新变量能运行 //ie.InvokeScript("getinfo"); //labIeGet.Text = string.Format("age={0}", ie.__Get<int>("age")); //普通变量交互Json变量 3: Json的变量获取 //先在页面中定义一个Json变量 //普通变量交互Json变量 4: 写入Json的变量到页面 //服务器Json时间地址:http://lms.chinahrt.com/course/lmsHeart_json.do?userId=b02109bd-6539-4ef7-a0c6-ef1a76cc54bc //返回:{"ret":0, "msg":"", "json":{"time":1375064355258}} //HttpWebRequest request =(HttpWebRequest)HttpWebRequest.Create("http://lms.chinahrt.com/course/lmsHeart_json.do?userId=b02109bd-6539-4ef7-a0c6-ef1a76cc54bc"); //request.Timeout=5000; //request.Method="GET"; //HttpWebResponse response=(HttpWebResponse)request.GetResponse(); //StreamReader sr=new StreamReader(response.GetResponseStream()); //string jsonstr=sr.ReadLine(); //JavaScriptSerializer j = new JavaScriptSerializer(); //string time = j.Deserialize<string>(jsonstr); //MessageBox.Show(jsonstr); //置于顶层显示 //this.TopMost = true; } //直接学完 private void tsbCompleted_Click(object sender, EventArgs e) { //labIeGet.Text = ie.__Get<string>("positiontime"); // HtmlDocument doc = web[tabControl1.SelectedIndex].Document; #region 本方法已经实现c#执行现有javascript函数的功能,实现了“课程学习完毕”,但并未真的学习完毕 //doc.InvokeScript("doLMSSetValue",new object[]{ "cmi.core.lesson_status", "completed" }); //doc.InvokeScript("doLMSCommit"); //doc.InvokeScript("doLMSFinish"); #endregion #region 试着调用jwplay功能 方法一用webbrowser执行函数 HtmlDocument doc = web[tabControl1.SelectedIndex].Document.Window.Frames[3].Document.Window.Frames[1].Document;//选择到media.htm //for (int i = 0; i < doc.Window.Frames.Count; i++) //{ // MessageBox.Show(doc.Window.Frames[i].Document.Url.ToString()); //} // HtmlDocument doc = web[tabControl1.SelectedIndex].Document.Window.Frames["1"].Document; //doc.InvokeScript("flv.scormplay"); doc.InvokeScript("onComplete"); #endregion } private void toolStripButton3_Click(object sender, EventArgs e) { //HtmlDocument doc = web[tabControl1.SelectedIndex].Document; //string userId = doc.GetElementById("userId").GetAttribute("value"); //string userName = doc.GetElementById("userNmae").GetAttribute("value"); //string entitId = doc.GetElementById("entitId").GetAttribute("value"); //string courseId = doc.GetElementById("courseId").GetAttribute("value"); //string itemIndex = doc.GetElementById("itemIndex").GetAttribute("value"); //string toWait = doc.GetElementById("toWait").GetAttribute("value"); //string exit = doc.GetElementById("exit").GetAttribute("value"); //labIeGet.Text = userId + "----" + userName + "----" + entitId + "----" + courseId + "----" + itemIndex + "----" + toWait + "----" + exit; //todo:浏览过的网址存盘ctrl+w+t //todo:双击删除当前tab 或加X按键 //todo:视频下载地址 } } }