C#写一个自由调节圆角角度同时自由调节边框阴影的panel控件
时间: 2023-10-19 13:14:44 浏览: 42
以下是一个简单的C#代码示例,可以创建一个自定义的Panel控件,使其可以自由调节圆角角度和边框阴影:
```csharp
using System.Drawing;
using System.Windows.Forms;
public class CustomPanel : Panel
{
private int _cornerRadius = 10;
private int _shadowDepth = 5;
public CustomPanel()
{
this.BackColor = Color.White;
this.DoubleBuffered = true;
this.ResizeRedraw = true;
}
public int CornerRadius
{
get { return _cornerRadius; }
set
{
_cornerRadius = value;
this.Invalidate();
}
}
public int ShadowDepth
{
get { return _shadowDepth; }
set
{
_shadowDepth = value;
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
int shadowOffset = _shadowDepth / 2;
// Draw the shadow
using (SolidBrush shadowBrush = new SolidBrush(Color.FromArgb(50, Color.Black)))
{
g.FillRoundedRectangle(shadowBrush, shadowOffset, shadowOffset, this.Width - _shadowDepth, this.Height - _shadowDepth, _cornerRadius);
}
// Draw the background
using (SolidBrush bgBrush = new SolidBrush(this.BackColor))
{
g.FillRoundedRectangle(bgBrush, 0, 0, this.Width - _shadowDepth, this.Height - _shadowDepth, _cornerRadius);
}
// Draw the border
using (Pen borderPen = new Pen(Color.Black, _shadowDepth))
{
g.DrawRoundedRectangle(borderPen, shadowOffset, shadowOffset, this.Width - _shadowDepth, this.Height - _shadowDepth, _cornerRadius);
}
}
}
public static class GraphicsExtensions
{
public static void FillRoundedRectangle(this Graphics graphics, Brush brush, float x, float y, float width, float height, float radius)
{
RectangleF rectangle = new RectangleF(x, y, width, height);
RectangleF innerRectangle = RectangleF.Inflate(rectangle, -radius, -radius);
GraphicsPath path = new GraphicsPath();
path.AddArc(innerRectangle.X, innerRectangle.Y, radius * 2, radius * 2, 180, 90);
path.AddArc(innerRectangle.X + innerRectangle.Width - radius * 2, innerRectangle.Y, radius * 2, radius * 2, 270, 90);
path.AddArc(innerRectangle.X + innerRectangle.Width - radius * 2, innerRectangle.Y + innerRectangle.Height - radius * 2, radius * 2, radius * 2, 0, 90);
path.AddArc(innerRectangle.X, innerRectangle.Y + innerRectangle.Height - radius * 2, radius * 2, radius * 2, 90, 90);
path.CloseAllFigures();
graphics.FillPath(brush, path);
}
public static void DrawRoundedRectangle(this Graphics graphics, Pen pen, float x, float y, float width, float height, float radius)
{
RectangleF rectangle = new RectangleF(x, y, width, height);
RectangleF innerRectangle = RectangleF.Inflate(rectangle, -radius, -radius);
GraphicsPath path = new GraphicsPath();
path.AddArc(innerRectangle.X, innerRectangle.Y, radius * 2, radius * 2, 180, 90);
path.AddArc(innerRectangle.X + innerRectangle.Width - radius * 2, innerRectangle.Y, radius * 2, radius * 2, 270, 90);
path.AddArc(innerRectangle.X + innerRectangle.Width - radius * 2, innerRectangle.Y + innerRectangle.Height - radius * 2, radius * 2, radius * 2, 0, 90);
path.AddArc(innerRectangle.X, innerRectangle.Y + innerRectangle.Height - radius * 2, radius * 2, radius * 2, 90, 90);
path.CloseAllFigures();
graphics.DrawPath(pen, path);
}
}
```
使用方法:
1. 将以上代码放入一个新的类文件中,例如CustomPanel.cs。
2. 在需要使用自定义Panel控件的窗体中,添加一个新的Panel控件。
3. 在设计器中,设置Panel控件的BorderStyle属性为None,将BackColor属性设置为白色(或其他颜色)。
4. 在代码中,实例化CustomPanel控件,并将其添加到窗体中,如下所示:
```csharp
CustomPanel customPanel = new CustomPanel();
customPanel.Location = new Point(10, 10);
customPanel.Size = new Size(200, 200);
this.Controls.Add(customPanel);
```
5. 可以通过设置CustomPanel控件的CornerRadius和ShadowDepth属性,来调整圆角角度和边框阴影的大小。例如:
```csharp
customPanel.CornerRadius = 20;
customPanel.ShadowDepth = 10;
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)