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 replicate开发者_如何学JAVAs it.
But is there an actual control or setting to do this? And what is the actual control called?
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.
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.
精彩评论