W自定义滚动条未出现在 WebBrowser 控件上

发布于 2024-08-17 01:09:26 字数 1443 浏览 1 评论 0原文

我正在尝试让滚动条与网络浏览器控件一起使用。但是,由于我将使用它来显示自定义消息框的消息,因此即使不需要滚动条,我也不希望出现滚动条 - 就像默认情况下那样。为了避免这种情况,我决定禁用控件上的滚动条,而是在另一个控件(如面板)上使用滚动条。这样,只有当浏览器页面的内容太大而无法容纳时,它们才会出现。

尽管我读过很多帖子,甚至在 StackOverflow 上,这似乎是一个有效的解决方案,但效果并不是很好。一个例子是当我尝试使用此处的解决方案时:

Panel 控件中包含的 WebBrowser 控件的滚动问题

似乎如果 Web 浏览器禁用了滚动条,它也不会让面板使用滚动条。在设计模式下测试时似乎就是这种情况。为了克服这个问题,我尝试在面板内的网络浏览器后面添加一个图片框;它在设计模式下工作(调整图片框和网络浏览器的大小将导致面板启用其滚动条),但在运行时不起作用(我添加了代码以使图片框更改为网络浏览器控件的大小 -它本身总是会调整大小以适应可滚动内容的大小)。

我还尝试根据 ScrollableRectangle 大小是否大于控件的大小,以编程方式启用和禁用 Web 浏览器的滚动条。从理论上讲,这很好,但每次更改 ScrollbarsEnabled 属性时似乎都会清除控件内的所有文本 - 从而更改回没有滚动条。

我使用以下代码执行此操作,只要在控件中按下某个键,就会有效地调用:

if (Output.Document.Body != null)
{
    if (Output.Document.Body.ScrollRectangle.Size.Height > Output.Size.Height
            || Output.Document.Body.ScrollRectangle.Size.Width > Output.Size.Width)
        Output.ScrollBarsEnabled = true;
    else
        Output.ScrollBarsEnabled = false;
}
else
    Output.ScrollBarsEnabled = false;

还需要注意的是,我还需要一个将在应用程序中使用的 HTML 编辑器的解决方案,因此理想情况下任何解决方案都不会依赖于页面加载事件等...因为当 Web 浏览器将设计模式设置为打开时(这是它作为 HTML 编辑器工作所必需的),这些事件似乎不会触发。但是,在这种特殊情况下,如果没有更好的解决方案,我可以依靠启用默认滚动条。

编辑:需要明确的是,我并不是在谈论 HTML 内容中的任何滚动条 - 这并不重要,因为 HTML 只是用于允许灵活的文本格式设置。我只讨论浏览器控件本身的滚动条。

非常感谢任何帮助。谢谢!

I'm trying to get scrollbars working with the web browser control. However, as I'll be using it to display a message for a custom messagebox, I don't want the scrollbar to appear even if its not needed - as it seems to do by default. To circumvent this I decided to disable scrollbars on the control and instead use scrollbars on another control like a Panel. This way they'll only appear when the contents of the browser page is too big to fit.

This hasn't worked out too well, though I've read quite a few posts, even on StackOverflow, where this seems to be a valid solution. One example is when I tried using the solution here:

Scrolling problem with a WebBrowser control contained in a Panel control

It seems as though if Scrollbars are disabled for the web browser, it won't let the panel use scrollbars either. This seemed to be the case when testing in design mode. To overcome this I tried adding a picture box behind the web browser inside the panel; it worked when in design mode (resizing the picture box and web browser would cause the panel to enable its scrollbars), but didn't work during runtime (I added code to have the picture box change to the size of the web browser control - which itself is always resized to fit the size of the scrollable contents).

I also tried programmatically enabling and disabling the web browser's scrollbars based on if the ScrollableRectangle size was bigger than the size of the control. This theoretically would be fine, except it seems to clear all text within the control any time the ScrollbarsEnabled property is changed - and thus is changed back to having no scrollbars.

I'm doing this with the following code, called effectively whenever a key is pressed in the control:

if (Output.Document.Body != null)
{
    if (Output.Document.Body.ScrollRectangle.Size.Height > Output.Size.Height
            || Output.Document.Body.ScrollRectangle.Size.Width > Output.Size.Width)
        Output.ScrollBarsEnabled = true;
    else
        Output.ScrollBarsEnabled = false;
}
else
    Output.ScrollBarsEnabled = false;

