WebBrowser in WPF

Source: Internet
Author: User

MainWindow.xaml.cs

New Window Events
{URI Uri=NewUri (Textbox_uri.                    Text); System.Windows.Forms.Integration.WindowsFormsHost Host=NewSystem.Windows.Forms.Integration.WindowsFormsHost (); Extendedwebbrowser WebBrowser1=NewExtendedwebbrowser (); Host. Child=WebBrowser1;  This. WEBGRID.CHILDREN.ADD (host); Webbrowser1.beforenewwindow+=NewEventhandler<webbrowserextendednavigatingeventargs>(Webbrowser1_beforenewwindow); Webbrowser1.url=URI; Webbrowser1.scripterrorssuppressed=true;//Disable popup script error}
New Window Events
void Webbrowser1_beforenewwindow (object  sender, Webbrowserextendednavigatingeventargs e)        {            true;            ((extendedwebbrowser) sender). Navigate (E.url);        }
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;usingSystem.Reflection;usingSystem.IO;usingMicrosoft.Office.Interop.Word;usingWord =Microsoft.Office.Interop.Word;usingSystem.Windows.Xps.Packaging;

ExtendedWebBrowser.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacewpf_webbrowser{ Public classExtendedWebBrowser:System.Windows.Forms.WebBrowser {System.Windows.Forms.AxHost.ConnectionPointCookie          Cookies;            Webbrowserextendedevents events; //This method is called to give you a chance to create your own event sink        protected Override voidCreateSink () {//Make sure to call the BASE or the normal events won ' t fire            Base.              CreateSink (); Events=NewWebbrowserextendedevents ( This); Cookies=NewSystem.Windows.Forms.AxHost.ConnectionPointCookie ( This. Activexinstance, Events,typeof(DWEBBROWSEREVENTS2)); }            protected Override voidDetachsink () {if(NULL!=cookies) {cookie.                  Disconnect (); Cookies=NULL; }              Base.          Detachsink (); }            //This new event would fire if the page is navigating         Public EventEventhandler<webbrowserextendednavigatingeventargs>beforenavigate;  Public EventEventhandler<webbrowserextendednavigatingeventargs>Beforenewwindow; protected voidOnbeforenewwindow (stringUrl out BOOLcancel) {EventHandler<WebBrowserExtendedNavigatingEventArgs> h =Beforenewwindow; Webbrowserextendednavigatingeventargs args=NewWebbrowserextendednavigatingeventargs (URL,NULL); if(NULL!=h) {h ( This, args); } Cancel=args.          Cancel; }            protected voidOnbeforenavigate (stringUrlstringFrame out BOOLcancel) {EventHandler<WebBrowserExtendedNavigatingEventArgs> h =beforenavigate; Webbrowserextendednavigatingeventargs args=NewWebbrowserextendednavigatingeventargs (URL, frame); if(NULL!=h) {h ( This, args); }              //Pass The cancellation chosen back out to the eventsCancel =args.          Cancel; }          //This class would capture events from the WebBrowser        classWebBrowserExtendedEvents:System.Runtime.InteropServices.StandardOleMarshalObject, DWebBrowserEvents2 {              Extendedwebbrowser _browser;  PublicWebbrowserextendedevents (Extendedwebbrowser browser) {_browser =Browser;} //Implement Whichever events you wish             Public voidBeforeNavigate2 (ObjectPdisp,ref ObjectUrlref ObjectFlagsref ObjectTargetframename,ref ObjectPostData,ref ObjectHeadersref BOOLcancel) {_browser.onbeforenavigate (string) URL, (string) Targetframename, outcancel); }                 Public voidNEWWINDOW3 (ObjectPdisp,ref BOOLCancelref ObjectFlagsref ObjectUrlcontext,ref ObjectURL) {_browser.onbeforenewwindow (string) URL, outcancel); }} [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 InterfaceDWebBrowserEvents2 {[System.Runtime.InteropServices.DispId ( -)]              voidBeforeNavigate2 ([System.Runtime.InteropServices.In, System.Runtime.InteropServices . MarshalAs (System.Runtime.InteropServices.UnmanagedType.IDispatch)]Objectpdisp, [System.Runtime.InteropServices.In]ref ObjectURL, [System.Runtime.InteropServices.In]ref Objectflags, [System.Runtime.InteropServices.In]ref ObjectTargetframename, [System.Runtime.InteropServices.In]ref Objectpostdata, [System.Runtime.InteropServices.In]ref Objectheaders, [System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out] /c10>ref BOOLcancel); [System.Runtime.InteropServices.DispId (273)]              voidNewWindow3 ([System.Runtime.InteropServices.In, System.Runtime.InteropServices.Mars HalAs (System.Runtime.InteropServices.UnmanagedType.IDispatch)]Objectpdisp, [System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out]ref BOOLCancel, [System.Runtime.InteropServices.In]ref Objectflags, [System.Runtime.InteropServices.In]ref ObjectUrlcontext, [System.Runtime.InteropServices.In]ref ObjectURL); }      }         Public classWebBrowserExtendedNavigatingEventArgs:System.ComponentModel.CancelEventArgs {Private string_url;  Public stringURL {Get{return_url;} }            Private string_frame;  Public stringFrame {Get{return_frame;} }             PublicWebbrowserextendednavigatingeventargs (stringUrlstringframe):Base() {_url=URL; _frame=frame; }      }}

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.