Replace the default Webbrowser pop-up menu

Source: Internet
Author: User

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 ;//! Set our own pop-up menu
WebBrowser1.IsWebBrowserContextMenuEnabled = false ;//! Disable the default pop-up menu of IE
}

Private void contextmenustripappsopening (object sender, CancelEventArgs e)
{
//! Screen coordinates
Point ScreenCoord = new Point (MousePosition. X, MousePosition. Y );
//! Browser coordinates
Point BrowserCoord = webBrowser1.PointToClient (ScreenCoord );
HtmlElement elem = webBrowser1.Document. GetElementFromPoint (BrowserCoord );

//! Hide all menu items
For (int I = 0; I <contextMenuStrip1.Items. Count; I ++)
{
ContextMenuStrip1.Items [I]. Visible = false;
}

//! Display the corresponding menu
Switch (elem. TagName)
{
Case "":
//! For a hyperlink, the relevant hyperlink menu items are displayed.
OpenToolStripMenuItem. Visible = true;
OpenInNewTabToolStripMenuItem. Visible = true;
OpenInNewWindowToolStripMenuItem. Visible = true;
Break;
Case "IMG ":
//! If it is an image, the corresponding menu items are displayed.
SaveImageToolStripMenuItem. Visible = true;
SetAsDesktopWallpaperToolStripMenuItem. Visible = true;
Break;
Default:
//! Others
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 ();
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.