C# WebBrowser 控件调整大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1155603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:33:12  来源:igfitidea点击:

WebBrowser control resize

c#.netwinformsresizewebbrowser-control

提问by willseward

I have a WebBrowser in my form that accesses a website that is too wide for the control. I would like to know if there is a way to resize the control to fit the website. It seems simple, but i have not found a way yet. I know how to resize the control, I just need to find the width of the website. Just like when you press the green button in safari it automatically resized the window to the correct size.

我的表单中有一个 WebBrowser,它可以访问对控件来说太宽的网站。我想知道是否有办法调整控件的大小以适合网站。看起来很简单,但我还没有找到方法。我知道如何调整控件的大小,我只需要找到网站的宽度。就像在 safari 中按下绿色按钮一样,它会自动将窗口大小调整为正确的大小。

采纳答案by Jeff

You should be able to access the size of the WebBrowser's Document and get it's size through the WebBrowser.Document.Window.Size.

您应该能够访问 WebBrowser 文档的大小并通过 WebBrowser.Document.Window.Size 获取它的大小。

I would use the WebBrowser Controls's DocumentCompleted event to wait until the page is loaded then use your resize code to resize the webControl container with this size.

我将使用 WebBrowser Controls 的 DocumentCompleted 事件等待页面加载,然后使用调整大小代码调整具有此大小的 webControl 容器的大小。

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        WebBrowser1.Size = WebBrowser1.Document.Body.ScrollRectangle.Size
    End Sub

回答by John Saunders

  • Step one would be to see if you can resize the control at all.
  • Step two would be to see if you can find the size that the web site would like
  • Step three would be to take that size, or the max screen size, whichever is less, minus any chrome, and resize the control to that size.
  • 第一步是查看您是否可以调整控件的大小。
  • 第二步是看看你是否能找到网站想要的尺寸
  • 第三步是采用该尺寸或最大屏幕尺寸(以较小者为准)减去任何镶边,并将控件调整为该尺寸。

回答by Jared Updike

Your best bet might be to just make the WebBrowser use as much space (Dock.Fill) as possible on the form from the get-go, so if the user full-screen maximizes the app they get plenty of space, and if they want it smaller for smaller sites they can resize your app however they want. This is how all browsers work.

您最好的选择可能是让 WebBrowser 从一开始就在表单上使用尽可能多的空间 (Dock.Fill),因此如果用户全屏最大化应用程序,他们将获得足够的空间,如果他们想要对于较小的网站,它更小,他们可以根据需要调整您的应用程序的大小。这就是所有浏览器的工作方式。

回答by ufologist

var webBrowser = new WebBrowser();
// the magic!!!
webBrowser.Dock = DockStyle.Fill;
form.get_Controls().Add(webBrowser);
webBrowser.Navigate('http://a.com/');

回答by ulatekh

Answered here: use WebBrowser.Document.Body.ScrollRectangle.Sizeinstead of WebBrowser.Document.Window.Size. Working like a charm for me.

在这里回答:使用WebBrowser.Document.Body.ScrollRectangle.Size代替WebBrowser.Document.Window.Size。工作对我来说就像一种魅力。