Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

.net - What is this control? Group Box or Not!

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...