根据停靠在它WebBrowser控件数据调整自定义用户控件 [英] Resizing custom user control according to data in the webBrowser control docked in it

查看:130
本文介绍了根据停靠在它WebBrowser控件数据调整自定义用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 web浏览器控制 webBrowser1 被添加和停靠如 DockStyle。全在自定义用户控件。网络浏览器动态地接受一些HTML文本并显示。我禁用了 web浏览器控制滚动条。我的问题是,每当内容是有些冗长,在 web浏览器从下面隐藏它。但我的项目目标的要求是,web浏览器不能显示任何滚动条或者不应该隐藏的一些内容。内容必须完全显示为它无需滚动。这意味着在其上 web浏览器停靠必须根据 web浏览器的内容调整自己的用户控件。因此,任何人都可以请建议我如何实现这一目标?我找遍了互联网和SO但一无所获。

I have a webBrowser control named webBrowser1 that is added and docked as DockStyle.Full on a custom user control. The web-browser accepts some HTML text dynamically and displays it. I disabled the scroll bars of the webBrowser control. My problem is that whenever the content is somewhat lengthy, the webBrowser hides it from below. But the requirement of my project objective is that the webBrowser must not show either scroll bars or it should not hide some of the content. The content must be completely shown as it is without scrolling. That means the user control on which the webBrowser is docked must resize itself according to webBrowser's content. So, can anyone please suggest me how to achieve this? I searched all over the internet and SO but found nothing.

推荐答案

您可以通过的 WebBrowser.Document.Window.Size 并调整因此容器控件。根据你的 web浏览器控制内容是如何接收动态更新,你可能需要在每次更新后做到这一点。您也可以尝试 WebBrowser.Document.Body.ScrollRectangle 如果 Document.Window.Size 在预期不会增长。方法

You can get the current size of HTML window via WebBrowser.Document.Window.Size and resize the container control accordingly. Depending on how your WebBrowser control content receives dynamic updates, you'd probably need to do this after each update. You could also try WebBrowser.Document.Body.ScrollRectangle if Document.Window.Size doesn't grow in the expected way.

下面的代码对我的作品(IE10):

The following code works for me (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();
}

这篇关于根据停靠在它WebBrowser控件数据调整自定义用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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