| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 841 人关注过本帖
标题:替换Webbrowser默认的弹出菜单
只看楼主 加入收藏
ioriliao
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:贵宾
威 望:32
帖 子:2829
专家分:647
注 册:2006-11-30
结帖率:78.95%
收藏
 问题点数:0 回复次数:0 
替换Webbrowser默认的弹出菜单
HTMLElementSelect.rar (113.9 KB)

程序代码:
using System;
using System.Collections.Generic;
using 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://");
            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();
        }
    }
}
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: Webbrowser 菜单 
2010-11-25 22:20
快速回复:替换Webbrowser默认的弹出菜单
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034473 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved