What is this control? Group Box or Not!
Asked Answered
R

2

6

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

Ranson answered 4/2, 2011 at 11:41 Comment(0)
P
12

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.

Paigepaik answered 4/2, 2011 at 11:58 Comment(0)
C
0

I had the same problem a couple of years ago and ended up just drawing a line for the purpose.

In fact I even used one fixed line image of a sufficiently long width so that it could be used in all cases by showing the required part (width) of the image.

This solution has worked fine for me ever since.

Cribwork answered 4/2, 2011 at 11:43 Comment(5)
Using an image will be a problem if the style of the controls changes in a future version of Windows. Your control will still have the old style.Digital
Technically that's true, but if it is just a simple thin line then there is not much "style" to it so it won't matter. My line has consistently been showing nicely in XP, Vista and Windows 7 also with different themes.Cribwork
It's very possible that a future version of Windows dramatically changes the style of that "simple thin line". Notice how much group boxes changed from the "Classic" theme to Luna in Windows XP, and then again in Aero in Windows Vista. (Another reason why using a group box won't work for duplicating the example in the original question.)Paigepaik
Perhaps I wasn't clear. I'm using a PictureBox control with transparant BackColor and BorderStyle=none. So nothing of the control itself is visible except the image that it holds. This is a BMP resource with dimensions 1000x2 pixels. I'm confident that the exact same line will still be shown against a transparant background and without borders in future windows versions. The only drawback is that the color of the line will never change so if the background color of the form changes, the line color may need to be adjusted.Cribwork
Not my point. You're using an image to simulate the natively-drawn control. What if that natively-drawn control changes and looks different in a future version of Windows? Your earlier argument questioned the likelihood of that scenario, but my response was to look at the GroupBox control. That's changed drastically for the past few versions of Windows. If you were trying to simulate that with an image, it would look outdated and wrong. That's why this is not a good approach.Paigepaik

© 2022 - 2024 — McMap. All rights reserved.