using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using HtmlDom;
namespace HTMLElementSelect
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
AboutForm afrm=new AboutForm();
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.cnblogs.com/JiangHuakey/");
webBrowser1.ContextMenuStrip = contextMenuStrip1; //! 設置我們自己的彈出菜單
webBrowser1.IsWebBrowserContextMenuEnabled = false; //! 禁用IE默認的彈出菜單
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
//! 屏幕坐標
Point ScreenCoord = new Point(MousePosition.X, MousePosition.Y);
//! 浏覽器坐標
Point BrowserCoord = webBrowser1.PointToClient(ScreenCoord);
HtmlElement elem = webBrowser1.Document.GetElementFromPoint(BrowserCoord);
//! 隱藏所有菜單項
for (int i = 0; i < contextMenuStrip1.Items.Count; i++)
{
contextMenuStrip1.Items[i].Visible = false;
}
//! 顯示對應的菜單
switch (elem.TagName)
{
case "A":
//! 如果是一個超鏈接,則顯示相關超鏈接的菜單項
openToolStripMenuItem.Visible = true;
openInNewTabToolStripMenuItem.Visible = true;
openInNewWindowToolStripMenuItem.Visible = true;
break;
case "IMG":
//! 如果是圖片,則顯示相關圖片的菜單項
saveImageToolStripMenuItem.Visible = true;
setAsDesktopWallpaperToolStripMenuItem.Visible = true;
break;
default:
//! 其它
refreshToolStripMenuItem.Visible = true;
viewSourceToolStripMenuItem.Visible = true;
break;
}
}
void SaveImageToolStripMenuItemClick(object sender, EventArgs e)
{
afrm.Show();
}
void SetAsDesktopWallpaperToolStripMenuItemClick(object sender, EventArgs e)
{
afrm.Show();
}
void OpenToolStripMenuItemClick(object sender, EventArgs e)
{
afrm.Show();
}
void OpenInNewTabToolStripMenuItemClick(object sender, EventArgs e)
{
afrm.Show();
}
void OpenInNewWindowToolStripMenuItemClick(object sender, EventArgs e)
{
afrm.Show();
}
void RefreshToolStripMenuItemClick(object sender, EventArgs e)
{
afrm.Show();
}
void ViewSourceToolStripMenuItemClick(object sender, EventArgs e)
{
afrm.Show();
}
}
}