重绘panel控件,实现panel的阴影效果

时间:2023-03-09 13:24:06
重绘panel控件,实现panel的阴影效果

最近想在项目中添加一个要有阴影的panel控件,找了好多资料,最后通过采用图片的方式实现了panel的阴影效果,效果图如下:

重绘panel控件,实现panel的阴影效果

重绘代码如下:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D; namespace ShadowPanel3
{
public partial class UserPanel : Panel
{
public UserPanel()
{ }
/// <summary>
/// 字段和属性 , panel的颜色
/// </summary>
private Color _panelColor;
public Color PanelColor
{
get { return _panelColor; }
set { this._panelColor = value; }
} /// <summary>
/// 字段和属性,border的颜色
/// </summary>
private Color _borderColor;
public Color BorderColor
{
get { return _borderColor; }
set { this._borderColor = value; }
} /// <summary>
/// 阴影区域大小
/// </summary>
private int shadowSize = ; //将预备的小图标转化
static Image shadowDownRight = new Bitmap(typeof(UserPanel), "Images.tshadowdownright.png");
static Image shadowDown = new Bitmap(typeof(UserPanel), "Images.tshadowdown.png");
static Image shadowRight = new Bitmap(typeof(UserPanel), "Images.tshadowright.png");
static Image shadowTop = new Bitmap(typeof(UserPanel),"Images.tshadowtop.png");
static Image shadowLeft = new Bitmap(typeof(UserPanel),"Images.tshadowleft.png");
static Image shadowLeftDown = new Bitmap(typeof(UserPanel),"Images.tshadowleftdown.png");
static Image shadowLeftTop = new Bitmap(typeof(UserPanel),"Images.tshadowlefttop.png");
static Image shadowTopLeft = new Bitmap(typeof(UserPanel),"Images.tshadowtopleft.png"); /// <summary>
/// 重绘panel
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//Get the graphics object. We need something to draw with
Graphics g = e.Graphics; //下边和右边画笔
TextureBrush shadowRightBrush = new TextureBrush(shadowRight,WrapMode.Tile);
TextureBrush shadowDownBrush = new TextureBrush(shadowDown,WrapMode.Tile); //上边和左边画笔
TextureBrush shadowLeftBrush = new TextureBrush(shadowLeft, WrapMode.Tile);
TextureBrush shadowTopBrush = new TextureBrush(shadowTop,WrapMode.Tile); //给画笔定位
shadowDownBrush.TranslateTransform(, Height - shadowSize);
shadowRightBrush.TranslateTransform(Width - shadowSize, ); shadowTopBrush.TranslateTransform(,);
shadowLeftBrush.TranslateTransform(,); //每个阴影区域非配一个矩形区域
Rectangle shadowDownRectangle = new Rectangle(
shadowSize, //X
Height-shadowSize, //Y
Width-shadowSize*, //width(stretches)
shadowSize //height
); Rectangle shadowRightRectangle = new Rectangle(
Width-shadowSize, //X
shadowSize, //Y
shadowSize, //width
Height-shadowSize* //height(stretches)
); Rectangle shadowTopRectangle = new Rectangle(
shadowSize, //X
, //Y
Width-shadowSize*, //width
shadowSize //height(stretches)
); Rectangle shadowLeftRectangle = new Rectangle(
, //X
shadowSize, //Y
shadowSize, //width
Height-shadowSize* //height(stretches)
); //在底部和右边画出阴影
g.FillRectangle(shadowDownBrush,shadowDownRectangle);
g.FillRectangle(shadowRightBrush,shadowRightRectangle);
//在上部和左边画出阴影
g.FillRectangle(shadowTopBrush,shadowTopRectangle);
g.FillRectangle(shadowLeftBrush,shadowLeftRectangle); //四个角落处的阴影
g.DrawImage(shadowDownRight, new Rectangle(Width-shadowSize, Height-shadowSize,shadowSize,shadowSize));
g.DrawImage(shadowLeftDown, new Rectangle(, Height - shadowSize, shadowSize, shadowSize));
g.DrawImage(shadowLeftTop, new Rectangle(, , shadowSize, shadowSize));
g.DrawImage(shadowTopLeft, new Rectangle(Width-shadowSize, , shadowSize, shadowSize)); Rectangle fullRectangle = new Rectangle(
,
,
Width - (shadowSize + ),
Height - (shadowSize + )
); if (PanelColor != null)
{
SolidBrush bgBrush = new SolidBrush(_panelColor);
g.FillRectangle(bgBrush,fullRectangle);
} //给panel添加边框颜色
if (_borderColor != null)
{
Pen borderPen = new Pen(BorderColor);
g.DrawRectangle(borderPen,fullRectangle);
} //释放画笔资源
shadowDownBrush.Dispose();
shadowRightBrush.Dispose();
shadowLeftBrush.Dispose();
shadowTopBrush.Dispose(); shadowDownBrush = null;
shadowRightBrush = null;
shadowTopBrush = null;
shadowLeftBrush = null;
} //Correct resizing
protected override void OnResize(EventArgs e)
{
base.Invalidate();
base.OnResize(e);
}
}
}