gpt4 book ai didi

c# - 圆边面板

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:55 24 4
gpt4 key购买 nike

我正在使用 C# 和 winform 应用程序 .net 版本 3.5 和 Vs 2008

我将如何创建具有圆角边缘的自定义面板?我们如何在不同的项目中使用该控件?

最佳答案

正在关注 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);
}
}

要使用,只需将此类包含在您的项目中或在您声明面板的同一文件中,然后改用 RoundedPanel。

关于c# - 圆边面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11540117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com