jim*_*im 4 c# controls visual-studio-2008 winforms
我想让面板有一个厚边框.我能以某种方式设置吗?
PS,我正在使用C#.VS 2008.
吉姆
我已经创建了一个用户控件,并给出了一个ParentControlDesigner.正如我在评论中指出的那样,它并不是你所要求的完美解决方案.但它应该是一个很好的起点.哦,任何FYI,我也有可定制的边框颜色.我受到了另一个SO帖子的启发,追求这个...它比我想象的更棘手.要在设置边框大小时正确重新排列,可以调用PerformLayout.DisplayRectangle的覆盖和OnResize中对SetDisplayRectLocation的调用会导致子控件的正确重新定位.同样,子控件在最左上角没有预期的"0,0"...除非边框宽度设置为0 ...而OnPaint提供边框的自定义绘图.
祝你好运!制作父母的自定义控件很棘手,但并非不可能.
[Designer(typeof(ParentControlDesigner))]
public partial class CustomPanel : UserControl
{
Color _borderColor = Color.Blue;
int _borderWidth = 5;
public int BorderWidth
{
get { return _borderWidth; }
set { _borderWidth = value;
Invalidate();
PerformLayout();
}
}
public CustomPanel() { InitializeComponent(); }
public override Rectangle DisplayRectangle
{
get
{
return new Rectangle(_borderWidth, _borderWidth, Bounds.Width - _borderWidth * 2, Bounds.Height - _borderWidth * 2);
}
}
public Color BorderColor
{
get { return _borderColor; }
set { _borderColor = value; Invalidate(); }
}
new public BorderStyle BorderStyle
{
get { return _borderWidth == 0 ? BorderStyle.None : BorderStyle.FixedSingle; }
set { }
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaintBackground(e);
if (this.BorderStyle == BorderStyle.FixedSingle)
{
using (Pen p = new Pen(_borderColor, _borderWidth))
{
Rectangle r = ClientRectangle;
// now for the funky stuff...
// to get the rectangle drawn correctly, we actually need to
// adjust the rectangle as .net centers the line, based on width,
// on the provided rectangle.
r.Inflate(-Convert.ToInt32(_borderWidth / 2.0 + .5), -Convert.ToInt32(_borderWidth / 2.0 + .5));
e.Graphics.DrawRectangle(p, r);
}
}
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
SetDisplayRectLocation(_borderWidth, _borderWidth);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19102 次 |
最近记录: |