根据停靠在其中的 WebBrowser 控件中的数据调整自定义用户控件的大小

本文关键字:控件 自定义 调整 用户 数据 在其中 WebBrowser 停靠 | 更新日期: 2023-09-27 18:31:33

我有一个名为 webBrowser1webBrowser控件,它作为DockStyle.Full添加到自定义用户控件上并停靠。Web 浏览器动态接受一些 HTML 文本并显示它。我禁用了webBrowser控件的滚动条。我的问题是,每当内容有点长时,webBrowser就会从下面隐藏它。但是我的项目目标的要求是WebBrowser不能显示滚动条,或者它不应该隐藏某些内容。内容必须完全按原样显示,无需滚动。这意味着停靠webBrowser的用户控件必须根据webBrowser的内容调整自身大小。那么,任何人都可以建议我如何实现这一目标吗?我搜索了整个互联网和SO,但一无所获。

根据停靠在其中的 WebBrowser 控件中的数据调整自定义用户控件的大小

您可以通过 WebBrowser.Document.Window.Size 获取 HTML 窗口的当前大小,并相应地调整容器控件的大小。根据WebBrowser控件内容接收动态更新的方式,您可能需要在每次更新后执行此操作。您也可以尝试WebBrowser.Document.Body.ScrollRectangle如果Document.Window.Size没有以预期的方式增长。

[已编辑] 以下代码适用于我 (IE10):

private void Form1_Load(object sender, EventArgs e)
{
    this.BackColor = System.Drawing.Color.DarkGray;
    this.webBrowser.ScrollBarsEnabled = false;
    this.webBrowser.Dock = DockStyle.None;
    this.webBrowser.Location = new System.Drawing.Point(0, 0);
    this.webBrowser.Size = new System.Drawing.Size(320, 200);
    DownloadAsync("http://www.example.com").ContinueWith((task) =>
    {
        var html = task.Result;
        MessageBox.Show(String.Format(
            "WebBrowser.Size: {0}, Document.Window.Size: {1}, Document.Body.ScrollRectangle: {2}'n'n{3}",
            this.webBrowser.Size,
            this.webBrowser.Document.Window.Size,
            this.webBrowser.Document.Body.ScrollRectangle.Size,
            html));
        this.webBrowser.Size = this.webBrowser.Document.Body.ScrollRectangle.Size;
    }, TaskScheduler.FromCurrentSynchronizationContext());
}
async Task<string> DownloadAsync(string url)
{
    TaskCompletionSource<bool> onloadTcs = new TaskCompletionSource<bool>();
    WebBrowserDocumentCompletedEventHandler handler = null;
    handler = delegate
    {
        this.webBrowser.DocumentCompleted -= handler;
        // attach to subscribe to DOM onload event
        this.webBrowser.Document.Window.AttachEventHandler("onload", delegate
        {
            // each navigation has its own TaskCompletionSource
            if (onloadTcs.Task.IsCompleted)
                return; // this should not be happening
            // signal the completion of the page loading
            onloadTcs.SetResult(true);
        });
    };
    // register DocumentCompleted handler
    this.webBrowser.DocumentCompleted += handler;
    // Navigate to url
    this.webBrowser.Navigate(url);
    // continue upon onload
    await onloadTcs.Task;
    // the document has been fully loaded, can access DOM here
    // return the current HTML snapshot
    return ((dynamic)this.webBrowser.Document.DomDocument).documentElement.outerHTML.ToString();
}

若要调整用户控件的大小,首先需要获取内容所需的大小。这可以通过TextRender.MeasureText来实现,如下所示:

public static int GetContentHeight(string content, Control contentHolder, Font contentFont)
{     
    Font font = (contentFont != null) ? contentFont : contentHolder.Font;
    Size sz = new Size(contentHolder.Width, int.MaxValue);
    int padding = 3;
    int borders = contentHolder.Height - contentHolder.ClientSize.Height;
    TextFormatFlags flags = TextFormatFlags.WordBreak;
    sz = TextRenderer.MeasureText(content, contentHolder.Font, sz, flags);
    int cHeight = sz.Height + borders + padding;
    return cHeight;             
}

在您的情况下,这有点棘手,因为文本包含HTML标签,需要过滤掉,以获得正确的高度。我相信这可以通过正则表达式或一个简单的算法来实现,从字符串中删除<和>之间的所有内容。您可能还需要为某些HTML标签(即列表)创建特殊的handlig