none
c#或c++怎样控制ie RRS feed

  • 问题

  • 我可以打开一个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:33

答案

全部回复

  • http://www.google.com

    http://msdn.microsoft.com

    以WebBrowser做关键字进行搜索,上面两个地方可以给你答案。

    2009年7月23日 4:35
  • 因为调用 IwebBrowser 老出现js错误但是在正常的ie下是不出现这类问题的
    2009年7月23日 4:41
  • 你好!
         这样可以打开某个网页,如果想更多的控制还是直接使用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();
    
    

    周雪峰
    2009年7月23日 4:55
  • System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe"
                    , "http://social.microsoft.com/Forums/zh-CN/visualcshartzhchs/thread/4b894b33-c2f3-4d07-a197-dcbf9ffcefd9");

    知识改变命运,奋斗成就人生!
    2009年7月23日 4:58
    版主
  • [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
    2009年7月23日 5:15
  • 这里是我很久以前写的一个代码,基本思想是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);

            }
            #endregion

            private 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();
            }
        }
    }

    2009年7月23日 5:18
  • Killmyday   您好,您的代码要加入什么组件吗,您的 using SHDocVw;
    using mshtml; 哪里弄的?
    2009年7月23日 6:05
  • 在Visual Studio里面的引用里面,进入.NET组件标签,选择“Microsoft.mshtml”,里面应该有好几个,随便选择一个好了,如果你不放心随便选择的话,就选择路径是c:\program files\microsoft.net\primary interop assemblies\microsoft.mshtml.dll的那个好了,至于为什么会有那么多的Microsoft.mshtml应该是历史遗留问题。

    2009年7月23日 7:53
  • 而shdocvw呢,需要在添加引用对话框里,选择浏览标签,然后添加c:\windows\system32\shdocvw.dll就可以了。
    2009年7月23日 7:54
  •   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很郁闷,
    但是有没有办法不从新打开而是本页面就跳转呢
    2009年7月23日 8:38
  • 试一下
    string target = "_self";
    explorer.Navigate2(ref uri, ref flag, ref target, ref empty, ref empty);
    2009年7月23日 11:07