简体   繁体   English

在 WebBrowser 控件中适合 html 页面宽度

[英]Fit html page width in WebBrowser control

I have a lot of local html pages which I open in a WebBrowser control in my app.我有很多本地 html 页面,我在我的应用程序的 WebBrowser 控件中打开这些页面。
I would like the page to initially open to fit the width of the control and ideally to change its width to fit the control after a user resize.我希望页面最初打开以适合控件的宽度,理想情况下在用户调整大小后更改其宽度以适合控件。
As there are a lot of pages, I would prefer a c#, js or css solution which doesn't require modifying the html files.由于页面很多,我更喜欢c#,js或css解决方案,不需要修改html文件。
I searched high and low, tried tens of solutions (many of them here on SO) but nothing seems to work.我到处搜索,尝试了数十种解决方案(其中很多都在 SO 上),但似乎没有任何效果。
Win 7, IE 11, VS 2022赢 7、IE 11、VS 2022
Thanks in advance提前致谢

Since you're using Win7 and IE11, I have a hunch you're using WinForms.由于您使用的是 Win7 和 IE11,我有预感您正在使用 WinForms。 The DOM can be accessed through the WebBrowser control's Document property where we can add the CSS. Put this in your WebBrowser.DocumentCompleted event handler:可以通过 WebBrowser 控件的 Document 属性访问 DOM,我们可以在其中添加 CSS。将其放入您的WebBrowser.DocumentCompleted事件处理程序中:

webBrowser1.Document.Body.Style += ";width:100%;";

The Style property is just like an HTML style= attribute, that is, a string of CSS styles separated by semicolons. Style 属性就像一个 HTML style=属性,即由分号分隔的 CSS styles 的字符串。 The appended string starts with a semicolon in case the existing styles don't end with one.附加的字符串以分号开头,以防现有的 styles 不以分号结尾。

For WPF, first see Accessing DOM from WebBrowser .对于 WPF,首先请参阅从 WebBrowser 访问 DOM Then you can put this in your WebBrowser.LoadCompleted event handler:然后你可以把它放在你的WebBrowser.LoadCompleted事件处理程序中:

var document = webBrowser1.Document as mshtml.HTMLDocument;
document.body.style.width = '100%';

There are a few ways you can achieve this without modifying the HTML pages themselves.有几种方法可以在不修改 HTML 页面本身的情况下实现这一点。

Using JavaScript: You can add a JavaScript function to the WebBrowser control's DocumentCompleted event to resize the HTML page to fit the width of the control.使用 JavaScript:您可以将 JavaScript function 添加到 WebBrowser 控件的 DocumentCompleted 事件,以调整 HTML 页面的大小以适应控件的宽度。 Here's an example of how you can do this:以下是如何执行此操作的示例:

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    webBrowser1.Document.Body.Style = "zoom:100%";
}

The above code sets the zoom level of the body of the HTML page to 100%, which will make it fit the width of the control.上面的代码将 HTML 页面的主体的缩放级别设置为 100%,这将使其适合控件的宽度。 You can also use the "width" and "height" properties of the body style to set the width and height of the page to the size of the control.您还可以使用主体样式的“宽度”和“高度”属性将页面的宽度和高度设置为控件的大小。

Using Css: You can use css to change the width of the html pages in your WebBrowser control.使用 Css:您可以使用 css 来更改 WebBrowser 控件中 html 页面的宽度。 You can create a css file and include it in your html file.您可以创建一个 css 文件并将其包含在您的 html 文件中。

    body {
  width: 100%;
}

This will set the width of the body to 100% of the parent container, which in this case is the WebBrowser control.这会将主体的宽度设置为父容器的 100%,在本例中为 WebBrowser 控件。

Using C#: You can use the WebBrowser control's Resize event to change the size of the HTML page to fit the control.使用 C#:您可以使用 WebBrowser 控件的 Resize 事件来更改 HTML 页面的大小以适应控件。 Here's an example of how you can do this:以下是如何执行此操作的示例:

    private void webBrowser1_Resize(object sender, EventArgs e)
{
    webBrowser1.Width = this.Width;
    webBrowser1.Height = this.Height;
}

This code sets the width and height of the WebBrowser control to the width and height of the parent form/container.此代码将 WebBrowser 控件的宽度和高度设置为父窗体/容器的宽度和高度。

It's worth noting that solution 1 and 3 may cause issues with the layout of the HTML page, as the page may not be designed to automatically adjust its size.值得注意的是,解决方案 1 和 3 可能会导致 HTML 页面的布局出现问题,因为该页面可能未设计为自动调整其大小。 Solution 2 is the best approach if you want to change the size of the HTML pages in a cleaner way, as it's less prone to errors.如果您想以更简洁的方式更改 HTML 页面的大小,则解决方案 2 是最佳方法,因为它不太容易出错。

It's also worth noting that you can combine the above solutions for better results.还值得注意的是,您可以结合上述解决方案以获得更好的结果。 You can use solution 2 to set the width of the HTML pages to 100% and then use solution 3 to adjust the size of the WebBrowser control to fit the parent form/container.您可以使用解决方案 2 将 HTML 页面的宽度设置为 100%,然后使用解决方案 3 调整 WebBrowser 控件的大小以适应父窗体/容器。

I hope this helps.我希望这有帮助。 Let me know if you have any further questions.如果您还有其他问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM