我正在使用C#和winform应用程序.net版本3.5和Vs 2008
我如何创建具有圆边的自定义面板?我们如何在不同项目中使用该控件?
答案 0 :(得分:1)
您必须覆盖OnPaint事件并使用GraphicPath对象绘制角点。
看一下这篇文章:http://www.switchonthecode.com/tutorials/csharp-creating-rounded-rectangles-using-a-graphics-path
答案 1 :(得分:0)
您可以使用GDI +隐藏面板边缘并绘制带圆角的新边缘。
这是一个example。
答案 2 :(得分:0)
在MLablanc's link之后,我将代码转换为C#:
public class RoundedPanel : Panel {
private float _thickness = 5;
public float Thickness {
get {
return _thickness;
}
set {
_thickness = value;
_pen = new Pen(_borderColor,Thickness);
Invalidate();
}
}
private Color _borderColor = Color.White;
public Color BorderColor {
get {
return _borderColor;
}
set {
_borderColor = value;
_pen = new Pen(_borderColor,Thickness);
Invalidate();
}
}
private int _radius = 20;
public int Radius {
get {
return _radius;
}
set {
_radius = value;
Invalidate();
}
}
private Pen _pen;
public MpRoundedPanel() : base() {
_pen = new Pen(BorderColor,Thickness);
DoubleBuffered = true;
}
private Rectangle GetLeftUpper(int e) {
return new Rectangle(0,0,e,e);
}
private Rectangle GetRightUpper(int e) {
return new Rectangle(Width - e,0,e,e);
}
private Rectangle GetRightLower(int e) {
return new Rectangle(Width - e,Height - e,e,e);
}
private Rectangle GetLeftLower(int e) {
return new Rectangle(0,Height - e,e,e);
}
private void ExtendedDraw(PaintEventArgs e) {
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsPath path = new GraphicsPath();
path.StartFigure();
path.AddArc(GetLeftUpper(Radius),180,90);
path.AddLine(Radius,0,Width - Radius,0);
path.AddArc(GetRightUpper(Radius),270,90);
path.AddLine(Width,Radius,Width,Height - Radius);
path.AddArc(GetRightLower(Radius),0,90);
path.AddLine(Width - Radius,Height,Radius,Height);
path.AddArc(GetLeftLower(Radius),90,90);
path.AddLine(0,Height - Radius,0,Radius);
path.CloseFigure();
Region = new Region(path);
}
private void DrawSingleBorder(Graphics graphics) {
graphics.DrawArc(_pen,new Rectangle(0,0,Radius,Radius),180,90);
graphics.DrawArc(_pen,new Rectangle(Width - Radius - 1,-1,Radius,Radius),270,90);
graphics.DrawArc(_pen,new Rectangle(Width - Radius - 1,Height - Radius - 1,Radius,Radius),0,90);
graphics.DrawArc(_pen,new Rectangle(0,Height - Radius - 1,Radius,Radius),90,90);
graphics.DrawRectangle(_pen,0.0f,0.0f,(float)Width - 1.0f,(float)Height - 1.0f);
}
private void Draw3DBorder(Graphics graphics) {
DrawSingleBorder(graphics);
}
private void DrawBorder(Graphics graphics) {
DrawSingleBorder(graphics);
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
ExtendedDraw(e);
DrawBorder(e.Graphics);
}
}
要使用该类,只需在您的项目或同一文件中包含该类,您就必须声明一个Panel并使用RoundedPanel。
答案 3 :(得分:-1)
快速谷歌
winforms创建一个带圆角的自定义面板
作为第一个结果返回以下内容:
C# Form with custom border and rounded edges
要在其他项目中使用它,请将面板创建为UserControl,而不是窗口。