积极答复者
c#或c++怎样控制ie

问题
-
我可以打开一个ie但是我就不能再对它控制了,有没有高手给些代码
Process currentProcess;
currentProcess = System.Diagnostics.Process.Start("http://192.168.18.66:8080/pickphone/index.do");
System.Diagnostics.Process.Start("http://192.168.18.66:8080/pickphone/index.do");
Thread.Sleep(10000);
//currentProcess.Refresh();
//currentProcess.Kill();
因为调用 IwebBrowser 老出现js错误但是在正常的ie下是不出现这类问题的- 已编辑 刘峰选 2009年7月23日 4:39
- 已移动 Sheng Jiang 蒋晟 2009年7月23日 5:11 IE (发件人:Visual C#)
答案
-
试一下
string target = "_self";
explorer.Navigate2(ref uri, ref flag, ref target, ref empty, ref empty);- 已标记为答案 Allen Chen - MSFTModerator 2009年8月5日 5:53
全部回复
-
你好!
这样可以打开某个网页,如果想更多的控制还是直接使用WebBrowser控件好一些:
using System.Diagnostics; public Process p1 = null; string program = @"C:\Program Files\Internet Explorer\IEXPLORE.EXE"; string argument = @"http://www.google.com/"; p1 = new Process(); p1.StartInfo.FileName = program; p1.StartInfo.Arguments = argument; p1.Start();
周雪峰 -
[ComImport] [Guid("0002DF01-0000-0000-C000-000000000046")] public class InternetExplorer { } public static void SpawnIEWithSource(String szHtml) { PInvoke.ShellDocView.IWebBrowser2 ie; ie = (PInvoke.ShellDocView.IWebBrowser2)new PInvoke.ShellDocView.InternetExplorer(); //Navigate to about:blank to initialize the browser object o = System.Reflection.Missing.Value; String url = @"about:blank"; ie.Navigate2(ref url, ref o, ref o, ref o, ref o); //stuff contents into the document object webDocument = ie.Document; //webDocument.Write(szHtml); //webDocument.Close(); ie.Visible = true; } [ComImport, DefaultMember("Name"), Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch), SuppressUnmanagedCodeSecurity] public interface IWebBrowser2 { [DispId(500)] void Navigate2([In] ref object URL, [In] ref object Flags, [In] ref object TargetFrameName, [In] ref object PostData, [In] ref object Headers); [DispId(0xcb)] object Document { [return: MarshalAs(UnmanagedType.IDispatch)] get; } }
Please mark the post answered your question as the answer, and click the chartreuse pyramid floating over "Vote as helpful" to mark other helpful posts as helpful. This posting is provided "AS IS" with no warranties, and confers no rights.
Visual C++ MVP -
这里是我很久以前写的一个代码,基本思想是InternetExplorer是在进程外启动IE的,然后你可以通过DCOM技术来监听IE的各个事件.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;using SHDocVw;
using mshtml;namespace LLas.CiteWork.GUI
{
/// <summary>
/// ISIForm 的摘要说明。
/// </summary>
public class ISIForm : System.Windows.Forms.Form
{
public string Host
{
get { return _host; }
set { _host = value; }
}public static string PostData
{
get { return _postData; }
}public static string DeleteHistoryData
{
get { return _deleteHistory; }
}public delegate void AuthenticationDoneHandler(
object source, EventArgs e);
public event AuthenticationDoneHandler OnAuthenticationDoneHandler;private System.Windows.Forms.Button btnISIStart;
private static string _postData = null;
private static string _deleteHistory = null;/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private string _host = null;
private System.Windows.Forms.Button btnListen;
private System.Windows.Forms.Button button1;
private static InternetExplorer explorer = null;public ISIForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}public ISIForm(string host)
: this()
{
_host = host;
}public string GetCookie(string name)
{
if (null == explorer)
throw new InvalidOperationException("IE窗口没有打开!");if (null == _postData)
throw new InvalidOperationException("IE窗口没有打开!");int index = _postData.IndexOf("SID");
int iDelimit = _postData.IndexOf("&", index);
string ret = _postData.Substring(index, iDelimit - index);return ret;
}/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnISIStart = new System.Windows.Forms.Button();
this.btnListen = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnISIStart
//
this.btnISIStart.Location = new System.Drawing.Point(200, 16);
this.btnISIStart.Name = "btnISIStart";
this.btnISIStart.Size = new System.Drawing.Size(80, 23);
this.btnISIStart.TabIndex = 0;
this.btnISIStart.Text = "开始处理(&P)";
this.btnISIStart.Click += new System.EventHandler(this.btnISIStart_Click);
//
// btnListen
//
this.btnListen.Location = new System.Drawing.Point(8, 16);
this.btnListen.Name = "btnListen";
this.btnListen.Size = new System.Drawing.Size(80, 23);
this.btnListen.TabIndex = 1;
this.btnListen.Text = "学习搜索(&L)";
this.btnListen.Click += new System.EventHandler(this.btnListen_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 23);
this.button1.TabIndex = 2;
this.button1.Text = " 删除历史(&H)";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// ISIForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(282, 55);
this.Controls.Add(this.button1);
this.Controls.Add(this.btnListen);
this.Controls.Add(this.btnISIStart);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "ISIForm";
this.Text = "ISIForm";
this.Closing += new System.ComponentModel.CancelEventHandler(this.ISIForm_Closing);
this.Load += new System.EventHandler(this.ISIForm_Load);
this.ResumeLayout(false);}
#endregionprivate void ISIForm_Load(object sender, System.EventArgs e)
{
if (null == _host)
return;explorer = new InternetExplorerClass();
object uri = _host;
object empty = Type.Missing;
explorer.Visible = true;
explorer.Navigate2(ref uri, ref empty, ref empty, ref empty, ref empty);
}protected void SinkEvents()
{
DWebBrowserEvents2_BeforeNavigate2EventHandler DBeforeNavigateE
= new DWebBrowserEvents2_BeforeNavigate2EventHandler(OnBeforeNavigate2);
explorer.BeforeNavigate2 += DBeforeNavigateE;
}protected void SinkEvents1()
{
DWebBrowserEvents2_BeforeNavigate2EventHandler DBeforeNavigateE
= new DWebBrowserEvents2_BeforeNavigate2EventHandler(OnDeleteHistory);
explorer.BeforeNavigate2 += DBeforeNavigateE;
}protected static void OnDeleteHistory(Object ob1, ref Object URL, ref Object Flags, ref Object TargetFrameName,
ref Object PostData, ref Object Headers, ref bool Cancel)
{
_deleteHistory = System.Text.Encoding.Default.GetString(PostData as byte[]);DWebBrowserEvents2_BeforeNavigate2EventHandler DBeforeNavigateE
= new DWebBrowserEvents2_BeforeNavigate2EventHandler(OnDeleteHistory);
explorer.BeforeNavigate2 -= DBeforeNavigateE;
}protected static void OnBeforeNavigate2(Object ob1, ref Object URL, ref Object Flags, ref Object TargetFrameName,
ref Object PostData, ref Object Headers, ref bool Cancel)
{
_postData = System.Text.Encoding.Default.GetString(PostData as byte[]);DWebBrowserEvents2_BeforeNavigate2EventHandler DBeforeNavigateE
= new DWebBrowserEvents2_BeforeNavigate2EventHandler(OnBeforeNavigate2);
explorer.BeforeNavigate2 -= DBeforeNavigateE;
}private void btnISIStart_Click(object sender, System.EventArgs e)
{
if (null == OnAuthenticationDoneHandler) return;EventArgs e1 = new EventArgs();
OnAuthenticationDoneHandler(this, e1);
}private void ISIForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (null != explorer)
explorer.Quit();
}private void btnListen_Click(object sender, System.EventArgs e)
{
SinkEvents();
}private void button1_Click(object sender, System.EventArgs e)
{
SinkEvents1();
}
}
} -
explorer = new InternetExplorerClass();
object uri = "http://192.168.18.66:8080/pickphone/index.do";
object empty = System.Reflection.Missing.Value;
object flag = 4;
explorer.Visible = true;
explorer.Navigate2(ref uri, ref flag, ref empty, ref empty, ref empty);
我现在每次发的请求还是重新打开一个ie很郁闷,
但是有没有办法不从新打开而是本页面就跳转呢 -
试一下
string target = "_self";
explorer.Navigate2(ref uri, ref flag, ref target, ref empty, ref empty);- 已标记为答案 Allen Chen - MSFTModerator 2009年8月5日 5:53