gpt4 book ai didi

c# - 将 Dialog/WebBrowser 调整为网页宽度

转载 作者:太空宇宙 更新时间:2023-11-03 21:37:05 25 4
gpt4 key购买 nike

有没有一种方法可以根据加载的初始网页使用网络浏览器控件自动调整窗口大小?

对 .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

最佳答案

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

请务必注意,网页没有“固有”页面大小 - 即,它们可以用完网络浏览器提供的任何空间。作为解决方法,我们创建了一个 WebBrowser,其大小为我们的首选最小值(上例中为 420x240),然后在呈现文档后,我们通过查看其 ScrollRectangle 来检查文档最终是否大于该值。

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

另请注意,WindowStartupLocation="CenterOwner"将不起作用,因为我们在启动后手动调整窗口大小。因此,我将 WindowSTartupLocation 更改为“Manual”,然后在当前工作区内手动将窗口居中。

最后,通过使用一些 Win32 互操作调用来获取浏览器的滚动信息,人们可能会使用此技术来处理 WPF 的 WebBrowser 控件。不过,仅使用 WinForms 控件要容易得多。

希望对您有所帮助!

关于c# - 将 Dialog/WebBrowser 调整为网页宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21170316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com