首页 文章

将Dialog / WebBrowser调整为网页宽度

提问于
浏览
1

有没有办法根据加载的初始网页使用webbrowser控件自动调整窗口大小?

没有运气.Height或.Width属性,也试过了ActualWidth,ActualHeight .

这是XAML .

<dialog:Window x:Class="MyApplication.Widgets.Controls.DiagnosticsWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:dialog="clr-namespace:UI.Common.Dialog;assembly=UI.Common"
            xmlns:controls="clr-namespace:UI.Common.Controls;assembly=UI.Common"
            mc:Ignorable="d" 
            Height="500" Width="600" 
            ShowInTaskbar="False" 
            ResizeMode="NoResize"
            Title="{Binding Path=WindowTitle}"
            WindowStartupLocation="CenterOwner">

    <controls:DialogFrame Width="{Binding Path=WindowWidth}" Height="Auto" CloseCommand="Close">
        <WebBrowser x:Name="DiagnosticsWebBrowser" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    </controls:DialogFrame>
</dialog:Window>

我试着做下面没有运气,WPF: How to auto-size WebBrowser inside a Popup?

这是我目前得到的......(也没有正确调整大小) .
enter image description here

2 回答

  • 4

    WPF Web浏览器控件不会公开所需的已加载页面大小以调整窗口大小 . 最简单的解决方案是使用WinForms WebBrowser(由Noseratio建议),您可以使用WindowsFormsHost元素将其嵌入到WPF窗口中:

    <Window 
        x:Class="MyApplication.Widgets.Controls.DiagnosticsWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        mc:Ignorable="d" 
        SizeToContent="WidthAndHeight"
        WindowStartupLocation="Manual">
    
        <WindowsFormsHost>
            <forms:WebBrowser 
                x:Name="DiagnosticsWebBrowser" 
                Width="420"
                Height="240"
                ScrollBarsEnabled="False" 
                DocumentCompleted="DiagnosticsWebBrowser_DocumentCompleted" />
        </WindowsFormsHost>
    </Window>
    

    不要忘记将System.Windows.Forms程序集添加到项目中 .

    然后,在您的代码隐藏中:

    // Called after the document has been rendered
    private void DiagnosticsWebBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
        {
            // Resize the window
            int width = WebBrowser.Document.Body.ScrollRectangle.Size.Width;
            width = Math.Min(width, (int)SystemParameters.WorkArea.Width - 100);
    
            int height = WebBrowser.Document.Body.ScrollRectangle.Size.Height;
            height = Math.Min(height, (int)SystemParameters.WorkArea.Height - 100);
    
            DiagnosticsWebBrowser.Size = new System.Drawing.Size(width, height);
            UpdateLayout();
    
            // Re-center the window
            WindowStartupLocation = WindowStartupLocation.Manual;
            Left = (SystemParameters.WorkArea.Width - ActualWidth) / 2 + SystemParameters.WorkArea.Left;
            Top = (SystemParameters.WorkArea.Height - ActualHeight) / 2 + SystemParameters.WorkArea.Top;
        }
    

    重要的是要注意网页没有“固有的”页面大小 - 即,它们可以用尽网络浏览器给出的任何空间 . 作为一种解决方法,我们使用我们的首选MINIMUM大小创建一个WebBrowser(在上面的示例中为420x240),然后在呈现文档之后,我们通过查看其ScrollRectangle来检查文档是否最终变大 .

    一些注意事项:我将窗口的大小限制在SystemParamters.WorkArea中,这样如果我们有一个非常大的文档,我们最终会得到一个比我们桌面大的窗口 . 我进一步将宽度和高度偏移了100px,以确保我们的窗口 Headers ,滚动条等也有空间 .

    另请注意,WindowStartupLocation =“CenterOwner”将无法工作,因为我们在启动后手动调整窗口大小 . 因此,我将WindowSTartupLocation更改为“Manual”,然后手动将窗口置于当前工作区域中心 .

    最后,一个人可能会通过使用一些Win32互操作调用来获取浏览器的滚动信息,从而使用这种技术来使用WPF的WebBrowser控件 . 但是,使用WinForms控件要容易得多 .

    我希望这有帮助!

  • 0

    对于WPF WebBrowser,我设置一个恒定的宽度,然后计算高度,所以:

    • SizeToContent 设置为 "Height" 并且窗口的宽度恒定 .

    • 添加对 Microsoft.mshtml 的引用

    • 在WebBrowser LoadCompleted 上使用以下代码:

    var webBrowser = (WebBrowser)sender;
    var doc = (IHTMLDocument2)webBrowser.Document;
    var height = ((IHTMLElement2)doc.body).scrollHeight;
    webBrowser.Height = Math.Min(height, (int)SystemParameters.WorkArea.Height - 100);
    

相关问题