?? ch9_demoform004.cs
字號:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using CH9.Properties;
using System.IO;
namespace CH9
{
public partial class CH9_DemoForm004 : Form
{
public CH9_DemoForm004()
{
InitializeComponent();
}
private const int myBUFFER = 20;
private XmlDocument xmldoc = new XmlDocument();
private void CH5_DemoForm004_Load(object sender, EventArgs e)
{
// 呼叫 GoHome 方法來導覽至首頁。
this.webBrowser1.GoHome();
// 更新「上一頁」與「下一頁」按鈕。
UpdateButtons();
this.InitializeOpenFileDialog();
// 呼叫 AddToolStripButtons() 程序以便加載 XML 檔案中的網站清單。
AddToolStripButtons();
}
private void backButton_ButtonDoubleClick(object sender, EventArgs e)
{
this.webBrowser1.GoBack();
}
private void forwardButton_ButtonClick(object sender, EventArgs e)
{
this.webBrowser1.GoForward();
}
private void stopButton_Click(object sender, EventArgs e)
{
this.webBrowser1.Stop();
}
private void refreshButton_Click(object sender, EventArgs e)
{
this.webBrowser1.Refresh();
}
private void homeButton_Click(object sender, EventArgs e)
{
this.webBrowser1.GoHome();
}
private void searchButton_Click(object sender, EventArgs e)
{
this.webBrowser1.GoSearch();
}
// 「移至」按鈕的Click事件處理例程。
private void tsbGo_Click(object sender, EventArgs e)
{
this.NavigateToURL();
}
private void urlTextBox_KeyDown(object sender, KeyEventArgs e)
{
// 按下 Enter 鍵便會導覽至所鍵入的 URL。
if (e.KeyCode == Keys.Enter)
{
this.NavigateToURL();
}
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
// 更新「上一頁」與「下一頁」按鈕。
UpdateButtons();
this.tsWebProgressBar.Visible = true;
}
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
// 更新「上一頁」與「下一頁」按鈕。
UpdateButtons();
// 更新 URL 文字方塊。
this.urlTextBox.Text = this.webBrowser1.Url.ToString();
// 更新文字。
this.statusText.Text = "正在打開網頁 " + this.webBrowser1.Url.ToString();
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.statusText.Text = "完成";
this.UpLevelButton.Enabled = canGoUp(formatPath(this.urlTextBox.Text));
this.tsWebProgressBar.Visible = false;
this.tsWebProgressBar.Value = 0;
}
private void NavigateToURL()
{
try
{
this.webBrowser1.Navigate(formatPath(this.urlTextBox.Text));
}
catch
{
MessageBox.Show("無效的 URL。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private string formatPath(string myPath)
{
string trimmed = myPath.Trim();
if (trimmed.StartsWith("file:"))
{
trimmed = trimmed.Replace("file:///", "");
trimmed = trimmed.Replace("file://", "\\");
trimmed = trimmed.Replace("/", @"\");
}
return trimmed;
}
private void UpLevelButton_Click(object sender, EventArgs e)
{
string myPath = formatPath(this.webBrowser1.Url.ToString());
if (canGoUp(myPath))
{
myPath = myPath.Substring(0, myPath.LastIndexOf(@"\"));
try
{
this.webBrowser1.Navigate(myPath);
}
catch
{
UpLevelButton.Enabled = false;
}
}
}
private bool canGoUp(string myPath)
{
myPath = myPath.TrimStart(@"\".ToCharArray());
myPath += @"\";
return (!(myPath.IndexOf(@"\") == myPath.LastIndexOf(@"\")) && !((myPath.IndexOf(@"\") + 1) == myPath.LastIndexOf(@"\")));
}
private void UpdateButtons()
{
// 更新「上一頁」與「下一頁」按鈕。
this.backButton.Enabled = this.webBrowser1.CanGoBack;
this.forwardButton.Enabled = this.webBrowser1.CanGoForward;
}
// 本程序用來設定「開啟檔案」對話框的各項屬性。
private void InitializeOpenFileDialog()
{
// 設定「開啟檔案」對話框的初始目錄。
OpenFileDialog1.InitialDirectory = Environment.CurrentDirectory + @"\HTML";
// 設定文件名篩選字符串以方便選取特定格式的檔案。
OpenFileDialog1.Filter =
"網頁 (*.HTM;*.HTML)|*.HTM;*.HTML|XML 文件(*.XML)|*.XML|Flash 文件(*.SWF)|*.SWF|Office 文件(*.DOC;*.XLS;*.PPT)|*.DOC;*.XLS;*.PPT";
// 設定「開啟檔案」對話框的標題。
OpenFileDialog1.Title = "請選取特定格式文件";
// 設定「開啟檔案」對話框在關閉前還原目前的目錄。
OpenFileDialog1.RestoreDirectory = true;
}
private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
this.tsWebProgressBar.Value = (int)(((e.CurrentProgress / e.MaximumProgress) * 100));
}
private void BrowserToolStrip_Resize(object sender, EventArgs e)
{
maximizeToolStripItem(this.BrowserToolStrip, this.urlTextBox, myBUFFER);
}
private void maximizeToolStripItem(ToolStrip parentToolStrip, ToolStripItem maximizedItem, int myBuffer)
{
int width = parentToolStrip.Width - (parentToolStrip.Padding.Horizontal + myBuffer);
foreach (ToolStripItem item in parentToolStrip.Items)
{
if (!(item.Equals(maximizedItem)))
{
width -= item.Width;
}
width -= item.Margin.Horizontal;
}
maximizedItem.Width = width;
}
private void printButton_Click(object sender, EventArgs e)
{
this.webBrowser1.Print();
}
#region "自定義工具欄程序代碼區塊"
private void AddToolStripButtons()
{
DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
Settings mySetting = new Settings();
// 加載內含網站清單的 XML 檔案。
xmldoc.Load(di.ToString() + @"\" + mySetting.XmlFileName);
// 清除 ToolStrip2 此一工具列所內含的所有項目。
ToolStrip2.Items.Clear();
// 循環處理 XML 檔案并將每一個網站信息加載成一個 ToolStripButton 控件。
XmlNodeList websiteNodes;
websiteNodes = xmldoc.GetElementsByTagName("WebSite");
int iCount = 1;
foreach (XmlNode webSiteNode in websiteNodes)
{
XmlNodeList eachSiteNodes;
eachSiteNodes = webSiteNode.ChildNodes;
foreach (XmlNode eachSiteNode in eachSiteNodes)
{
if (eachSiteNode.Name == "Name")
{
// 呼叫 AddTSButton 程序以便替每一個網站定義建立一個 ToolStripButton 控件。
AddTSButton("tsWebsite" + iCount.ToString(), eachSiteNode.InnerText);
iCount += 1;
}
}
}
}
private void AddTSButton(string buttonName, string webSiteName)
{
// 建立一個 ToolStripButton 對象。
System.Windows.Forms.ToolStripButton newbutton = new System.Windows.Forms.ToolStripButton();
// 設定 ToolStripButton 對象的各個屬性。
newbutton.Name = buttonName;
newbutton.Text = webSiteName;
newbutton.Image = Resources.stspeopl;
newbutton.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
// 將 ToolStripButton 對象加入至 ToolStrip2 工具列中。
ToolStrip2.Items.Add(newbutton);
// 設定當此發此 ToolStripButton 對象的 Click 時就執行事件處理例程 tsbutton_Click。
newbutton.Click += new System.EventHandler(tsbutton_Click);
}
private void tsbutton_Click(object sender, System.EventArgs e)
{
ToolStripButton aButton = (ToolStripButton)(sender);
GoToUrl(aButton.Text);
}
private void GoToUrl(string sWebsite)
{
string xPath = @"//WebSite[Name='" + sWebsite + "']/URL";
XmlNode xn = xmldoc.SelectSingleNode(xPath);
webBrowser1.Url = new Uri(xn.InnerText);
}
private void tsbManage_Click(object sender, EventArgs e)
{
Sites frmManage = new Sites();
frmManage.ShowDialog();
AddToolStripButtons();
}
private void tsbMsTaiwan_Click(object sender, EventArgs e)
{
webBrowser1.Url = new Uri("http://www.microsoft.com/china");
}
private void tsbMSDNCHT_Click(object sender, EventArgs e)
{
webBrowser1.Url = new Uri("http://www.microsoft.com/china/msdn/");
}
private void tsbCommunity_Click(object sender, EventArgs e)
{
webBrowser1.Url = new Uri("http://www.microsoft.com/china/community/");
}
#endregion
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -