设置大小属性时,Winform 不会调整大小 [英] Winform won't resize when setting Size property

查看:50
本文介绍了设置大小属性时,Winform 不会调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于托管 WebBrowser 控件的 WinForm.我想根据浏览器加载的文档大小动态调整表单大小.

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.

我可以从 WebBrowser 控件中成功读取文档大小,并基于此设置表单大小,但表单根本不会调整大小.

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.

调整大小在 WebBrowsers DocumentCompleted 事件中:

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

此事件触发得很好,按预期检测文档加载和文档尺寸,并且根据我正在加载的页面它们是正确的,但事件处理程序中的大小始终为 37x38.这是调试器在断点处的屏幕截图:

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:

我也尝试将像素转换为点,但结果相同.尺寸仍然是 37x38.

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

WebBrowser 控件在表单的 Activated 事件期间加载文档:

The WebBrowser control loads the document during the form's Activated event:

private void WebBrowserView_Activated(object sender, EventArgs e)
{
    ViewWebBrowser.Navigate(URL); 
}

URL 是演示者设置的公共字符串属性.演示者没有在表单上设置任何大小属性.

URL is a public string property set by a presenter. The presenter does not set any size properties on the form.

AutoSize 设置为 false.我从默认设置更改的表单上唯一的属性是 TextFormBorderStyle,它们设置为 SizableToolWindow.

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.

除了新的 Size 结构之外,我还尝试独立设置 HeightWidth 属性,结果相同.

In addition to a new Size structure, I've also tried setting the Height and Width properties independently with the same result.

MinimumSizeMaximumSize 都设置为 0,0.将 MinimumSize 设置为 1,1 不会改变任何东西.

MinimumSize and MaximumSize are both set to 0,0. Setting MinimumSize to 1,1 does not change anything.

DockStyle 在 WebBrowser 控件上设置为 Fill 所以我只在表单上设置 Size.

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?

这是表单的完整类:

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

推荐答案

从代码来看,一切都应该按预期工作,但在 WinFormspending layout requests 的东西> 请求更新布局.这些更改在 UI 失效后应用,因此建议使用 SuspendLayout 方法,然后更新关键布局/视觉元素,然后调用 ResumeLayout 应用这些待处理的布局请求.

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

这篇关于设置大小属性时,Winform 不会调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