我使用的是wpf WebBrowser控件(System.Windows.Controls),我需要防止用户执行各种操作,如下载文件或打印页面。我已经在Internet Explorer选项中禁用了文件下载选项(安全选项卡->自定义级别->下载->文件下载)。正因为如此,在点击一个pdf链接后,我得到的不是文件下载弹出窗口,而是弹出窗口中的消息:“您当前的安全设置不允许下载此文件”。
有没有办法防止这条消息发生?我只希望从用户的角度不执行任何操作。我使用IE10。
发布于 2013-08-16 16:34:32
WPF WebBrowser是WebBrowser ActiveX控件的一个非常有限的(但不可扩展的sealed)包装器。幸运的是,我们可以使用一种技巧来获得底层的ActiveX对象(请注意,在.NET的未来版本中,这可能会发生变化)。以下是如何阻止文件下载的方法:
using System.Reflection;
using System.Windows;
namespace WpfWbApp
{
// By Noseratio (http://stackoverflow.com/users/1768303/noseratio)
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.WB.Loaded += (s, e) =>
{
// get the underlying WebBrowser ActiveX object;
// this code depends on SHDocVw.dll COM interop assembly,
// generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
// and add as a reference to the project
var activeX = this.WB.GetType().InvokeMember("ActiveXInstance",
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, this.WB, new object[] { }) as SHDocVw.WebBrowser;
// now we can handle previously inaccessible WB events
activeX.FileDownload += activeX_FileDownload;
};
this.Loaded += (s, e) =>
{
this.WB.Navigate("http://technet.microsoft.com/en-us/sysinternals/bb842062");
};
}
void activeX_FileDownload(bool ActiveDocument, ref bool Cancel)
{
Cancel = true;
}
}
}
XAML:
<Window x:Class="WpfWbApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<WebBrowser Name="WB"/>
</Window>
https://stackoverflow.com/questions/17697023
复制相似问题