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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…