简体   繁体   English

从C#WinForms应用程序禁用打开超链接的浏览器

[英]Disable hyperlink opening browser from c# winforms application

We have a .Net application that embeds google maps in some of the pages. 我们有一个.Net应用程序,它将Google地图嵌入到某些页面中。 We would like to prevent the user from opening a WebBrowser control when they click on a link found in Google Maps (such as in info tips on places etc...). 我们希望防止用户单击Google地图中的链接(例如在地方的信息提示等中)时打开WebBrowser控件。 Is this possible? 这可能吗? I'm fairly new to .Net :) 我对.Net还是相当陌生:)

尝试使用WebBrowser.AllowNavigation禁用导航

You can prevent navigations that are based on clicking a link using these 2 ways: 您可以使用以下两种方式来防止基于单击链接的导航:

1- Using AllowNavigation property 1-使用AllowNavigation属性

this.webBrowser1.AllowNavigation = false;

2- Using Navigating event 2-使用Navigating事件

private void BrowserSample_Load(object sender, EventArgs e)
{
    this.webBrowser1.Navigate("http://www.google.com");
    this.webBrowser1.Navigating += webBrowser1_Navigating;
}

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    //you can use e.Url to allow or deny navigation based on your logic
    e.Cancel = true;
}

Consider these notes when using one of the ways above: 使用上述方法之一时,请考虑以下注意事项:

  • Using Navigating event you can gain more control over navigation and you can prevent some navigations and allow some others. 使用Navigating事件,您可以更好地控制导航,还可以阻止某些导航并允许其他一些导航。
  • When using Navigating event, remember to add handler of this event only after you navigated to your desired url. 使用Navigating事件时,请记住仅在导航到所需的URL后才添加此事件的处理程序。
  • Using these ways, you can only prevent navigations that are based on clicking a link, for example if you set the initial url too google, then you can perform search but you can't navigate to urls or results. 使用这些方法,您只能阻止基于单击链接的导航,例如,如果您将初始URL也设置为google,那么您可以执行搜索,但不能导航到URL或结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM