简体   繁体   English

从 WebBrowser 控件中删除边框

[英]Removing border from WebBrowser control

I've got a WebBrowser control sitting on a Form which has an irritating 2px inset-looking border around it.我有一个 WebBrowser 控件坐在一个 Form 上,它周围有一个令人讨厌的 2px 内嵌边框。 It's causing my content to be clipped by 4 px on the right and bottom, and I can't figure out how to get rid of it.这导致我的内容在右侧和底部被 4 px 剪裁,我不知道如何摆脱它。 The control itself doesn't have any BorderStyle properties -- how does one remove the border?控件本身没有任何 BorderStyle 属性——如何删除边框?

See the red area in the screen shot:请参阅屏幕截图中的红色区域:

Make it stop!让它停下来!http://img229.imageshack.us/img229/8342/badbadwebbrowser.gifhttp://img229.imageshack.us/img229/8342/badbadwebbrowser.gif

I want the WebBrowser to look like the blue area -- that is, to fill the Form and be flush against the Form's edges.我希望 WebBrowser 看起来像蓝色区域——也就是说,填充表单并与表单的边缘齐平。

IE draws that as part of the default style on the body tag. IE 将其绘制为 body 标签上默认样式的一部分。 Set border:0px on the body element and it goes away.在 body 元素上设置border:0px ,它就会消失。

Thankfully, this is going away in IE9 .值得庆幸的是,这将在 IE9 中消失

WebBrowser control inherits display style from control class. WebBrowser 控件从控件类继承显示样式。 If you want to control the border style of control, you can use code like that, eg in Form.Designer.cs:如果你想控制控件的边框样式,你可以使用这样的代码,例如在 Form.Designer.cs 中:


    using System;
    using System.ComponentModel;
    using System.Windows.Forms;

    public class wbExt : System.Windows.Forms.WebBrowser
    {
        private BorderStyle _borderStyle;
        [
        Category("Appearance"),
        Description("The border style")
        ]

        public BorderStyle BorderStyle
        {
            get
            {
                return _borderStyle;
            }
            set
            {
                _borderStyle = value;
                this.RecreateHandle();
                Invalidate();
            }
        }

        protected override CreateParams CreateParams
        {
            get
            {
                const int WS_BORDER = 0x00800000;
                const int WS_EX_STATICEDGE = 0x00020000;
                CreateParams cp = base.CreateParams;
                switch (_borderStyle)
                {
                    case BorderStyle.FixedSingle:
                        cp.Style |= WS_BORDER;
                        break;
                    case BorderStyle.Fixed3D:
                        cp.ExStyle |= WS_EX_STATICEDGE;
                        break;
                }
                return cp;
            }
        }

        public wbExt()
        {
        }
    }

Now you can change generated code in Form class.现在您可以在 Form 类中更改生成的代码。

private wbExt webBrowser1;

and rewrite creation of webBrowser item in InitializeComponent of form, like that:并在表单的 InitializeComponent 中重写 webBrowser 项的创建,如下所示:

this.webBrowser1 = new WindowsFormsApplication1.wbExt();
So, now 所以,现在
webBrowser1.BorderStyle = BorderStyle.None;
will remove any borders from webBrowser control. 将从 webBrowser 控件中删除任何边框。

I have resolved the same problem by adding border:none;我通过添加border:none;解决了同样的问题border:none; style attribute into the html element.将 style 属性添加到html元素中。

<html style="border:none;">

First I tried to add it into the body element but It didn't work, but it works on root html element.首先,我尝试将它添加到body元素中,但没有成功,但它适用于根html元素。

The IE version used on Windows XP for WebBrowser object - I think the version 6 - interprets the root html element as having a border by default.在 Windows XP 上用于 WebBrowser 对象的 IE 版本 - 我认为版本 6 - 默认情况下将根 html 元素解释为具有边框。 If you have direct control over the web page displayed in the WebBrowser, you can add a style attribute directly to the HTML source of the page - as I did -, If not, There should be a way to edit the HTML inside the WebBrowser programmatically on the side of your application.如果您可以直接控制 WebBrowser 中显示的网页,则可以将样式属性直接添加到页面的 HTML 源中 - 就像我所做的那样 - 如果没有,应该有一种方法可以通过编程方式编辑 WebBrowser 中的 HTML在您的应用程序方面。

I cannot reproduce the appearance you are telling about.我无法重现您所说的外观。 My code in the Form1.Designer.cs is:我在 Form1.Designer.cs 中的代码是:

    this.webBrowser1.Location = new System.Drawing.Point(0, 0);
    this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
    this.webBrowser1.Name = "webBrowser1";
    this.webBrowser1.ScrollBarsEnabled = false;
    this.webBrowser1.Size = new System.Drawing.Size(141, 125);
    this.webBrowser1.TabIndex = 0;
    this.webBrowser1.Uri = ....

and the webcontrol is shown without the border... I'm using VS 2008 SP1/Windows 7.并且 webcontrol 显示为没有边框......我使用的是 VS 2008 SP1/Windows 7。

Maybe you should try to add the control inside a container, eg a panel.也许您应该尝试将控件添加到容器中,例如面板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM