[英]How to set scroll in web browser control in C#
I want to set the position of the webpage in a web browser control. 我想在网页浏览器控件中设置网页的位置。 the application size is smaller than the webpage.
应用程序的大小小于网页。 I want to set it in the middle of it.
我想将其设置在中间。
This gets you most of the way there 这将带您到那里
Note that the main form is set AutoSize = true
and AutoSizeMode = GrowAndShrink
请注意,主要形式设置为
AutoSize = true
和AutoSizeMode = GrowAndShrink
When the html
document is navigated to, a check is made is to see if either scrollbar is present. 将
html
文档导航到时,将进行检查以查看是否存在任何滚动条。 If either scrollbar is present, the size of the webbrowser
is set to the scroll rectangle, ie, the web browser will grow to the minimum size needed to display the webpage. 如果任何一个滚动条目前,尺寸
webbrowser
设置为滚动矩形,即Web浏览器将发展壮大,以显示该网页所需的最小尺寸。 Since main form is AutoSize
, it will resize to fit the webbrowser
由于主要形式是
AutoSize
,它将调整大小以适合webbrowser
Form1.cs 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 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;
}
}
**Output for minimum html file **最小HTML文件的输出
**Output for big html file **大HTML文件的输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.