C# 如何禁用浏览器使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/591543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:29:00  来源:igfitidea点击:

C# how to disable webbrowser usage

c#winforms

提问by MD6380

I have a C# application that has a webbrowser and need to be able to completely disable it at times. What is the best method of doing this?

我有一个 C# 应用程序,它有一个 webbrowser,有时需要能够完全禁用它。这样做的最佳方法是什么?

I was thinking about creating a transparent panel to put over the webbrowser when needed, but am not sure if this is the best approach. I also noticed that C# doesn't easily allow making a panel transparent.

我正在考虑创建一个透明面板以在需要时放置在网络浏览器上,但我不确定这是否是最好的方法。我还注意到 C# 不允许使面板透明。

Edit: To clarify what I mean by disable...I don't want to allow the user to be able to click or change\edit anything in the webbrowser.

编辑:澄清我所说的禁用的意思......我不想让用户能够点击或更改\编辑网络浏览器中的任何内容。

采纳答案by andynormancx

Strangely the WebBrowser control doesn't have an Enabled property as you might expect it to. You can set the AllowNavigation property to false which will stop the user clicking on links.

奇怪的是,WebBrowser 控件没有您所期望的 Enabled 属性。您可以将 AllowNavigation 属性设置为 false,这将阻止用户单击链接。

Turns out it does have an Enabled property, inherited from Control. But you have to explicitly cast it to Control to access it:

原来它确实有一个 Enabled 属性,继承自 Control。但是您必须将其显式转换为 Control 才能访问它:

((Control)webBrowser1).Enabled = false;

You should be aware though that setting Enabled completelydisables any user interaction with the WebBrowser control. That includes the scroll bar, so once you set Enabled to false the user can't even scroll the web page in the control.

您应该知道,设置 Enabled 会完全禁用任何用户与 WebBrowser 控件的交互。这包括滚动条,因此一旦您将 Enabled 设置为 false,用户甚至无法在控件中滚动网页。

If that was more than you wanted then setting the AllowNavigation property to false will stop the user clicking on links but still allow them to scroll the page. You'd need to check though that it also stops Javascript onclick events from firing.

如果这超出了您的预期,则将 AllowNavigation 属性设置为 false 将阻止用户单击链接,但仍允许他们滚动页面。您需要检查它是否还会阻止 Javascript onclick 事件触发。

If you wanted to stop any events from firing in the page you could probably achieve that will some DOM work from the WebForms code into the WebBrowser control. You also probably need to start disabling input controls within the WebBrowser. It all depends how disabled you want it.

如果您想阻止在页面中触发任何事件,您可能会实现将某些 DOM 从 WebForms 代码工作到 WebBrowser 控件中。您可能还需要开始禁用 WebBrowser 中的输入控件。这一切都取决于您想要它的禁用程度。

回答by Franci Penov

Based on your last comment, I would assume you are using WinForms. (WPF does allow for transparent panels quite easily :-)) This means you are using the WebBrowsercontrol. Have you tried setting its Enabledproperty to false?

根据您的最后一条评论,我假设您使用的是 WinForms。(WPF 确实允许使用透明面板很容易:-))这意味着您正在使用WebBrowser控件。您是否尝试将其Enabled属性设置为 false?

回答by LoveMeSomeCode

When you say 'disable' it, what do you mean? If you set the 'Enabled' property to false, it will be disabled, this works for all form controls. But if you needed to, you can set a panel to transparent by setting it's BackColor.

当您说“禁用”它时,您是什么意思?如果您将 'Enabled' 属性设置为 false,它将被禁用,这适用于所有表单控件。但如果需要,您可以通过设置它的 BackColor 将面板设置为透明。

UPDATE: Actually, you're right, it's an wrapper for an ActiveX control, so it's not a typical WinForms control. What you want is the transparent paenl solution. This guy Tobi has a solution on his blog:

更新:实际上,您是对的,它是 ActiveX 控件的包装器,因此它不是典型的 WinForms 控件。你想要的是透明的paenl解决方案。这家伙 Tobi 在他的博客上有一个解决方案:

http://saftsack.fs.uni-bayreuth.de/~dun3/archives/creating-a-transparent-panel-in-net/108.html

http://saftsack.fs.uni-bayreuth.de/~dun3/archives/creating-a-transparent-panel-in-net/108.html

the basic idea is overriding this method on the panel class:

基本思想是在面板类上覆盖此方法:

protected override CreateParams CreateParams
{
    get
    {
         CreateParams createParams = base.CreateParams;
         createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
         return createParams;
    }
}

回答by cjk

Can you remove the webbrowser from its parent Control array?

你能从它的父控件数组中删除 webbrowser 吗?

回答by user2048591

In VB: DirectCast(WebBrowser1, Control).Enabled = False

在 VB 中: DirectCast(WebBrowser1, Control).Enabled = False