It's also important to note that I also need a solution for the HTML editor which will be used within the app, so ideally any solution would not rely on a page load event etc... as these do not seem to trigger when the web browser has design mode set to on (which is needed for it to work as an HTML editor). However, in this particular situation I can fall back on enabling the default scrollbars if there's no better solution.

EDIT: To be clear, I am not talking about any scrollbars within the HTML content - that is of no concern as the HTML is simply being used to allow for flexible formatting of text. I'm talking only about the scrollbar of the browser control itself.

Any help much appreciated. Thanks!

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

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

发布评论

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

评论(2

温柔戏命师 2024-08-24 01:09:26

我不久前终于找到了解决方案,但忘记在这里发布。基本上我所做的是首先默认启用滚动条,这样它们就可以工作,尽管总是出现。然后,我创建了一个面板控件,并将其大小调整到出现在 Web 浏览器控件右侧的非活动滚动条的顶部。

接下来,我更改了滚动条面板的锚点,以便顶部、底部和右侧始终卡入表单的大小,因此浏览器控件的大小也被锚定,因为它的所有边缘也被锚定。

然后我添加了一些代码来检查 ScrollRectangle 尺寸并将其与浏览器的尺寸进行比较,如果结果是高度或宽度更大,则我使覆盖面板不可见,但否则保留它就位。

这是相关的代码片段:

            //If still bigger, set scrollbars:

            if ((Output.Document.Body.ScrollRectangle.Size.Height > Output.Size.Height) ||
                Output.Document.Body.ScrollRectangle.Size.Width > Output.Size.Width)
            {
                ScrollPanel.Visible = false;
                ScrollPanel.Enabled = false;
            }

希望这对将来的其他人有帮助,在我找到一种效果足够好的方法之前,采取了几种不同的方法。

我决定提高盛的回答(当我获得足够的声誉时),因为首先他是唯一做出回应的人,其次,当我考虑使用某种调用来尝试启用滚动条时,他的信息帮助了我在运行时。尽管我发现我的解决方案更简单且同样有效。

I ended up finding a solution to this some time ago now, but forgot to post here. Basically what I did was first enable the scrollbars by default so they will work, although always appear. Then I created a panel control and sized it over the top of the inactive scrollbar that appears on the right of the web browser control.

Next I changed the anchors of the scroll bar panel so that the top, bottom, and right sides would always snap into the size of the form, and hence the size of the browser control as all edges of it too are anchored.

Then I added some code that checked the ScrollRectangle size and compared it with that of the browser, if it turned out to be larger in height or width, I then made the cover panel not visible, but otherwise left it in place.

Here's the relevant code snippets:

            //If still bigger, set scrollbars:

            if ((Output.Document.Body.ScrollRectangle.Size.Height > Output.Size.Height) ||
                Output.Document.Body.ScrollRectangle.Size.Width > Output.Size.Width)
            {
                ScrollPanel.Visible = false;
                ScrollPanel.Enabled = false;
            }

Hope this helps someone else in future, took a few different methods before I found one that worked well enough.

I've decided to up Sheng's answer (when I get enough reputation) as, firstly he was the only person to response, and secondly, his information helped me when I was contemplating the use of some kind of invocation to try and enable the scrollbars during runtime. Though I found my solution simpler and just as effective.

站稳脚跟 2024-08-24 01:09:26

那就太晚了。创建 Web 浏览器时调用 IDocHostUIHandler.GetHostInfo,并且 WebBrowser 的实现根据其 ScrollBarsEnabled 属性的值设置 DOCHOSTUIFLAG_SCROLL_NO 或 DOCHOSTUIFLAG_FLAT_SCROLLBAR 标志。

我建议您在创建 Webbrowser 控件的窗口之前将 ScrollBarsEnabled 设置为 false。

如果您不希望出现特定元素(例如 body、div 或 textarea)的滚动条,可以将它们的样式设置为 Overflow='hidden' 或使用特定于元素的滚动属性,例如 doc.Body.SetAttribute (“滚动”,“否”)。

That's too late. IDocHostUIHandler.GetHostInfo is called when the webbrowser is created, and the WebBrowser's implementation sets DOCHOSTUIFLAG_SCROLL_NO or the DOCHOSTUIFLAG_FLAT_SCROLLBAR flag based on the value of its ScrollBarsEnabled property.

I suggest you to set ScrollBarsEnabled to false before creating the Webbrowser control's window.

If you don't want the scrollbar of a particular element, such as body, div or textarea, to appear, you can set their styles to overflow='hidden' or use scrolling properties specific to the elements such as doc.Body.SetAttribute("scroll","no").

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