[英]Resizing user control that is docked in Panel control does not resize Panel control - better solution?
[英]Resizing custom user control according to data in the webBrowser control docked in it
我有一個名為webBrowser1
的webBrowser
控件,它在自定義用戶控件上添加並停靠為DockStyle.Full
。 Web瀏覽器動態接受一些HTML文本並顯示它。 我禁用了webBrowser
控件的滾動條。 我的問題是,只要內容有點冗長, webBrowser
就會從下面隱藏它。 但是我的項目目標的要求是webBrowser不能顯示滾動條或它不應該隱藏一些內容。 必須完全顯示內容,而不滾動。 這意味着webBrowser
停靠的用戶控件必須根據webBrowser
的內容調整自身大小。 那么,任何人都可以建議我如何實現這一目標? 我搜索了整個互聯網,但沒有發現任何東西。
您可以通過WebBrowser.Document.Window.Size獲取HTML窗口的當前大小,並相應地調整容器控件的大小。 根據WebBrowser
控件內容如何接收動態更新,您可能需要在每次更新后執行此操作。 如果Document.Window.Size
沒有按預期方式增長,您還可以嘗試WebBrowser.Document.Body.ScrollRectangle
。
[已編輯]以下代碼適用於我(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標簽,以獲得正確的高度..我相信這可以通過RegEx或簡單算法刪除<和>之間的所有內容從一個字符串..你可能還需要為一些HTML標簽(IE列表)創建特殊的handlig
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.