Resize dialog box / web browser to web page width.

Is there a way to automatically resize a window using a web browser control based on the initial page loaded?

Having not received good luck in the .Height or .Width properties, we also tried ActualWidth, ActualHeight.

Here is the 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>

I tried doing the following with no luck, WPF: how to adjust the size of the WebBrowser inside the popup?

Here is what I am currently getting ... (Doesn't resize correctly). enter image description here

+1
source share
2 answers

- WPF , . WinForms WebBrowser ( Noseratio), WPF WindowsFormsHost:

<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;
    }

, - "" , .. , -. WebBrowser (420x240 ), , , ScrollRectangle.

: SystemParamters.WorkArea, , , , . 100 , , ..

, WindowStartupLocation = "CenterOwner" , . WindowSTartupLocation "Manual", .

, WPF WebBrowser, Interactive Win32, . WinForms.

, !

+4

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);
    
0

Source: https://habr.com/ru/post/1662314/


All Articles