1.添加引用: WindowsFormsIntegration.dll System.Windows.Forms.dll 2.頁面代碼: <Window x:Class="Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="378" Width="620" Loaded="Window_Loaded" > <Grid Name="grid"> </Grid> </Window> 3.后臺代碼: System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost(); System.Windows.Forms.WebBrowser web = new System.Windows.Forms.WebBrowser(); web.Url = new Uri("http://www.baidu.com"); host.Child = web; this.grid.Children.Add(host); 防止彈出新的頁面,所有的頁面只能在webbrowser控件中顯示。 代碼如下: 【將所有的連接都指向本窗體】 private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{ //將所有的鏈接的目標,指向本窗體 foreach (HtmlElement archor in this.webBrowser.Document.Links) { archor.SetAttribute("target", "_self"); } //將所有的FORM的提交目標,指向本窗體 foreach (HtmlElement form in this.webBrowser.Document.Forms) { form.SetAttribute("target", "_self"); } } 【取消新窗口事件】 private view sourceprint?private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{ e.Cancel = true; } 將 WebBrowser 的 AllowWebBrowserDrop 設為 false(禁止拖放) 將 WebBrowser 的 WebBrowserShortcutsEnabled 設為 false(禁止使用快捷鍵) 將 WebBrowser 的 IsWebBrowserContextMenuEnabled 設為 false(禁止右鍵上下文菜單) |
|
來自: 風中Robin > 《WebBrowser》