我想让面板有一个厚边框。 我能以某种方式设置吗?

我想让面板有一个厚边框。 我能以某种方式设置吗?

PS,我正在使用C#。 VS 2008。

吉姆

我已经创建了一个用户控件,并给出了一个ParentControlDesigner。 正如我在评论中指出的那样,它并不是你所要求的完美解决方案。 但它应该是一个很好的起点。 哦,任何FYI,我也有可定制的边框颜色。 我受到了另一个SOpost的启发,追求这个…它比我想象的要复杂。 要在设置边框大小时正确重新排列,可以调用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); } } 

只需实现面板的Paint事件并绘制边框。 例如:

 using System; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); panel1.Paint += panel1_Paint; } VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.Button.PushButton.Normal); private void panel1_Paint(object sender, PaintEventArgs e) { renderer.DrawEdge(e.Graphics, panel1.ClientRectangle, Edges.Bottom | Edges.Left | Edges.Right | Edges.Top, EdgeStyle.Raised, EdgeEffects.Flat); } } } 

四处寻找你喜欢的东西。 如果未启用视觉样式,则应该添加代码以回退到ControlPaint.DrawBorder。 咩。

如果这只是演示文稿,请使用所需边框颜色的背景颜色填充表单,并使用Dock样式填充。 将另一个面板放在这个面板内,使用标准背景颜色和Dock样式填充。 使用两个面板的填充和边距来获得您想要的边框大小(我忘记哪个参数适用于内部面板和外部面板)。 将控件放在内部面板上。 将两个面板设置为Dock = Fill,将自动为您处理表单大小调整。 您可能需要尝试一些控件,但我已经多次完成此操作,并且应用程序主窗口和弹出窗体都没有问题。

这是一个老post,但我仍然觉得它很有用。 我只是找到了另一种方式 。

 ControlPaint.DrawBorder(e.Graphics, control.ClientRectangle, Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset, Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset, Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset, Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset); 

这是一种操纵它,但我总是只为每个边框使用一个标签。 您必须将autosize属性设置为false并将一个停靠到每一侧(左侧,右侧,顶部,底部)。 然后只需设置宽度/高度/背景颜色即可。

您可以轻松地将其设置为用户控件,并公开一些自定义公共属性以设置您的宽度/高度以及所有标签的背景颜色以更改颜色。