![](/img/trans.png)
[英]How to get scroll width from web browser control in wpf/windows c#
[英]How to set scroll in web browser control in C#
我想在網頁瀏覽器控件中設置網頁的位置。 應用程序的大小小於網頁。 我想將其設置在中間。
這將帶您到那里
請注意,主要形式設置為AutoSize = true
和AutoSizeMode = GrowAndShrink
將html
文檔導航到時,將進行檢查以查看是否存在任何滾動條。 如果任何一個滾動條目前,尺寸webbrowser
設置為滾動矩形,即Web瀏覽器將發展壯大,以顯示該網頁所需的最小尺寸。 由於主要形式是AutoSize
,它將調整大小以適合webbrowser
Form1.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
//comment out one of these for testing
webBrowser1.Navigate(@"C:\foo\minimumWebPage.htm");
//webBrowser1.Navigate(@"C:\foo\bigWebPage.htm");
}
private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (IsHorizontalScrollbarPresent() || IsVerticalScrollbarPresent())
{
webBrowser1.Size = new Size(webBrowser1.Document.Body.ScrollRectangle.Width, webBrowser1.Document.Body.ScrollRectangle.Height);
}
}
public bool IsHorizontalScrollbarPresent()
{
return webBrowser1.Document.Body.ScrollRectangle.Width > webBrowser1.Document.Window.Size.Width;
}
public bool IsVerticalScrollbarPresent()
{
return webBrowser1.Document.Body.ScrollRectangle.Height > webBrowser1.Document.Window.Size.Height;
}
}
}
Form1.Designer.cs
namespace WindowsFormsApp1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Go";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.Button1_Click);
//
// webBrowser1
//
this.webBrowser1.Location = new System.Drawing.Point(11, 66);
this.webBrowser1.Margin = new System.Windows.Forms.Padding(10);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(330, 138);
this.webBrowser1.TabIndex = 1;
this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.WebBrowser1_DocumentCompleted);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.ClientSize = new System.Drawing.Size(475, 222);
this.Controls.Add(this.webBrowser1);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "Form1";
this.Text = "Show Get-NetConnectionProfile Output";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.WebBrowser webBrowser1;
}
}
**最小HTML文件的輸出
**大HTML文件的輸出
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.