2017-01-26 147 views
0

我想在wpf WebBrowser控件中显示一个网站。但是,内容的大小是大的,所以有滚动条,你可以在这里看到:缩放WPF-WebBrowser-Control内容

enter image description here

我想显示在该窗口的整个网站,而不调整其大小。我倒是喜欢的页面内放大,使它看起来像这样:

enter image description here

我倒是想,以防止使用JavaScript这样做。这里显示的WPF方式WPF WebBrowser - How to Zoom Content?也没有为我工作。它总是说mshtml.IHTMLDocument2为空。

我也喜欢用WindowsForms来阻止它。我希望有一个“唯一的XAML” - 解决这个问题的方法。

这是我的代码:

<Window x:Class="BrowserApp.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"> 
     <Grid> 
      <WebBrowser Source="https://www.google.de/"></WebBrowser> 
     </Grid> 
    </Window> 

谢谢!

编辑

这是我里面的Webbrowser1_Navigated法,其中HRESULT代码:0x80020101错误发生。

private void Webbrowser1_Navigated(object sender, NavigationEventArgs e) 
{ 
    double Zoom = 0.5; 
    mshtml.IHTMLDocument2 doc = Webbrowser1.Document as mshtml.IHTMLDocument2; 
    doc.parentWindow.execScript("document.body.style.zoom=" + Zoom.ToString().Replace(",", ".") + ";"); 
} 

回答

0

它总是说,mshtml.IHTMLDocument2为空。

这与Web浏览器控件的线程模型有关。您在XAML或代码中设置的任何导航都将在您离开构造函数之后才会完成。这意味着您必须在Navigated事件触发后或定时器触发后执行缩放代码。

这就是我该怎么做的。

  public MainWindow() 
      { 
       InitializeComponent(); 
       Webbrowser1.Navigate("http://www.google.com"); //won't complete until you leave this code block 
       Webbrowser1.Navigated += Webbrowser1_Navigated; 

      } 

      private void Webbrowser1_Navigated(object sender, NavigationEventArgs e) 
      { 
       //do your zoom code here 
      } 
} 

在您的XAML中,您可以引用该事件,但仍然必须执行缩放部分。

<WebBrowser Navigated="Webbrowser1_Navigated" Name="Webbrowser1"/> 

当然the other stackoverflow post's code是罚款变焦。

+0

实现您的解决方案后,我得到异常:HRESULT:0x80020101 – L4c0573

+0

什么行代码triggeres的HRESULT? –

+0

在这条线内。 doc.parentWindow.execScript(“document.body.style.zoom =”+ Zoom.ToString()。Replace(“,”,“。”)+“;”); – L4c0573

0
webBrowser.LoadCompleted += Web_LoadCompleted; 

private void Web_LoadCompleted (object sender, NavigationEventArgs e) 
{ 
    // Place your code here 
}