程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 替換Webbrowser默認的彈出菜單

替換Webbrowser默認的彈出菜單

編輯:C#入門知識

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();
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved