仅允许在 System.Windows.Forms.WebBrowser 控件中复制/粘贴上下文菜单

发布于 2024-07-08 07:40:09 字数 980 浏览 5 评论 0原文

WebBrowser 控件有一个名为“IsWebBrowserContextMenuEnabled”的属性,该属性禁用在网页上右键单击并查看上下文菜单的所有功能。 这非常接近我想要的(我不希望任何人能够右键单击并打印、回击、点击属性、查看源代码等)。

唯一的问题是,这也会禁用文本框中出现的用于复制/粘贴等的上下文菜单。

为了使这一点更清楚,这是我不想要的:
badcontext

这就是我想要的:
goodcontext

我想禁用主上下文菜单,但允许出现的菜单在文本框中。 有人知道我会怎么做吗? WebBrowser.Document.ContextMenuShowing 事件看起来很有希望,但似乎无法通过 HtmlElementEventArgs 参数的“FromElement”和“ToElement”属性正确识别用户右键单击的元素,发送者也不是除了 HtmlDocument 之外的任何内容元素。

提前致谢!

The WebBrowser control has a property called "IsWebBrowserContextMenuEnabled" that disables all ability to right-click on a web page and see a context menu. This is very close to what I want (I don't want anyone to be able to right-click and print, hit back, hit properties, view source, etc).

The only problem is this also disables the context menu that appears in TextBoxes for copy/paste, etc.

To make this clearer, this is what I don't want:
badcontext

This is what I do want:
goodcontext

I would like to disable the main context menu, but allow the one that appears in TextBoxes. Anyone know how I would do that? The WebBrowser.Document.ContextMenuShowing event looks promising, but doesn't seem to properly identify the element the user is right-clicking on, either through the HtmlElementEventArgs parameter's "FromElement" and "ToElement" properties, nor is the sender anything but the HtmlDocument element.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

汐鸠 2024-07-15 07:40:09

您是否考虑过用 JavaScript 编写自己的上下文菜单? 只需听用户右键单击正文,然后使用复制和粘贴命令显示菜单(提示:element.style.display =“block|none”)。 要复制,请执行以下代码:

   CopiedTxt = document.selection.createRange();
   CopiedTxt.execCommand("Copy");

并粘贴:

   CopiedTxt = document.selection.createRange();
   CopiedTxt.execCommand("Paste");

源:

http://www.geekpedia.com/tutorial126_Clipboard-cut-copy-and-paste-with-JavaScript.html

注意:这仅适用于 IE(这适合您的应用程序)。

我知道它无论如何都不是防弹的,但这里有一个代码示例可以帮助您入门:

<html>
    <head>
        <script type = "text/javascript">
            var lastForm = null;
            window.onload = function(){

                var menu = document.getElementById("ContextMenu");
                var cpy = document.getElementById("CopyBtn");
                var pst = document.getElementById("PasteBtn");

                document.body.onmouseup = function(){
                    if (event.button == 2)
                    {
                        menu.style.left = event.clientX + "px";
                        menu.style.top = event.clientY + "px";
                        menu.style.display = "block";

                        return true;
                    }

                    menu.style.display = "none";
                };

                cpy.onclick = function(){
                    copy = document.selection.createRange();
                    copy.execCommand("Copy");
                    return false;
                };

                pst.onclick = function(){
                    if (lastForm)
                    {
                        copy = lastForm.createTextRange();
                        copy.execCommand("Paste");
                    }
                    return false;
                };
            };
        </script>
    </head>

    <body oncontextmenu = "return false;">
        <div id = "ContextMenu" style = "display : none; background: #fff; border: 1px solid #aaa; position: absolute;
            width : 75px;">
            <a href = "#" id = "CopyBtn" style = "display: block; color : blue; text-decoration: none;">Copy</a>
            <a href = "#" id = "PasteBtn" style = "display: block; color : blue; text-decoration: none;">Paste</a>
        </div>
        sadgjghdskjghksghkds
        <input type = "text" onfocus = "lastForm = this;" />
    </body>
</html>

have you considered writing your own context menu in javascript? Just listen to the user right clicking on the body, then show your menu with copy and paste commands (hint: element.style.display = "block|none"). To copy, execute the following code:

   CopiedTxt = document.selection.createRange();
   CopiedTxt.execCommand("Copy");

And to paste:

   CopiedTxt = document.selection.createRange();
   CopiedTxt.execCommand("Paste");

Source:

http://www.geekpedia.com/tutorial126_Clipboard-cut-copy-and-paste-with-JavaScript.html

NOTE: This only works in IE (which is fine for your application).

I know its not bulletproof by any means, but here is a code sample that should get you started:

<html>
    <head>
        <script type = "text/javascript">
            var lastForm = null;
            window.onload = function(){

                var menu = document.getElementById("ContextMenu");
                var cpy = document.getElementById("CopyBtn");
                var pst = document.getElementById("PasteBtn");

                document.body.onmouseup = function(){
                    if (event.button == 2)
                    {
                        menu.style.left = event.clientX + "px";
                        menu.style.top = event.clientY + "px";
                        menu.style.display = "block";

                        return true;
                    }

                    menu.style.display = "none";
                };

                cpy.onclick = function(){
                    copy = document.selection.createRange();
                    copy.execCommand("Copy");
                    return false;
                };

                pst.onclick = function(){
                    if (lastForm)
                    {
                        copy = lastForm.createTextRange();
                        copy.execCommand("Paste");
                    }
                    return false;
                };
            };
        </script>
    </head>

    <body oncontextmenu = "return false;">
        <div id = "ContextMenu" style = "display : none; background: #fff; border: 1px solid #aaa; position: absolute;
            width : 75px;">
            <a href = "#" id = "CopyBtn" style = "display: block; color : blue; text-decoration: none;">Copy</a>
            <a href = "#" id = "PasteBtn" style = "display: block; color : blue; text-decoration: none;">Paste</a>
        </div>
        sadgjghdskjghksghkds
        <input type = "text" onfocus = "lastForm = this;" />
    </body>
</html>
梦里人 2024-07-15 07:40:09
//Start:

function cutomizedcontextmenu(e)
{
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if( navigator.userAgent.toLowerCase().indexOf("msie") != -1 )
    {
        if (target.type != "text" && target.type != "textarea" && target.type != "password") 
        {
            alert(message);
            return false;
        }
    return true;
    }
    else if( navigator.product == "Gecko" )
    {
        alert(message);
        return false;
    }
} 

document.oncontextmenu = cutomizedcontextmenu;
//End:

我希望这对你有帮助 安德森·伊姆斯

//Start:

function cutomizedcontextmenu(e)
{
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if( navigator.userAgent.toLowerCase().indexOf("msie") != -1 )
    {
        if (target.type != "text" && target.type != "textarea" && target.type != "password") 
        {
            alert(message);
            return false;
        }
    return true;
    }
    else if( navigator.product == "Gecko" )
    {
        alert(message);
        return false;
    }
} 

document.oncontextmenu = cutomizedcontextmenu;
//End:

I hope this will help you Anderson Imes

浮华 2024-07-15 07:40:09

快速浏览一下 MSDN 文档 显示您的程序不支持使用任何鼠标事件(单击、按钮向下/向上等)。 恐怕要么是:要么禁用 conetxt 菜单,要么允许它们。

如果禁用它们,用户仍然可以复制和复制它们。 使用键盘快捷键(Ctrl-C、Ctrl-V)粘贴。 也许这给了你你需要的功能。

A quick look at the MSDN documentation shows that none of the mouse events (click, button down/up etc) are supported to be used in your program. I'm afraid its either or: Either disable conetxt menus, or allow them.

If you disable them, the user can still copy & paste using keyboard shortcuts (Ctrl-C, Ctrl-V). Maybe that gives you the functionality you need.

一个人练习一个人 2024-07-15 07:40:09

我们最终使用了上述两种评论的组合。 更接近第二个,这就是我给他信任的原因。

有一种方法可以在客户端 Web 代码上以及通过 winforms 替换上下文菜单,这就是我们采用的方法。 我真的不想重写上下文菜单,但这似乎给了我们正确的控制组合。

We ended up using a combination of both of the above comments. Closer to the second, which is why I gave him credit.

There is a way to replace the context menu on both the client-side web code as well as through winforms, which is the approach we took. I really didn't want to rewrite the context menu, but this seems to have given us the right mix of control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文