禁用 WPF WebBrowser 滚动条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12930297/
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
Disable WPF WebBrowser scrollbar
提问by Alvin
I am trying to hide the webbrowser scrollbar, but it is still visible.
我试图隐藏 webbrowser 滚动条,但它仍然可见。
XAML:
XAML:
<WebBrowser Name="wb" Width="700" Height="600"
OverridesDefaultStyle="False"
ScrollViewer.CanContentScroll="False"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Hidden" />
Thank you.
谢谢你。
回答by befstrat
This works for me:
这对我有用:
<WebBrowser LoadCompleted="wb_LoadCompleted"></WebBrowser>
void wb_LoadCompleted(object sender, NavigationEventArgs e)
{
string script = "document.body.style.overflow ='hidden'";
WebBrowser wb = (WebBrowser)sender;
wb.InvokeScript("execScript", new Object[] { script, "JavaScript" });
}
This way you don't need mshtml
这样你就不需要 mshtml
回答by Sisyphe
Not ideal but it works :
不理想,但它有效:
Add Microsoft.mshtml to your project references. Then change your xaml to this :
将 Microsoft.mshtml 添加到您的项目引用中。然后将您的 xaml 更改为:
<WebBrowser Name="wb" Width="700" Height="600"
OverridesDefaultStyle="False"
ScrollViewer.CanContentScroll="False"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
LoadCompleted="wb_LoadCompleted"></WebBrowser>
and in your code behind :
在你后面的代码中:
private void wb_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
mshtml.IHTMLDocument2 dom = (mshtml.IHTMLDocument2)wb.Document;
dom.body.style.overflow = "hidden";
}
回答by doron aviguy
in your html ....
在你的 html ....
html{overflow:hidden;}
it should solve it or you can use meta tag to specify Ie render mode
它应该解决它,或者您可以使用元标记来指定即渲染模式
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
回答by TomerAgmon1
Adding scroll="no"
to the html body
tag worked for me while other suggestions here did not.
添加scroll="no"
到 htmlbody
标签对我有用,而这里的其他建议没有。
回答by Devdude
Add Microsoft.mshtml to your project references. You dont need to change any of the scroll properties in XAML as they are not the one controlling the webbrowser when mshtml is used. In the LoadCompleted function do the following:
将 Microsoft.mshtml 添加到您的项目引用中。您不需要更改 XAML 中的任何滚动属性,因为它们不是在使用 mshtml 时控制 webbrowser 的属性。在 LoadCompleted 函数中执行以下操作:
private void webBrowserChat_LoadCompleted(object sender, NavigationEventArgs e)
{
mshtml.IHTMLDocument2 documentText = (IHTMLDocument2)webBrowserChat.Document;
//this will access the document properties
documentText.body.parentElement.style.overflow = "hidden";
// This will hide the scrollbar (Set to "auto" if you want to see when it passes the surfacelimit)
}
回答by kurakura88
Sorry a bit late, but I finally am able to disable the scrollbar. The hint from @Devdude was the key.
抱歉有点晚了,但我终于能够禁用滚动条了。来自@Devdude 的提示是关键。
The main point is to set the overflow = hidden
, but how to do that in WPF? I used DependencyObject
so that I can bind: enable and disable whenever I want.
重点是设置overflow = hidden
,但如何在 WPF 中做到这一点?我使用DependencyObject
以便我可以绑定:随时启用和禁用。
First of all you need to add reference to mshtml
. In your project, add reference add Microsoft.mshtml
. Then in your .cs
file add:
首先,您需要添加对mshtml
. 在您的项目中,添加引用 add Microsoft.mshtml
。然后在您的.cs
文件中添加:
using mshtml;
The DependencyObject
依赖对象
public class WebBrowserUtility : DependencyObject
{
public static readonly DependencyProperty HideScrollBarProperty = DependencyProperty.RegisterAttached(
"HideScrollBar",
typeof(string),
typeof(WebBrowserUtility),
new UIPropertyMetadata(null, HideScrollBarPropertyChanged));
public static string GetHideScrollBar(DependencyObject obj)
{
return (string)obj.GetValue(HideScrollBarProperty);
}
public static void SetHideScrollBar(DependencyObject obj, string value)
{
obj.SetValue(HideScrollBarProperty, value);
}
public static void HideScrollBarPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
WebBrowser browser = obj as WebBrowser;
string str = args.NewValue as string;
bool isHidden;
if (str != null && bool.TryParse(str, out isHidden))
{
browser.HideScrollBar(isHidden);
}
}
}
The WebBrowser
Extension, which actually does the work to disable the overflow, which only happens after WebBrowser
Document Load is Completed:
该WebBrowser
扩展,它实际上做的工作禁用溢出,之后只发生WebBrowser
文档加载完成:
public static class WebBrowserExtension
{
public static void HideScrollBar(this WebBrowser browser, bool isHidden)
{
if (browser != null)
{
IHTMLDocument2 document = browser.Document as IHTMLDocument2;
if (document == null)
{
// If too early
browser.LoadCompleted += (o, e) => HideScrollBar(browser, isHidden);
return;
}
//string bodyOverflow = string.Format("document.body.style.overflow='{0}';", isHidden ? "hidden" : "auto");
//document.parentWindow.execScript(bodyOverflow); // This does not work for me...
string elementOverflow = string.Format("document.documentElement.style.overflow='{0}';", isHidden ? "hidden" : "auto");
document.parentWindow.execScript(elementOverflow);
}
}
}
To use in the XAML
在 XAML 中使用
<WebBrowser ns:WebBrowserUtility.HideScrollBar="True"/>
Note: Make sure that you stretch the WebBrowser
to see the whole contents. Regardless, the scrollbar
will be hidden this time.
注意:确保拉伸WebBrowser
以查看全部内容。无论如何,这次scrollbar
将被隐藏。
回答by Richard Garside
I've made my WebBrowser control wider than the visible area by 16 pixels which is the width of the scrollbar.
我使我的 WebBrowser 控件比可见区域宽 16 像素,这是滚动条的宽度。
This will only work if your web browser control touches the far right of your app. The web browser control will not allow other XAML elements on top of it, but you can make it overflow the edge of your app.
这仅在您的 Web 浏览器控件触及应用程序的最右侧时才有效。Web 浏览器控件不允许在它上面有其他 XAML 元素,但您可以使其溢出应用程序的边缘。
I've done this in a Loaded event handler for the window:
我已经在窗口的 Loaded 事件处理程序中完成了此操作:
private void AppLoaded(object sender, RoutedEventArgs routedEventArgs)
{
WebBrowserView.Width = WebBrowserView.ActualWidth + 16;
}
I found injecting JavaScript into the page seemed to break some pages and the scrollbar would only disappear once the page had completed loading.
我发现将 JavaScript 注入页面似乎会破坏一些页面,并且只有在页面完成加载后滚动条才会消失。
回答by Eido95
The overflowproperty assigned with hidden
value on body
tag solves this issue.
标签上赋值的溢出属性解决了这个问题。hidden
body
If you have a css rule-set for your body
tag, add the following line to it:
如果您的body
标签有 css 规则集,请向其中添加以下行:
overflow: hidden
Otherwise, add the following line to your concrete <body>
tag deceleration:
否则,将以下行添加到您的具体<body>
标记减速中:
style="overflow:hidden"
回答by Jeff
This isn't quite the same problem you all had, but my searching brought me here so I hope this helps someone. I was trying to embed a PDF document using the WPF WebBrowser component. Turns out it was the PDF viewer (called from the browser) that was creating the scroll bars!
这与你们遇到的问题并不完全相同,但是我的搜索将我带到了这里,所以我希望这对某人有所帮助。我试图使用 WPF WebBrowser 组件嵌入 PDF 文档。原来是 PDF 查看器(从浏览器调用)创建了滚动条!
To solve it add parameters to the call to the PD file. In my XML: Source="http://address/file.pdf#toolbar=0&navpanes=0&scrollbar=0"
要解决它,请向 PD 文件的调用添加参数。在我的 XML 中:Source="http://address/file.pdf#toolbar=0&navpanes=0&scrollbar=0"
In the code behind it would be in the format as follows: http://address/file.pdf#toolbar=0&navpanes=0&scrollbar=0
在它后面的代码中,格式如下:http://address/file.pdf#toolbar=0&navpanes=0&scrollbar=0
(Thanks to leeand00for this solution from his answer to thisquestion.)
回答by HackSlash
I used this simple one line to directly define the body of the document:
我用这个简单的一行来直接定义文档的正文:
wb.Document.Body.scroll = "no"