开发者

Add Border to WebBrowser control

开发者 https://www.devze.com 2022-12-14 02:46 出处:网络
When I add a WebBrowser control on my TabPage, it doesn\'t have a border. I can\'t find a BorderStyle attribute.

When I add a WebBrowser control on my TabPage, it doesn't have a border. I can't find a BorderStyle attribute. How do get the control to have a border? (3D, sunken, whatever)

Add Border to WebBrowser control

Only by the scrollbar on 开发者_StackOverflow中文版the right you see there's actually a control there...


Gumpy comments, not accurate. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of your toolbar onto your form.

using System;
using System.Windows.Forms;

class MyWebBrowser : WebBrowser {
  protected override CreateParams CreateParams {
    get {
      var parms = base.CreateParams;
      parms.Style |= 0x800000;  // Turn on WS_BORDER
      return parms;
    }
  }
}

The other border styles work too, check out WinUser.h in the SDK.


You can wrap the WebBrowser control in a Panel and set the Panel.BorderStyle property.

Panel panel1 = new Panel();
panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
panel1.Controls.Add(webbrowser1);


First of all be aware that IE 6 paints the border alone if you specify

<!DOCTYPE html> or

body { border-style:inset; border-width:2px; margin:0px; }

But this does not work anymore with newer IE versions.

So if you paint the border in C# and specify a DOCTYPE you must set

html,body { border:0; }

otherwise you have two borders in IE 6.


The solution of Hans (to set WS_BORDER) produces an ugly black border.

If you want a sunken 3D border the easiest solution is this:

protected override CreateParams CreateParams
{
    get
    {
        CreateParams i_Parms = base.CreateParams;
        i_Parms.ExStyle |= 0x200;  // WS_EX_CLIENTEDGE
        return i_Parms;
    }
}

The problem is that the sunken 3D border was used until Windows 2000. But things changed with XP where the UXTHEME.DLL paints all the controls rather then the good old USER32.DLL. All the control painting flags in User.h were used up to Windows 2000 and do not produce Theme compatible painting anymore since XP.

So how to get a border that looks like the XP/Windows 7 Theme border of the ListBox, ListView, TreeView etc, (a thin blue or green line) and changes the color when you change the theme?

An easy solution is to insert a ListBox in the Form Designer in VS at the location where you want the browser to appear and then write in your Form:

protected override void OnLoad(EventArgs e)
{
    ....
    listBox.IntegralHeight = false;
    webBrowser.Parent = listBox;
    webBrowser.Dock   = DockStyle.Fill;
}

This makes the browser be a child of the listbox. The listbox paints its border and the browser inside paints the Html content.

This works perfect and looks the same way as the other themed controls but a little disadvantage is that resizing the form flickers more because there is one more control to be painted now.

The perfect solution would be to pinvoke DrawThemeBackground or DrawThemeEdge to paint the border manually or to obtain the border color by GetThemeColor and paint the border with System.Drawing.Graphics.DrawRectangle() But this is quite clumsy so I didn't try it. (Additionaly you would have to implement a fallback if the user has turned off themes)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号