C# winform使用webbrowser控件,不弹出新的页面,只在当前窗口打开


1.记得导入类库:using System.ComponentModel;
2.修改webbrowser的属性:
AllowWebBrowserDrop设为 false
WebBrowserShortcutsEnabled设为 false
IsWebBrowserContextMenuEnabled设为 false

3.具体的设置代码如下:


       private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
       {
                e.Cancel = true;
       }        
 
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {            
            foreach (HtmlElement links in this.webBrowser1.Document.Links)
            {
                links.SetAttribute("target", "_self");
            }            
            foreach (HtmlElement form in this.webBrowser1.Document.Forms)
            {
                form.SetAttribute("target", "_self");
            }
        }


你可能感兴趣的:(C#,库,WebBrowser)