打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
WPF界面添加WinForm的Webbrowser控件

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(禁止右键上下文菜单)

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
如何:创建 C# Windows 窗体应用程序
【C#控件】MenuStrip控件(菜单控件)
如何在多线程中调用winform窗体控件
Visual?C#?windows窗体示例主题(一)(MSDN整理)
C#多线程窗体控件安全访问
C#"焦点事件"中的Validating处理方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服