问题描述
找到了这个示例网络浏览器代码
这确实创建了一个 Windows 窗体应用程序和一个将 bing.com 作为地址打开的网络浏览器。我不知道他们从哪里获得 bing.com。它在任何地方都没有硬编码。我想尝试从这段代码开始创建 bing.com 以外的网络浏览器。
using System;
using System.Windows.Forms;
using System.Security.Permissions;
[PermissionSet(SecurityAction.Demand,Name = "FullTrust")]
public class Form1 : Form
{
public Form1()
{
// Create the form layout. If you are using Visual Studio,// you can replace this code with code generated by the designer.
Initializeform();
// The following events are not visible in the designer,so
// you must associate them with their event-handlers in code.
webbrowser1.CanGoBackChanged +=
new EventHandler(webbrowser1_CanGoBackChanged);
webbrowser1.CanGoForwardChanged +=
new EventHandler(webbrowser1_CanGoForwardChanged);
webbrowser1.DocumentTitleChanged +=
new EventHandler(webbrowser1_DocumentTitleChanged);
webbrowser1.StatusTextChanged +=
new EventHandler(webbrowser1_StatusTextChanged);
// Load the user's home page.
webbrowser1.GoHome();
}
// displays the Save dialog Box.
private void saveAsToolStripMenuItem_Click(object sender,EventArgs e)
{
webbrowser1.ShowSaveAsDialog();
}
// displays the Page Setup dialog Box.
private void pageSetupToolStripMenuItem_Click(object sender,EventArgs e)
{
webbrowser1.ShowPageSetupDialog();
}
// displays the Print dialog Box.
private void printToolStripMenuItem_Click(object sender,EventArgs e)
{
webbrowser1.ShowPrintDialog();
}
// displays the Print Preview dialog Box.
private void printPreviewToolStripMenuItem_Click(
object sender,EventArgs e)
{
webbrowser1.ShowPrintPreviewDialog();
}
// displays the Properties dialog Box.
private void propertiesToolStripMenuItem_Click(
object sender,EventArgs e)
{
webbrowser1.ShowPropertiesDialog();
}
// Selects all the text in the text Box when the user clicks it.
private void toolStripTextBox1_Click(object sender,EventArgs e)
{
toolStripTextBox1.SelectAll();
}
// Navigates to the URL in the address Box when
// the ENTER key is pressed while the ToolStripTextBox has focus.
private void toolStripTextBox1_KeyDown(object sender,KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Navigate(toolStripTextBox1.Text);
}
}
// Navigates to the URL in the address Box when
// the Go button is clicked.
private void goButton_Click(object sender,EventArgs e)
{
Navigate(toolStripTextBox1.Text);
}
// Navigates to the given URL if it is valid.
private void Navigate(String address)
{
if (String.IsNullOrEmpty(address)) return;
if (address.Equals("about:blank")) return;
if (!address.StartsWith("http://") &&
!address.StartsWith("https://"))
{
address = "http://" + address;
}
try
{
webbrowser1.Navigate(new Uri(address));
}
catch (System.UriFormatException)
{
return;
}
}
// Updates the URL in TextBoxAddress upon navigation.
private void webbrowser1_Navigated(object sender,WebbrowserNavigatedEventArgs e)
{
toolStripTextBox1.Text = webbrowser1.Url.ToString();
}
// Navigates webbrowser1 to the prevIoUs page in the history.
private void backButton_Click(object sender,EventArgs e)
{
webbrowser1.GoBack();
}
// disables the Back button at the beginning of the navigation history.
private void webbrowser1_CanGoBackChanged(object sender,EventArgs e)
{
backButton.Enabled = webbrowser1.CanGoBack;
}
// Navigates webbrowser1 to the next page in history.
private void forwardButton_Click(object sender,EventArgs e)
{
webbrowser1.GoForward();
}
// disables the Forward button at the end of navigation history.
private void webbrowser1_CanGoForwardChanged(object sender,EventArgs e)
{
forwardButton.Enabled = webbrowser1.CanGoForward;
}
// Halts the current navigation and any sounds or animations on
// the page.
private void stopButton_Click(object sender,EventArgs e)
{
webbrowser1.Stop();
}
// Reloads the current page.
private void refreshButton_Click(object sender,EventArgs e)
{
// Skip refresh if about:blank is loaded to avoid removing
// content specified by the DocumentText property.
if (!webbrowser1.Url.Equals("about:blank"))
{
webbrowser1.Refresh();
}
}
// Navigates webbrowser1 to the home page of the current user.
private void homeButton_Click(object sender,EventArgs e)
{
webbrowser1.GoHome();
}
// Navigates webbrowser1 to the search page of the current user.
private void searchButton_Click(object sender,EventArgs e)
{
webbrowser1.GoSearch();
}
// Prints the current document using the current print settings.
private void printButton_Click(object sender,EventArgs e)
{
webbrowser1.Print();
}
// Updates the status bar with the current browser status text.
private void webbrowser1_StatusTextChanged(object sender,EventArgs e)
{
toolStripStatusLabel1.Text = webbrowser1.StatusText;
}
// Updates the title bar with the current document title.
private void webbrowser1_DocumentTitleChanged(object sender,EventArgs e)
{
this.Text = webbrowser1.DocumentTitle;
}
// Exits the application.
private void exitToolStripMenuItem_Click(object sender,EventArgs e)
{
Application.Exit();
}
// The remaining code in this file provides basic form initialization and
// includes a Main method. If you use the Visual Studio designer to create
// your form,you can use the designer generated code instead of this code,// but be sure to use the names shown in the variable declarations here,// and be sure to attach the event handlers to the associated events.
private Webbrowser webbrowser1;
private MenuStrip menuStrip1;
private ToolStripMenuItem filetoolStripMenuItem,saveAsToolStripMenuItem,printToolStripMenuItem,printPreviewToolStripMenuItem,exitToolStripMenuItem,pageSetupToolStripMenuItem,propertiesToolStripMenuItem;
private ToolStripSeparator toolStripSeparator1,toolStripSeparator2;
private ToolStrip toolStrip1,toolStrip2;
private ToolStripTextBox toolStripTextBox1;
private ToolStripButton goButton,backButton,forwardButton,stopButton,refreshButton,homeButton,searchButton,printButton;
private Statusstrip statusstrip1;
private ToolStripStatusLabel toolStripStatusLabel1;
private void Initializeform()
{
webbrowser1 = new Webbrowser();
menuStrip1 = new MenuStrip();
filetoolStripMenuItem = new ToolStripMenuItem();
saveAsToolStripMenuItem = new ToolStripMenuItem();
toolStripSeparator1 = new ToolStripSeparator();
printToolStripMenuItem = new ToolStripMenuItem();
printPreviewToolStripMenuItem = new ToolStripMenuItem();
toolStripSeparator2 = new ToolStripSeparator();
exitToolStripMenuItem = new ToolStripMenuItem();
pageSetupToolStripMenuItem = new ToolStripMenuItem();
propertiesToolStripMenuItem = new ToolStripMenuItem();
toolStrip1 = new ToolStrip();
goButton = new ToolStripButton();
backButton = new ToolStripButton();
forwardButton = new ToolStripButton();
stopButton = new ToolStripButton();
refreshButton = new ToolStripButton();
homeButton = new ToolStripButton();
searchButton = new ToolStripButton();
printButton = new ToolStripButton();
toolStrip2 = new ToolStrip();
toolStripTextBox1 = new ToolStripTextBox();
statusstrip1 = new Statusstrip();
toolStripStatusLabel1 = new ToolStripStatusLabel();
menuStrip1.Items.Add(filetoolStripMenuItem);
filetoolStripMenuItem.DropDownItems.AddRange(
new ToolStripItem[] {
saveAsToolStripMenuItem,toolStripSeparator1,toolStripSeparator2,propertiesToolStripMenuItem,exitToolStripMenuItem
});
filetoolStripMenuItem.Text = "&File";
saveAsToolStripMenuItem.Text = "Save &As...";
pageSetupToolStripMenuItem.Text = "Page Set&up...";
printToolStripMenuItem.Text = "&Print...";
printPreviewToolStripMenuItem.Text = "Print Pre&view...";
propertiesToolStripMenuItem.Text = "Properties";
exitToolStripMenuItem.Text = "E&xit";
printToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.P;
saveAsToolStripMenuItem.Click +=
new System.EventHandler(saveAsToolStripMenuItem_Click);
pageSetupToolStripMenuItem.Click +=
new System.EventHandler(pageSetupToolStripMenuItem_Click);
printToolStripMenuItem.Click +=
new System.EventHandler(printToolStripMenuItem_Click);
printPreviewToolStripMenuItem.Click +=
new System.EventHandler(printPreviewToolStripMenuItem_Click);
propertiesToolStripMenuItem.Click +=
new System.EventHandler(propertiesToolStripMenuItem_Click);
exitToolStripMenuItem.Click +=
new System.EventHandler(exitToolStripMenuItem_Click);
toolStrip1.Items.AddRange(new ToolStripItem[] {
goButton,printButton});
goButton.Text = "Go";
backButton.Text = "Back";
forwardButton.Text = "Forward";
stopButton.Text = "Stop";
refreshButton.Text = "Refresh";
homeButton.Text = "Home";
searchButton.Text = "Search";
printButton.Text = "Print";
backButton.Enabled = false;
forwardButton.Enabled = false;
goButton.Click += new System.EventHandler(goButton_Click);
backButton.Click += new System.EventHandler(backButton_Click);
forwardButton.Click += new System.EventHandler(forwardButton_Click);
stopButton.Click += new System.EventHandler(stopButton_Click);
refreshButton.Click += new System.EventHandler(refreshButton_Click);
homeButton.Click += new System.EventHandler(homeButton_Click);
searchButton.Click += new System.EventHandler(searchButton_Click);
printButton.Click += new System.EventHandler(printButton_Click);
toolStrip2.Items.Add(toolStripTextBox1);
toolStripTextBox1.Size = new System.Drawing.Size(250,25);
toolStripTextBox1.KeyDown +=
new KeyEventHandler(toolStripTextBox1_KeyDown);
toolStripTextBox1.Click +=
new System.EventHandler(toolStripTextBox1_Click);
statusstrip1.Items.Add(toolStripStatusLabel1);
webbrowser1.Dock = DockStyle.Fill;
webbrowser1.Navigated +=
new WebbrowserNavigatedEventHandler(webbrowser1_Navigated);
Controls.AddRange(new Control[] {
webbrowser1,toolStrip2,toolStrip1,menuStrip1,statusstrip1,menuStrip1 });
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
谢谢,
解决方法
webBrowser1.GoHome();
最初被调用以打开用户主页,如果没有其他设置,默认情况下该主页将是 Bing。
将其替换为调用 Navigate()
方法并传递您选择的 URL。
GoHome() 导航到 bing.com。 WebBrowser 使用 IE/Edge 设置,显然您已将 bing.com 设置为主页。设置 url 而不是导航到默认页面
myprog_datadir=~/tmp