Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Just curious about the control shown below, the straight line with label beside it. I tried to find a similar control for it but there was none nor any group box setting, so instead I just made a GroupBox with a height of 2 that replicates it.

But is there an actual control or setting to do this? And what is the actual control called?

Internet Options property dialog

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
700 views
Welcome To Ask or Share your Answers For Others

1 Answer

Spy++ tells us those are actually two separate STATIC controls (similar to a Label in WinForms).

  • The first is simply a regular static text control that says "Home page".

  • The second has the SS_ETCHEDHORZ style set, which makes it draw as a 3D line. Unfortunately, the ability to set this style is not exposed to us from within WinForms.

As you noted in the question, there are some hacks/workarounds that allow us to achieve a similar look, like vertically compressing a GroupBox control, or overriding the OnPaint method of a Label control and using the ControlPaint class to draw a 3D border. They work, but I've never liked them.

But you can actually set the SS_ETCHEDHORZ style yourself so that you can replicate the native UI exactly. Here's a little class that does exactly that. Add it to your project, compile, and you should see a new control appear in your toolbox called "HorizontalRule". Use it just like you would any other control!

public class HorizontalRule : Control
{
    private const int FixedHeight   = 2;

    private const int WS_CHILD      = 0x40000000;
    private const int WS_VISIBLE    = 0x10000000;
    private const int SS_ETCHEDHORZ = 0x00000010;
    private const int SS_ETCHEDVERT = 0x00000011;

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ClassName = "STATIC";
            cp.Style = WS_CHILD | SS_ETCHEDHORZ;
            if (this.Visible)
            {
                cp.Style |= WS_VISIBLE;
            }
            return cp;
        }
    }

    protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
    {
        height = FixedHeight;
        base.SetBoundsCore(x, y, width, height, specified);
    }
}

You can also find more detailed information and additional sample code here on CodeProject.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...