WebBrowser Space Connection InvokeMember ("click") in C # has an invalid problem

Source: Internet
Author: User

First, make sure that you get the connection object.

New window opened because it was set

private void Webbrowser1_newwindow (object sender, CancelEventArgs e)
{
E.cancel=true;
}

So the connection to the pop-up window in WebBrowser is masked and the display is invalid.

Method One,

The target of all links, point to this form,    foreach (HtmlElement archor in this.webBrowser1.Document.Links)    {        archor. SetAttribute ("Target", "_self");    }    Put all the form's submission targets, pointing to this form,    foreach (htmlelement form in this.webBrowser1.Document.Forms)    {        form. SetAttribute ("Target", "_self");    }

  

Set the Allowwebbrowserdrop of WebBrowser to False
Set the webbrowsershortcutsenabled of WebBrowser to False
Set the iswebbrowsercontextmenuenabled of WebBrowser to False


Method Two

Get the URL you want to open in the NewWindow event, cancel opening a new window, and open the URL you want to turn to in this window
private void Webbrowser_1_newwindow (object sender, CancelEventArgs e) {webBrowser webbrowser_temp = (webBrowser) sender; String newurl = Webbrowser_temp. Document.ActiveElement.GetAttribute ("href"); Webbrowser_1.url = new Uri (Newurl); E.cancel = true; }

  

Although this method can solve most of the requirements, but the symptoms do not cure.
And when there are Chinese characters in the URL is more troublesome, such as Baidu MP3, song names are Chinese characters, and Baidu space inside, most of the user name is Chinese characters, so take the URLs are garbled. Although the URL can be encoded, but not all of the pages are UFT-8 encoding, how to know the page encoding is a topic.

Method Three

The most fundamental way is to rewrite it.
To create a new class

Using system;using system.collections.generic;using system.text;namespace webtestrecorder{public class         ExtendedWebBrowser:System.Windows.Forms.WebBrowser {System.Windows.Forms.AxHost.ConnectionPointCookie cookie;        Webbrowserextendedevents events; This method is called to give you a chance to create your own event sink protected override void CreateSink ( {//make sure to call the base or the normal events won ' t Fire BASE.            CreateSink ();            Events = new Webbrowserextendedevents (this); cookie = new System.Windows.Forms.AxHost.ConnectionPointCookie (this.        Activexinstance, Events, typeof (DWebBrowserEvents2)); } protected override void Detachsink () {if (null! = cookie) {COOKIE.D                Isconnect ();            cookie = null; } base.        Detachsink (); }//this new event would fire when the page was navigating public Event eventhandler<webbrowserextendednavigatingeventargs> BeforeNavigate;        public event eventhandler<webbrowserextendednavigatingeventargs> Beforenewwindow; protected void Onbeforenewwindow (string URL, out bool cancel) {Eventhandler<webbrowserextendednaviga            tingeventargs> h = beforenewwindow;            Webbrowserextendednavigatingeventargs args = new Webbrowserextendednavigatingeventargs (URL, null);            if (null! = h) {h (this, args); } Cancel = args.        Cancel; } protected void Onbeforenavigate (string url, string frame, out bool cancel) {Eventhandler<we            bbrowserextendednavigatingeventargs> h = beforenavigate;            Webbrowserextendednavigatingeventargs args = new Webbrowserextendednavigatingeventargs (URL, frame);            if (null! = h) {h (this, args); }//pass the cancellation chosen back OUT to the events Cancel = args.        Cancel; }//this class would capture events from the WebBrowser class WebBrowserExtendedEvents:System.Runtime.Inter            Opservices.standardolemarshalobject, DWebBrowserEvents2 {extendedwebbrowser _browser;            Public webbrowserextendedevents (Extendedwebbrowser browser) {_browser = browser;} Implement Whichever events wish public void BeforeNavigate2 (object pdisp, ref object URL, ref object flag s, ref object Targetframename, ref object PostData, ref object headers, ref bool cancel) {_brow Ser.            Onbeforenavigate (String) URL, (String) Targetframename, out cancel); public void NewWindow3 (object pdisp, ref bool Cancel, ref object flags, ref object Urlcontext, ref object URL            {_browser.onbeforenewwindow (String) URL, out cancel); }} [System.Runtime.InteropServices.ComImport (), SysteM.runtime.interopservices.guid ("34a715a0-6587-11d0-924a-0020afc7ac4d"), System.Runtime.InteropServices.InterfaceTypeAttribute (        System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch), System.Runtime.InteropServices.TypeLibType (System.Runtime.InteropServices.TypeLibTypeFlags.FHidden)] public Interface DWebBrowserEvents2 {[System.Runtime.InteropServices.DispId (+)] void Beforenaviga TE2 ([System.Runtime.InteropServices.In, System.Runtime.InteropServices.MarshalAs (system.run Time.                 InteropServices.UnmanagedType.IDispatch)] "Object pdisp, [System.Runtime.InteropServices.In] ref object URL, [SYSTEM.RUNTIME.INTEROPSERVICES.IN] Ref object flags, [System.Runtime.InteropServices.In] R EF Object Targetframename, [System.Runtime.InteropServices.In] Ref object PostData, [System.Runtime.Interop Services.in] Ref object headers, [System.runTime.            Interopservices.in, System.Runtime.InteropServices.Out] ref bool cancel); [System.Runtime.InteropServices.DispId (273)] void NewWindow3 ([System.Runtime.InteropServices.In , System.Runtime.InteropServices.MarshalAs (System.Runtime.InteropServices.UnmanagedType.IDispatch)] Object                Pdisp, [System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out] ref bool Cancel, [SYSTEM.RUNTIME.INTEROPSERVICES.IN] Ref object flags, [System.Runtime.InteropServices.In] ref object UR        Lcontext, [System.Runtime.InteropServices.In] ref object URL); }} public class WebBrowserExtendedNavigatingEventArgs:System.ComponentModel.CancelEventArgs {p rivate        String _url;        public string Url {get {return _url;}        } p rivate string _frame;        public string Frame {get {return _frame;}   }     Public Webbrowserextendednavigatingeventargs (string URL, string frame): Base () {_url =            Url        _frame = Frame; }    }}

 

Then replace the WebBrowser with our rewritten Extendedwebbrowser to add event handling

Iebrowser = new Extendedwebbrowser (); Iebrowser.beforenewwindow + = new eventhandler< Webbrowserextendednavigatingeventargs> (Iebrowser_beforenewwindow);

Then in its Beforenewwindow event:

void Iebrowser_beforenewwindow (object sender, Webbrowserextendednavigatingeventargs e) {     e.cancel=true;     ((extendedwebbrowser) sender). Navigate (E.url);}

  



WebBrowser Space Connection InvokeMember ("click") in C # has an invalid problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.