Cod*_*ray 10
Spy ++告诉我们这些实际上是两个独立的STATIC控件(类似于LabelWinForms中的一个).
第一个是简单的常规静态文本控件,显示"主页".
第二个具有SS_ETCHEDHORZ样式集,使其绘制为3D线.不幸的是,在WinForms中没有向我们展示设置此样式的能力.
正如您在问题中所提到的,有一些黑客/变通方法可以让我们实现类似的外观,例如垂直压缩GroupBox控件,或者重写控件的OnPaint方法Label并使用ControlPaint类来绘制3D边框.他们工作,但我从来没有喜欢过他们.
但您实际上可以SS_ETCHEDHORZ自己设置样式,以便您可以完全复制本机UI.这是一个完全相同的小班.将它添加到您的项目中,编译,您应该会在工具箱中看到一个名为"HorizontalRule"的新控件.像使用任何其他控件一样使用它!
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);
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以在CodeProject上找到更多详细信息和其他示例代码.
| 归档时间: |
|
| 查看次数: |
671 次 |
| 最近记录: |