【问题标题】:Zoom WPF-WebBrowser-Control Content缩放 WPF-WebBrowser-Control 内容
【发布时间】:2017-06-11 19:05:05
【问题描述】:

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

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

我想阻止使用 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-Method 中的代码,其中发生了 HRESULT: 0x80020101-Error。

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(",", ".") + ";");
}

【问题讨论】:

    标签: c# wpf webbrowser-control


    【解决方案1】:
    webBrowser.LoadCompleted += Web_LoadCompleted;
    
    private void Web_LoadCompleted (object sender, NavigationEventArgs e)
    {
        // Place your code here
    }
    

    【讨论】:

      【解决方案2】:

      它总是说 mshtml.IHTMLDocument2 为空。

      这与网络浏览器控件的线程模型有关。在您离开构造函数之前,您在 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 适合缩放。

      【讨论】:

      • 实施您的解决方案后出现异常:HRESULT: 0x80020101
      • 这一行里面。 doc.parentWindow.execScript("document.body.style.zoom=" + Zoom.ToString().Replace(",", ".") + ";");
      猜你喜欢
      • 1970-01-01
      • 2013-07-26
      • 2011-08-26
      • 2021-07-14
      • 1970-01-01
      • 2010-11-20
      • 2011-09-16
      • 2011-01-20
      • 1970-01-01
      相关资源
      最近更新 更多