问题
I have a WinForm that is used to host a WebBrowser control. I want to dynamically resize the form based on the document size that the browser loads.
I can successfully read the document size from within the WebBrowser control and I set the form size based on that, but the form simply will not resize.
The resize is within the WebBrowsers DocumentCompleted event:
private void ViewWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
ViewWebBrowser.Height = ViewWebBrowser.Document.Window.Size.Height;
ViewWebBrowser.Width = ViewWebBrowser.Document.Window.Size.Width;
Size = new Size(ViewWebBrowser.Width, ViewWebBrowser.Height);
}
This event fires just fine, the document loads and the document dimensions are detected as expected and they are correct based on the page I'm loading, but Size is always 37x38 coming out of the event handler. Here's a screenshot of the debugger at a breakpoint:
I also tried converting pixels to points, but this had the same result. Size was still 37x38.
private void ViewWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Graphics g = this.CreateGraphics();
g.PageUnit = GraphicsUnit.Pixel;
ViewWebBrowser.Height = Convert.ToInt32(ViewWebBrowser.Document.Window.Size.Height * 72 / g.DpiY);
ViewWebBrowser.Width = Convert.ToInt32(ViewWebBrowser.Document.Window.Size.Width * 72 / g.DpiX);
Size = new Size(ViewWebBrowser.Width, ViewWebBrowser.Height);
}
The WebBrowser control loads the document during the form's Activated
event:
private void WebBrowserView_Activated(object sender, EventArgs e)
{
ViewWebBrowser.Navigate(URL);
}
URL
is a public string property set by a presenter. The presenter does not set any size properties on the form.
AutoSize
on the form is set to false. The only properties on the form that I've changed from the default are Text
and FormBorderStyle
which is set to SizableToolWindow
.
In addition to a new Size
structure, I've also tried setting the Height
and Width
properties independently with the same result.
MinimumSize
and MaximumSize
are both set to 0,0. Setting MinimumSize
to 1,1 does not change anything.
DockStyle
on the WebBrowser control is set to Fill
so I'm only setting Size
on the form.
Why won't the form accept the new Size?
EDIT:
Here is the full class of the form:
public partial class WebBrowserView : Form, IWebBrowserView
{
public WebBrowserView()
{
InitializeComponent();
}
public string URL { private get; set; }
private void WebBrowserView_Activated(object sender, EventArgs e)
{
ViewWebBrowser.Navigate(URL);
}
private void ViewWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var newHeight = ViewWebBrowser.Document.Window.Size.Height;
var newWidth = ViewWebBrowser.Document.Window.Size.Width;
ViewWebBrowser.Height = newHeight;
ViewWebBrowser.Width = newWidth;
this.Size = new Size(ViewWebBrowser.Width, ViewWebBrowser.Height);
}
private void WebBrowserView_FormClosing(object sender, FormClosingEventArgs e)
{
ViewWebBrowser.Dispose();
}
}
回答1:
Judging by the code, everything should work as intended but there is something called pending layout requests
in WinForms
which requests an update for the layout. These changes are applied after invalidation of the UI so it is recommended to use SuspendLayout method before updating crucial layout/visual elements and then call ResumeLayout to apply these pending layout requests.
To apply these changes simply do :
void ViewWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
SuspendLayout();
ViewWebBrowser.Height = ViewWebBrowser.Document.Window.Size.Height;
ViewWebBrowser.Width = ViewWebBrowser.Document.Window.Size.Width;
Size = new Size(ViewWebBrowser.Width, ViewWebBrowser.Height);
ResumeLayout();
}
回答2:
Try using the ScrollRectangle property instead:
ViewWebBrowser.Height = ViewWebBrowser.Document.Body.ScrollRectangle.Height;
ViewWebBrowser.Width = ViewWebBrowser.Document.Body.ScrollRectangle.Width;
Per the comment thread, remove the Dock style:
ViewWebBrowser.Dock = DockStyle.None;
Using Fill will interfere with your height and width measurements.
来源:https://stackoverflow.com/questions/45984883/winform-wont-resize-when-setting-size-property